Control flow statements

The control flow statements are the statements that have capabilities to take control in the hand of program execution. Each statement inside our program is generally executed from top to bottom, in the same order they are written.
There are three categories of control statements

  • Decision-Making statements (if-then, if-then-else, switch)

  • Looping statements (for, while, do-while)

  • Branching statements (break, continue, return)

Java If Statement with Example

It is the most basic and mostly used statement in our program statement. The if statement conditionally executes a single statement or block of code.

Java if Statement

Syntax

if (boolean expression){
// this block will be executed if the 
// boolean expression is evaluated to true;
statement…
}

If the Boolean statement is evaluated to true, the code written inside if statement will be executed. If the expression is evaluated to false then block will not be executed and it continues with program execution.

The below program checks whether an inputted number is even.

public class mydemo {

	public static void main(String[] args) {

		int n = 10;
		if (n % 2 == 0) { // check reminder of number
			System.out.println(n + " is Even Number");
		}
	}
}
Example Output

Explanation:
Here in this example, 10 (even number). So it will print “10 is EVEN number” on the console.

Java if-else statement with Example

An if statement can be followed by an optional else statement. Else statements execute if a Boolean expression inside if evaluated to false.

Java if-else statement
Syntax

if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}  

An if statement can have an else statement, here in the example –
Below the program check whether the inputted number is even or odd. If the number is even it will print Even number and Else the number is odd it will print Odd number.

public class mydemo {

	public static void main(String[] args) {

		int n = 35;
		if (n % 2 == 0) { // check reminder of number
			System.out.println(n + " is Even Number");
		}
		else
		{
			System.out.println(n + " is Odd Number");
		}
	}
}
Example Output

Java if-else-if ladder Statement with Example


There is another way of putting else if together when multiple decisions are involved. A multipath decision is a chain of if in which the statement associated with each else is an if.
This construct is known as the else if ladder.

Java if-else-if ladder Statement

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  
}  

public class mytest {

	public static void main(String[] args) {

		int marks = 20;
		
		if (marks >= 70) { // if statement
			System.out.println("Grade A");
		}
		else if (marks >= 30) { // else if statement
			System.out.println("Grade B");
		}
		else if (marks >= 90) { // if statement
			System.out.println("Grade C");
		}
		else { // else statement
			System.out.println("Grade D");
		}
	}
}
Example Output

Java Nested If statements with Example

When a series of decisions are involved, we may have to use more than one if…else statement in nested form as follows:

Java Nested if statement

The logic in execution is that, if the test condition-1 is evaluated to false, the test statement-3 will be executed. Otherwise, it continues to perform the second test.

Here below program employs nested if…else statements to determine the largest of given three numbers.

Syntax

if(condition){    
     //code to be executed    
          if(condition){  
             //code to be executed    
    }    
} 

public class mytest {

	public static void main(String[] args) {

		int a = 155, b = 400, c = 1000;

		System.out.println("Largest Value is :");

		if (a > b) {
			if (a > c) {
				System.out.println(a);
			} else {
				System.out.println(c);
			}
		} else {
			if (c > b) {
				System.out.println(c);
			} else {
				System.out.println(b);
			}
		}

	}
}
Example Output

Share