if-else Decision Making Examples In Java | infoStud blogs

if-else Decision Making Examples In Java

This post contains some examples related to if-else Decision Making in Java. Decision-making is one of the basic and important concepts not in Java but in any programming. The if statement tells us that if a condition is true and it will execute a block of code 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. 

We can use the else statement with if statement to execute a block of code when the condition is false. Now it's a time to watch some examples related to the if-else Decision making in Java

Examples

1. Check whether the input given by the users is suitable for finding the Area of the Rectangle.

If Length>Breadth

Length: 8

Breadth: 4

Code

import java.util.Scanner;

public class Checkrectangle {
    public static void main (String args[]){
        
        Scanner sc = new Scanner(System.in);  // Create a Scanner object
        System.out.println("Enter the Length Measurement");
        int Length = sc.nextInt();
        System.out.println("Enter the Breadth Measurement");
        int Breadth  = sc.nextInt(); 
        if(Length>Breadth){
            int cLength*Breadth;
            System.out.println("Area of Rectangle is: "+c);
        }
        else{
            System.out.println("Please Enter the valid Dimensions for the 
Recatngle as per the values assigned");
        }
    }
}

Output

Enter the Length Measurement

8

Enter the Breadth Measurement

4

 Area of the Rectangle is: 32

If Length<Breadth

Length: 4

Breadth: 8

Code

import java.util.Scanner;

public class Checkrectangle {
    public static void main (String args[]){
        
        Scanner sc = new Scanner(System.in);  // Create a Scanner object
        System.out.println("Enter the Length Measurement");
        int Length = sc.nextInt();
        System.out.println("Enter the Breadth Measurement");
        int Breadth  = sc.nextInt(); 
        if(Length>Breadth){
            int cLength*Breadth;
            System.out.println("Area of Rectangle is: "+c);
        }
        else{
            System.out.println("Please Enter the valid Dimensions for the
 Recatngle as per the values assigned");
        }
    }
}


Output

Enter the Length Measurement

4

Enter the Breadth Measurement

8

Please Enter the valid Dimensions for the Rectangle as per the values assigned

Post a Comment

0 Comments