0
What is the output of this code. Explain(dumb it down)
What is the output of this code? int a = 8; int b = ++a; if(a > 5) b -= 3; else b = 9; Console.WriteLine(b);
11 odpowiedzi
+ 12
int a = 8; /* sets a to 8 */
int b = ++a; /* increases a by one, so a is now 9, and also sets b to this value, so b is now also 9 */
if(a > 5) /* a is 9 and 9 > 5 is true, so we enter the if (next line) */
b -= 3; /* b -= 3 is the same as b = b - 3. b was 9 before, so now b is 9 - 3 = 6 */
else /* the if was true so we can skip the else part */
b = 9; /* skip */
/* now b is still 6, so the output will be 6 */
+ 7
ans is 2
+ 3
6. because u have an if there where it says if a>5 and it is! because a = 8. and the compiler is doing b = 9 - 3 //(9 because a is now = to a + 1). if a was < 5 , the compiler jumped to the 'else'
+ 2
1. a is 8
2. a is 9, b is 9
3. is 9 > 5? Yes
4. so b is 6
5. skip else
6. skip
7. print 6
+ 2
answer is 6
+ 2
what is the value of b ?
b = 1 if 2+2 == 5 else 2
2
+ 1
https://jsfiddle.net/ppjyksw4/
We start with two defaults.
A is 8. B is equal to a plus 1.
Steps
1. A is declared.
2. B is set to A plus 1.
3. logic: if a is bigger than 5, b is equal to 8+1-3. If A is equal to or smaller than 5, b is 9.
Since A is 8, obviously larger than 5, B is 8+1-3
0
the answer is 6
0
6
0
the answer is 6
- 3
What is the value of b?
b = 1 if 2+2 == 5 else 2