+ 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
5 Réponses
+ 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
+ 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 ;)
+ 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
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
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.