Why when you change the order of curly braces affects the result of if and else statement? Check the code below and explain why | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why when you change the order of curly braces affects the result of if and else statement? Check the code below and explain why

int age = 17; if (age > 14) { if(age > 18) { Console.WriteLine("Adult");}} // i placed curly braces here and the answer differ with the code below! Why please help here am stuck! else { Console.WriteLine("Teenager");} // NO OUTPUT int age = 17; if (age > 14) { if(age > 18) { Console.WriteLine("Adult");} else { Console.WriteLine("Teenager");}} // i placed curly braces here and results differ with the above! //OUTPUT TEENAGER

14th Aug 2019, 10:52 PM
DataStream Cowboy
DataStream Cowboy - avatar
2 Answers
+ 5
After age>14 is satisfied, age>18 is not satisfied.
14th Aug 2019, 11:03 PM
Sonic
Sonic - avatar
+ 4
Ok. I see. It is currect. First example You close 2 } curlybrances, so... You exit out of 2 IFs. Thus the ELSE is executing somethin <= 14 that is why "no outputs" Of course the second example is more currect than first about our human logic.
14th Aug 2019, 10:59 PM
Ronaldo Marques
Ronaldo Marques - avatar