+ 3

PLS HELP ME

b= 3 b=b++ COUT <<b<<ENDL; what is the output and pls help me what is the concept of putting 2 plus signs after b

1st Jan 2017, 5:42 PM
MEDHIR
MEDHIR - avatar
5 odpowiedzi
+ 5
b++ : b+= 1
1st Jan 2017, 5:48 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
First please note that C++ is case sensitive so you will need to write 'cout' and 'endl'. b++ is an expression. By writing ++ after b you are using the post-increment operator. 'Post' here means 'after'. Post-increment has the side effect of incrementing (adding a unit to) the value of b after getting the current value of b. So b++ gives the current value of b (3) and then increments b, which changes it to 4. Then you assign (=) what was the current value of b (3) to b, overwriting the new value in b (4) with its previous value (3). Then the value of b (3) is output. If you intended to add 1 to b you can use one of several idioms: b = b + 1; b += 1; b++; ++b; Which to use depends on context and style. The third form (b++) is the most popular. That is a hold over from the C programming language. Strictly speaking, this requires a call to the copy constructor for b. I prefer the last form (++b), because it avoids using the copy constructor (improving speed and reducing memory use). Finally, to understand what is happening in your expression (b = b++) you need to understand that the processor does one thing at a time using operator precedence. ++ has higher precedence than =. So, ++ is done first, followed by =. Also, there are two elements to ++. Getting a value from a variable; incrementing that variable. With pre-increment (++b) the variable is incremented and its new value is retrieved. Post-increment (b++) retrieves the current value of the variable, saving a copy for the next step in the expression evaluation (by calling the copy constructor). Then it modifies the value of the variable. Sorry for the long answer to a straight-forward question, but getting a solid grasp of theses concepts is required to be an effective programmer.
1st Jan 2017, 6:15 PM
Gordon Garmaise
Gordon Garmaise - avatar
+ 1
b++ : b=b+1 output : 4
1st Jan 2017, 5:47 PM
Modhafer Hajji
Modhafer Hajji - avatar
0
Hi! firstly here some error in your code to write correct code like that: int b = 3; b = b++ cout << b << endl; because C++ is a case sensitive language so Compiler don't under stand the meaning of COUT & ENDL Now C++ is a strong language so every variable has a data type as int. b = b++ The increment operator ( ++ ) which is also know as postfix operator. Firstly b = b++ is an increment operator which perform increment operator as b = b +1; Secondly you just print the b value so the answer is just 3. if you you want to print the incremented value you must add an extra line above the cout << b <<endl; as cout << b++ << endl; cout << b << endl; Now the output is 3 // b++ has 3 4 // now b has 4 value because b++ get the value and then increment the value. For Example : We use as postpaid mobile network , firstly we use the data and after the end of the month we pay bill same as firstly we b = b++ get the value and then allot the value. so the after the execution finaly b = b++ has 3 value only. if we change a little in the code b = 3; a = b++; cout << b++ << endl; cout << b << endl;
1st Jan 2017, 6:39 PM
Abdul Rehman
Abdul Rehman - avatar
0
The output is 4. Shen you write a variable and ++ you add 1 to it's, and -- you remove 1.
1st Jan 2017, 8:29 PM
Hickacou
Hickacou - avatar