0

About else-if statement

int age = 25; if(age <= 0) { System.out.println("Error"); } else if(age <= 16) { System.out.println("Too Young"); } else if(age < 100) { System.out.println("Welcome!"); } else { System.out.println("Really?"); for the code given in the course, why don't both 'Welcome' and 'Really' get printed? Why does 'Really' get printed only when input >=100?

4th Jun 2018, 4:11 PM
Roslyn Chen
Roslyn Chen - avatar
1 Odpowiedź
+ 6
That's because the else block is a fallback when all other (previously specified in if and else if) conditions fail. So, Really? will only get printed when age is not under 100.
4th Jun 2018, 4:16 PM
DAB
DAB - avatar