5 Minutes of Java (5. Decision Making with if/else statement)

Decision making is done using the if/else statement. See example below on the code to determine whether a person passed a test:

int score = 59;

if (score >= 50){
    System.out.println("You passed the test.");
}
else{
    System.out.println("Try harder next time.");
}

To handle more than 2 conditions, you will use the else if statement. This is similar to elif in python.

int score = 65;
char grade = 'F';

if (score > 70){
   grade = 'A';
}
else if (score >= 50){
   grade = 'C';
}
else{
    grade = 'F';
}

System.out.println("Your grade is " +  grade);

Leave a Reply