Exercises for Preand Post increment and Decrement operators

Exercises for Pre-and Post- increment and Decrement operators int x = 0, y = 0; cout

Solution

Please follow the code and comments for description :

6)

CODE :

#include <iostream>

using namespace std;

int main()
{
int x = 0, y = 3;
while(y != 0){
cout << x << \" \" << y << endl;
x += 1;
y -= 1;
}
return 0;
}

OUTPUT :

x = 0 y = 3
x = 1 y = 2
x = 2 y = 1

7)

CODE :

#include <iostream>

using namespace std;

int main()
{
int x = 0, y = 3;
for (int i = 0; i < 3; i++) {
x = x + 1;
cout << \"x = \" << x << endl;
}
return 0;
}

OUTPUT :

x = 1
x = 2
x = 3

8)

CODE :

#include <iostream>

using namespace std;

int main()
{
int x = 0;
x += 1;
cout << \"x = \" << x << endl;
return 0;
}

#include <iostream>

using namespace std;

int main()
{
int x = 0;
x = x + 1;
cout << \"x = \" << x << endl;
return 0;
}


#include <iostream>

using namespace std;

int main()
{
int x = 0;
x += 1;
cout << \"x = \" << x << endl;
return 0;
}


#include <iostream>

using namespace std;

int main()
{
int x = 0;
x++;
cout << \"x = \" << x << endl;
return 0;
}


#include <iostream>

using namespace std;

int main()
{
int x = 0;
++x;
cout << \"x = \" << x << endl;
return 0;
}

OUTPUT :

For all these case the output is x = 1.

DESCRIPTION :

The main difference is that for the x += 1 and x = x + 1 is that both does the incrementing by a value of 1 where as the first operation is named as the shorthand notation and that the later is the general implementation of the incrementing the value of the variable.

The next two statements ++x and x++ also does the same incrementing but the difference is that in the first case the incrementing is done at first and then saved to the value of the variable and the later is like the incrementing is done after the saving of the data, these are generally named as the pre-increment and the post-increment operators in the programming context.

9)

CODE :

#include <iostream>

using namespace std;

int main()
{
int x = 0;
x += 1;
cout << \"x = \" << x << endl;
return 0;
}


#include <iostream>

using namespace std;

int main()
{
int x = 0;
x++;
cout << \"x = \" << x << endl;
return 0;
}


#include <iostream>

using namespace std;

int main()
{
int x = 0;
++x;
cout << \"x = \" << x << endl;
return 0;
}

OUTPUT :

For all these case the output is x = 1.

DESCRIPTION :

The main difference is that for the x += 1 and x = x + 1 is that both does the incrementing by a value of 1 where as the first operation is named as the shorthand notation and that the later is the general implementation of the incrementing the value of the variable.

The next two statements ++x and x++ also does the same incrementing but the difference is that in the first case the incrementing is done at first and then saved to the value of the variable and the later is like the incrementing is done after the saving of the data, these are generally named as the pre-increment and the post-increment operators in the programming context.


10)

CODE :

a)

#include <iostream>

using namespace std;

int main()
{
int x = 1, y = 2;
y = x++;
cout << \"x = \" << x << \" y = \"<< y << endl;
return 0;
}

OUTPUT :

x = 2 y = 1

b)

#include <iostream>

using namespace std;

int main()
{
int x = 1, y = 2;
y = ++x;
cout << \"x = \" << x << \" y = \"<< y << endl;
return 0;
}

OUTPUT :

x = 2 y = 2

c)

#include <iostream>

using namespace std;

int main()
{
int x = 1, y = 2;
int z = (++y - x--);
cout << \"x = \" << x << \" y = \"<< y << \" z = \" << z << endl;
return 0;
}

OUTPUT :

x = 0 y = 3 z = 2

d)

#include <iostream>

using namespace std;

int main()
{
int x = 1, y = 2;
int z = (--y + --x);
cout << \"x = \" << x << \" y = \"<< y << \" z = \" << z << endl;
return 0;
}

OUTPUT :

x = 0 y = 1 z = 1


Hope this is helpful.

 Exercises for Pre-and Post- increment and Decrement operators int x = 0, y = 0; cout SolutionPlease follow the code and comments for description : 6) CODE : #i
 Exercises for Pre-and Post- increment and Decrement operators int x = 0, y = 0; cout SolutionPlease follow the code and comments for description : 6) CODE : #i
 Exercises for Pre-and Post- increment and Decrement operators int x = 0, y = 0; cout SolutionPlease follow the code and comments for description : 6) CODE : #i
 Exercises for Pre-and Post- increment and Decrement operators int x = 0, y = 0; cout SolutionPlease follow the code and comments for description : 6) CODE : #i
 Exercises for Pre-and Post- increment and Decrement operators int x = 0, y = 0; cout SolutionPlease follow the code and comments for description : 6) CODE : #i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site