Control Statements in Java with Example

0
323
Control Statement in Java
Advertisement

Control Statements in Java with Example

Whenever we need to execute a set of statements based on some condition then we need to use Control flow statements or Conditional Statements. Control flow statement executes the set of statements based on the condition is true or false. If condition is true then Control flow statement executes the set of instruction inside it otherwise if the condition is false then Control goes to the next Control flow or next defined statements.

Example: For example, we are printing the even and odd numbers from 1 to 10 then What should we do?

We first check the no. number is divided by 2 or not? If number is divided by 2 then we point that number as Even or if the number is not divided by 2 then we point that number as Odd.

Solution: In this tutorial we are covering the four types of Conditional Statements. These Conditional Statements are used in Java Programs as per their requirement. The conditional Statements are given below:

  1. If Statement
  2. Nested If Statement
  3. If-else Statement
  4. Nested If-else Statement

If statement

If Statement contains Condition that need to be verified and instruction or Set of Instruction. These instructions to be executed when the given condition under the If Statement is True.

Syntax for If Statement:

if(condition){
        //Instruction or
       //Set of Instructions
        }

The Statements only gets executed when the Condition given inside the If Statement is True otherwise the program simply skip the statements inside the If Statements and execute the next given command.

Please find the Flowchart that is very useful for you understanding with If Statement below:

If Statements Flow Chart

Example of If Statement

import java.util.Scanner;

public class If {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a Number : ");
        int num = scanner.nextInt();

        if (num>100){
            System.out.println("Entered Number is grater than 100.");
        }
    }
}

Output

Enter a Number : 
400
Entered Number is grater than 100.

Nested If Statement

Nested If Statement stated that, “When there are multiple if statements inside one-another is know as Nested If Statement.”

Syntax of Nested If Statement:

if(condition1){
    if(condition2){
        statement1
    }
}

As showed in the above syntax, Both condition1 and condition2 need to be True for executing the statement1. If the condition1 is false then condtion2 not checked and skipped the Nested If Block. If condition1 is True and condition2 is false then control goes inside to the first If Block and skipped the next If Block.

Example: Assume Government wants check that the person’s name who’s age is over 18 located in “New Delhi”.

For that question we need to put two conditional check 1st checks the aged is over 18 or not is the age is over 18 then we check that the person is located in “New Delhi” or not.

Example of Nested If Statement

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
	    // Take user input for location and age
	    Scanner sc = new Scanner(System.in);
	    System.out.println("Enter the Locality :");
	    String location = sc.nextLine();
	    
	    System.out.println("Enter the age :");
	    int age = sc.nextInt();
	    
	    //Check for Condition 1
	    if (age >= 18){
	        System.out.println("The age is >= 18");
	        //Check for Condition 2
	        if (location.equals("New Delhi")){
	            System.out.println("The Location is matched with <<New Delhi>>.");
	        }
	    } 
		
	}
}

Output

//Scenario - 1: When Location is New Delhi and age is greater than or equal to 18
Enter the Locality :                                                                                                          
New Delhi                                                                                                                     
Enter the age :                                                                                                               
23                                                                                                                            
The age is >= 18                                                                                                              
The Location is matched with <<New Delhi>>.

//Scenario - 2: When Location is not New Delhi and age is greater than or equal to 18
Enter the Locality :                                                                                                          
Raipur                                                                                                                        
Enter the age :                                                                                                               
24                                                                                                                            
The age is >= 18

//Scenario - 3: When Location is not New Delhi and age is not greater than or equal 18
Enter the Locality :                                                                                                          
Raipur                                                                                                                        
Enter the age :                                                                                                               
17

If-Else Statement

If-else conditional statement is advanced version of If Conditional Statement. It is a two way checking condition and executing it based on the input.

Syntax of If-else Control Statement:

if(condition) {
    //Statement executes when if condition is true
        }else{
    // Statement executes when if condition is false
    }

