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);

25th Jan 2017, 6:45 PM
Christopher
11 Answers
+ 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 */
25th Jan 2017, 7:06 PM
Robobrine
Robobrine - avatar
+ 7
ans is 2
10th Apr 2020, 6:55 PM
Shakhayet Tanveer
Shakhayet Tanveer - avatar
+ 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'
26th Jan 2017, 11:37 AM
Catalin Popinciuc
Catalin Popinciuc - avatar
+ 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
25th Jan 2017, 7:06 PM
Jafca
Jafca - avatar
+ 2
answer is 6
27th Jul 2017, 10:32 AM
Phumlani Nsibande
Phumlani Nsibande - avatar
+ 2
what is the value of b ? b = 1 if 2+2 == 5 else 2 2
21st Dec 2020, 4:26 PM
Albin Jusufi
+ 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
25th Jan 2017, 7:02 PM
Louis Milotte
Louis Milotte - avatar
0
the answer is 6
11th Jan 2021, 11:54 AM
Fredrick Brighton
Fredrick Brighton - avatar
0
6
23rd Jun 2022, 1:07 PM
ilham niya
ilham niya - avatar
0
the answer is 6
7th Nov 2022, 11:27 AM
Okereke Nnenna
- 3
What is the value of b? b = 1 if 2+2 == 5 else 2
29th May 2020, 7:28 PM
shweta shukla
shweta shukla - avatar