+ 2

Ifā€¦ā€¦else

Why the output does not print both "Welcome " and "Really?" ? The condition for else statement is age ļ¼ž0, right? 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?"); } //Outputs "Welcome!"

31st Jan 2018, 7:55 AM
Iris
Iris - avatar
3 Answers
+ 17
if u want more than statements on the basis of two or more condition than condition must be provided by "if" , not by "else if"
31st Jan 2018, 9:11 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
int age = 25; if(age <= 0) /* This first if statement sets the condition to : age < or = to 0 which is false because age = 25 */ { System.out.printIn("Error"); // No output here } else if(age <= 16) { /* This else if statement assumes that age is NOT <= 0 AND is less or equal to 16 which is also false */ System.out.print("Yoo Young"); // Outputs nothing } else if ( age < 100) { /* This assumes that age is NOT <= 16 BUT > 16 AND < 100 (from 17 to 99) which is true */ System.out.printIn("Welcome"); // Outputs Welcome }else{ /* This last statement nests the precedent thus this means that age >= 100 which is false so the program outputs nothing.*/
31st Jan 2018, 8:01 AM
Ranjan Bagri
Ranjan Bagri - avatar
+ 8
I think here, the else statement is age>100 try to enter an age above 100 , i guess itll outputs" Really ?"
31st Jan 2018, 9:16 AM
Anon Fox
Anon Fox - avatar