Example: Just assume you are checking a number is greater that 100 or not? if you are using only If Control Statement then you get output whenever the number is greater than 100 otherwise you didn’t get any output, But in If-else Control Statement you will get the output in both of the cases.

If-Else Statements Flow Chart

Example of If Else Statement

import java.util.Scanner;

public class If_Else {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a Number : ");
        int num = scanner.nextInt();

        if (num>100){
            System.out.println("Entered Number is grater than 100.");
        }else{
            System.out.println("Entered Number is less than 100.");
        }
    }
}

Output

// Output 1

Enter a Number : 
294
Entered Number is grater than 100.

//Output 2

Enter a Number : 
76
Entered Number is less than 100.

Nested If-Else Statement

When there are series of multiple If-Else Statements and sometimes the If-Else Statement inside one-another is called as Nested If-Else Statement.

Syntax of Nested If-Else Condition:

if (condition1){
    
    if (condition3){
        statement1;
    }else{
        statement2;
    }
    
}else if (condition2){
    statement3;
}else{
    statement4;
}

Example of Nested If-Else Statement

Example: As a Fact, The Peoples from SARC Countries have discount when they visit any Historical Monument over there.

Assume you are an Indian Citizen and your are visiting the TAJ MAHAL then how much discount you will get. Also Check for the Discount getting by the peoples who are from the SARC Countries.

As Problem stated here:

Discount given as per the Nationality (Food discount applied when you buy more than 300):

INDIAN : 70% discount (20% discount on Food)

SARC Countries : 50% discount (15% discount on Food)

Others : 20% discount (10% discount on Food)

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
	    // Take user input for Nationality
	    Scanner sc = new Scanner(System.in);
	    System.out.println("Enter the Nationality :");
	    String nationality = sc.next();
	    
	    System.out.println("Enter the Food Cost : ");
	    int food_cost = sc.nextInt();
	    
	    //Setting the Price for Visiting Taj Mahal
	    float taj_mahal_visit_cost = 1000f;
	    
	   
	    if (nationality.equals("India")){
	        
	        System.out.println("Taj Mahal Visit Cost: "+(taj_mahal_visit_cost-taj_mahal_visit_cost*0.7));
	        
	        if (food_cost>300){
	            System.out.println("Food Cost : "+(food_cost-food_cost*0.2));
	        }else{
	            System.out.println("Food Cost : "+food_cost);
	        }
	        
	        
	    } else if(nationality.equals("SARC")){
	        
	        System.out.println("Taj Mahal Visit Cost: "+(taj_mahal_visit_cost-taj_mahal_visit_cost*0.5));
	        
	        if (food_cost>300){
	            System.out.println("Food Cost : "+(food_cost-food_cost*0.15));
	        }else{
	            System.out.println("Food Cost : "+food_cost);
	        }
	        
	    }else {
	        
	        System.out.println("Taj Mahal Visit Cost: "+(taj_mahal_visit_cost-taj_mahal_visit_cost*0.2));
	        
	        if (food_cost>300){
	            System.out.println("Food Cost : "+(food_cost-food_cost*0.1));
	        }else{
	            System.out.println("Food Cost : "+food_cost);
	        }
	    }
		
	}
}

Output

// Scenario -1: If you are Indian and Paying more than 300 in food.
Enter the Nationality :                                                                                                       
India                                                                                                                         
Enter the Food Cost :                                                                                                         
400                                                                                                                           
Taj Mahal Visit Cost: 300.0                                                                                                   
Food Cost : 320.0 

// Scenario -2: If you are from SARC countries and Paying less than 300 in food.
Enter the Nationality :                                                                                                       
SARC                                                                                                                          
Enter the Food Cost :                                                                                                         
250                                                                                                                           
Taj Mahal Visit Cost: 500.0                                                                                                   
Food Cost : 250 

Hope, Control Statements in Java with Example post helps you to build the more knowledge in your Learning.

Also, find below links useful:

LEAVE A REPLY

Please enter your comment!
Please enter your name here