0

I don't understand what do x++ and x-- do

Im at a part where I need to figure out the output of code but I don't understand what the -- and ++ does even after repeating the lessons with this part

19th Jun 2017, 6:52 PM
sovietcat
sovietcat - avatar
8 Antworten
+ 1
x++; is the same as x=x+1; or x+=1; It increases the number by 1. -- decreases the number by 1
19th Jun 2017, 6:59 PM
Louis
Louis - avatar
+ 2
++ and -- are operators. something that is used for simplification. saying x++ is the same as adding one to x or x += 1 and the orientation of the operator matters as well. if you were to print out the variable x, then x++ : cout << x << " " << x++ << endl; you would see 5 5 however if you were to print out cout << x << " " << ++x << endl; you would see 5 6 because the operator incremented the variable before the output where as the one above did it after the output. the number will increment as long as the ++ operator is used somewhere around it. this is the opposite for -- using this operator will decrement the variable by one. in similar fashion with the out put as well, you could have --y or y--. they both subtract one just at different times. normally we would use x++ (like in for loops) this way it can test the variable x then increment it when its done. hope this helps!!
19th Jun 2017, 6:58 PM
Michael Szczepanski
Michael Szczepanski - avatar
+ 1
int a=3; int b=2; b=a++;// this time b value is 3 cout<<++b;//this time b value will increase by 1 than it will print. print value will be //4 cout<<a;//this time a value is 4
19th Jun 2017, 7:23 PM
MD. WOALID HOSSAIN
MD. WOALID HOSSAIN - avatar
0
x++ same as x=x+1 or x+=1 increase x value by 1 and x-- same as x=x-1 or x-=1 Decrease x value by 1
19th Jun 2017, 7:03 PM
MD. WOALID HOSSAIN
MD. WOALID HOSSAIN - avatar
0
ok but then tell me why the output of this code is: 4 int a = 3; int b=2; b=a++; //So here we forget that b=2 because the value is changed to b=4 cout<<++b; //But what happens here why is the output still 4 not 5 I dont understand the operators BEFORE the variable
19th Jun 2017, 7:04 PM
sovietcat
sovietcat - avatar
0
b=a++; // b gets assigned the value of a. a is increased by 1 after the assignment cout<<++b; // b is increased by 1 before it is printed
19th Jun 2017, 7:06 PM
Louis
Louis - avatar
0
Why is b=2 since a=3 and then b changes its value from 2 to b=a(a is 3) ++ (b increases by 1 because of the ++) 3+1=4 so im still confused
19th Jun 2017, 7:11 PM
sovietcat
sovietcat - avatar
0
int a=3; int b=2; b=a++; // b=a (b=3) and a=a+1 // b==3, a==4 cout<<++b; // b is increased by 1 (3+1=4) and then printed
19th Jun 2017, 7:15 PM
Louis
Louis - avatar