+ 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
2 Antworten
+ 5
After age>14 is satisfied, age>18 is not satisfied.
+ 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.