How it outputs 211 coulnt understand | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How it outputs 211 coulnt understand

var a = 0; let b= ++a; let c = a++ print("\(a)\(b)\(c)"). the answer is 211 how it did happen? isn't it print \0\1\2

7th Mar 2017, 10:25 PM
Kenny Zzy
Kenny Zzy - avatar
5 Answers
+ 6
a starts at 0 b = ++a means add 1 to a and then set the result equal to b, so now a is 1 and b is 1 c = a++ means set c equal to a and then add one to a, so now c is 1 and a is 2 at the end a is 2, b is 1 and c is 1 so the answer is 211
7th Mar 2017, 10:45 PM
Shane
+ 4
a is zero b is (a=a+1); assignment to b happens AFTER the incrementation of a c is (a=a+1); assignment to c happens BEFORE the incrementation of a so the output is correct. there are several to many posts here on how x++ and ++x work ;)
7th Mar 2017, 10:47 PM
Mario L.
Mario L. - avatar
+ 1
The values ​​are printed out after the assignment, then: ++a, a++ increase a of 2 b = ++a increase a of 1 and set b to 1 (equal a) c = a++ set c to 1 (equal a) and increase a of 1
7th Mar 2017, 10:44 PM
AtoMX
AtoMX - avatar
0
var a // incremented twice and equal 2 let c // constant at first get value of "a" and after incremented, but that's constant and change value is error That's why result 211
7th Mar 2017, 10:49 PM
Omelyan Burtnyk
0
Un complex answer: There is a reason why the rest of the "Variables" are defined as constants, so they are not affected by the shift in numbers (the ++ // -- ) In the string, the Var "A" is prone to being effected by the entire string's actions. Hence: Var A // 0, then Constant "B" effects Var A when it invokes A++, then when C invokes A++ again, it messes with the Var A.
1st Jun 2017, 11:22 PM
Jonathan Shidler
Jonathan Shidler - avatar