a=2 b=a++ b= ? (why 2)😠😠 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a=2 b=a++ b= ? (why 2)😠😠

3rd Jul 2016, 1:34 PM
Uğur Atakan Sürmeli
Uğur Atakan Sürmeli - avatar
6 Answers
+ 6
the answer is 'b=2' because first the value of 'a' is assigned to the variable 'b' then it is incremented. since at the time of assignment its value is 2 so result is "b=2"
3rd Jul 2016, 5:57 PM
cyberian
cyberian - avatar
+ 2
where is the code?
3rd Jul 2016, 3:15 PM
John Francis
John Francis - avatar
+ 2
it is important to understand the order of operations with the increment operator. There are 2 versions of the increment operator: $a++ ++$a there's one important a subtle difference between them. $a++ executes the increment LAST inside the line of code. ++$a executes the increment FIRST inside the line of code. example: $a = 5 print($a++) // prints 5 since increment executes after print. print($a) // prints 6 print (++$a) // prints 7 since increment executes before print.
6th Jul 2016, 1:44 PM
Alejandro Aristizabal
Alejandro Aristizabal - avatar
+ 1
operators Module 3 quiz 3 Th question
3rd Jul 2016, 5:09 PM
Uğur Atakan Sürmeli
Uğur Atakan Sürmeli - avatar
- 1
b=3
4th Jul 2016, 2:02 AM
Cheikhna Cherif Cheikhna
Cheikhna Cherif Cheikhna - avatar
- 3
$b=++$a
3rd Jul 2016, 4:16 PM
Антон Карицкий
Антон Карицкий - avatar