Java If, If Else, If Else If Statement
This Lesson will teach us one of the control flow statements in Java programming: the if statement.
Just like every other programming language the if statement in Java is one way form of incorporating decision making on our program.
if statement (the one way selection)
This statement is used to test if the expression is true. In order for the action statement to be executed the expression must return true, otherwise the program will do nothing.
Syntax:
if (expressionToTest)
statementToExecute
Where:
expressionToTest is the Boolean or logical expression to be tested.
statementToExecute is the line(s) of codes to execute if the expression is true. It is sometimes called the action statement.
Example:
import java.util.*;
class IfStatement{
static Scanner console = new Scanner(System.in);
public static void main(String[] args){
int score;
System.out.print("Enter your score:");
score = console.nextInt();
if( score == 100 ){
System.out.print("you've got the perfect score");
}
}
}
In the example above the program will asked you to enter a score. If the user enters a score which is not 100 the program will do nothing because the expression states that the score must be 100 for the program to print the phrase “you’ve got the perfect score” without the quotes.
if else statement (two way selection)
if else statement is used if you want something to be executed even if the expression returns false.
Syntax:
if(expressionToTest){
statementToExecuteIfReturnsTrue
}else{
statementToExecuteIfReturnsFalse
}
Where:
expressionToTest is the Boolean or logical expression to be tested.
statementToExecuteIfReturnsTrue is the line(s) of codes to execute if the expression is true.
statementToExecuteIfReturnsFalse is the line(s) of codes to execute if the expression is false.
Example:
import java.util.*;
class IfStatement{
static Scanner console = new Scanner(System.in);
public static void main(String[] args){
int score;
System.out.print("Enter your score:");
score = console.nextInt();
if( score == 100 ){
System.out.print("you've got the perfect score");
}else{
System.out.print("study harder to get the perfect score");
}
}
}
Same with the previous example the program will asked you to enter a score. In the if statement if you enter a score which is not 100 the program will do nothing, but in this case even if you have not entered 100 as your score the program will return something and print the phrase “study harder to get the perfect score” without the quotes.
If else if statement
This statement is used to test several or various conditions.
Syntax:
if(expression1){
statementToExecuteIfExpression1isTrue
}else if(expression2){
statementToExecuteIfExpression2isTrue
}else if(expression3){
statementToExecuteIfExpression3isTrue
}else {
statementToExecuteIfAllExpressionFalse
}
Where:
expression1 is the first expression to be tested if true. If that returns true then statementToExecuteIfExpression1isTrue will be executed.
expression2 is the first expression to be tested if true. If that returns true then statementToExecuteIfExpression2isTrue will be executed.
expression3 is the first expression to be tested if true. If that returns true then statementToExecuteIfExpression3isTrue will be executed.
statementToExecuteIfAllExpressionFalse this statement will be executed if all of the expression returns false.
Example:
import java.util.*;
class IfStatement{
static Scanner console = new Scanner(System.in);
public static void main(String[] args){
int score;
System.out.print("Enter your score:");
score = console.nextInt();
if( score == 100 ){
System.out.print("you've got the perfect score");
}else if( score == 88 ){
System.out.print("you've got the average score");
}else if( score == 75 ){
System.out.print("you've got the passing score");
}else{
System.out.print("please enter another score");
}
}
}
In the above example is the working code on how to demonstrate a if else if statement in Java.
The program will asked you to enter a score, if your score is 100 then the program will print “you’ve got the perfect score”, if your score is 88 it will print “you’ve got the average score”, if your score is 75 then it will print “you’ve got the passing score”, but if your score is not 100, 88 and 75 the program will print “please enter another score” which means that the value of our score variable did not match to any of our expression statement.