With the help of Conditions and If statements , you can control the flow of your java program by deciding which code to run and which code to skipped.
Real time example: If score is more than 80,eligible for gift. Otherwise,not eligible.
if statements work with boolean values. Every if statement needs a condition that results in true or false.
Example
boolean isEligible=true;
if(isEligible){
System.out.println(“Score is more than 80”);
}
Mostly, conditions are created using comparison operators, as given below:
- Greater than: a>b
- Greater than or equal to: a>=b
- Less than: a<b
- Less than or equal to: a<=b
- Equal to: a==b
- Not equal to: a!=b
One can use these conditions to perform different actions as per requirement.
Java has the following conditional statements:
- Conditional statement if is used to specify a block of code to be executed, if the specified condition is true.
- Conditional statement else is used to specify a block of code to be executed, if the same condition is false.
- Conditional statement else if is used to specify a new condition to test, if the first condition is false.
- Conditional statement switch is used to specify many alternative blocks of code to be executed.
The if Statement
In java, use if statement to specify a block of code to be executed if a condition is true.
Syntax
if (condition){
//block of code to be executed if the condition is true
}
NOTE:
- if is in lowercase letters, uppercase letters (If or IF) will generate an error.
- if statement must result in boolean value.
In this example we will test two values to find out if 5 is smaller than 7. If the condition is true, print some text.
Example
if (5<7) {
System.out.println(“5 is smaller than 7”);
}
Variables can also be compared instead of hard-coded values:
Example
int x = 5;
int y = 7;
if(x < y){
System.out.println(” x is less than y”);
}
Comparision is also often used to check if two values are equal, using the == operator.
Example
int x = 33;
int y = 33;
if (x == y) {
System.out.println(” x is equal to y”);
}
You can also test boolean variables directly in an if statement.
Example
boolean isEligible = true;
if( isEligible ) {
System.out.println(“The person is eligible for gift”);
}
NOTE:
- If an if statement has only one line of code, you can write it without curly braces { }
Example:
if ( 7 > 5 )
System.out.println(” Seven is greater than five “)
- If we write any if statement without any braces and if the condition is false then only first line will not execute , but any other lines after it will run which can lead to unexpected result.
- To avoid mistakes, always use curly braces { }. This makes clear which lines belong to the if statement.
The else Statement
The else statement run a block of code when the condition in the if statement is false.
Syntax
if ( condition ) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Real Time Example:
If the score is more than 80, eligible for gift. Otherwise(else), not eligible for gift.
Example:
boolean isEligible= false;
if ( isEligible ) {
System.out.println(“Score is more than 80, so eligible for gift”);
}
else{
System.out.println(“Score is less than 80, so not eligible for gift”);
}
In the above example, since isEligible is false , so the condition inside if statement is not going to execute. So the else block will run instead , printing the message ” Score is less than 80, so not eligible for gift “.
NOTES:
- else statement doesn’t have a condition. It runs when the if statement condition is false.
The else if Statement
else if statement is used to specify a new condition if the first condition is false.
Syntax:
if ( condition1 ) {
// block of code to be executed if the condition1 is true.
} else if ( condition2 ) {
// block of code to be executed if the condition1 is false and condition2 is true .
} else {
// block of code to be executed if condition1 is false and condition2 is false.
}
Real time example :
If the score is more than 90, eligible for luxurious gift. Else if the score is more than 80 and less than 90, eligible for cute gifts . Else, not eligible .
Example:
int score = 85 ;
if ( score >90 ) {
System.out.println(“Eligible for luxurious gifts”);
} else if ( 80<score<90 ) {
System.out.println(” Eligible for cute gifts “);
} else {
System.out.println(” not eligible for gifts”);
}
In the above example , since score is 85, the first condition ( score > 90 ) is not met, so the if block skipped. Then it will check the else if condition ( 80<score<90 ), which is true. That means the else if block runs and prints ” Eligible for cute gifts”
