In this tutorial, you will learn about control flow statements in Java using Java if and if...else statements with the help of examples.
In programming we face some situations where we want a certain block of code to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program
if statement is the most simple decision making statement. The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
For example:
// Java program to illustrate if condition
public class IfStatementExample {
public static void main(String args[]){
int test=5;
if( test > 10 )
{
/* This println statement will only execute,
* if the above condition is true
*/
{
System.out.println("The number is greater.");
System.out.println("number is less than 10. ");
}
}
}
Output:
number is less than 10
Flow Chart
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement.
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
For example:
// Java program to illustrate if-else
public class IfElseExample {
public static void main(String args[]){
int num=10;
if( num < 5 ){
System.out.println("num is less than 5");
}
else {
System.out.println("num is greater than or equal 5");
}
}
}
Output:
num is greater than or equal 5
Flow Chart
The Java if-else-if statement executes one condition from multiple statements.. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
For example:
// Java program to illustrate if-else-if ladder
class ifelseifExample
{
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output:
i is 20
The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
For example:
// Java program to illustrate if-else-if ladder
public class NestedIfExample {
public static void main(String args[]){
int num=70;
if( num < 100)
{
System.out.println("number is less than 100");
if(num > 50){
System.out.println("number is greater than 50");
}
}
}
}
Output:
number is less than 100
number is greater than 50
Java provides a special operator called ternary operator, which is a kind of shorthand notation of if...else statement.
You can replace the following code:
if (expression) {
number = 10;
}
else {
number = -10;
}
with
number = (expression) ? expressionTrue : expressinFalse;