JAVA Control Statements

JAVA has three major control statements:-

  1. if statement,
  2. if – else statement, and 
  3. Switch statement.

if Statement:-

The statement inside if body executes only when the condition defined by if statement is true. If the condition is false then the compiler skips the statement enclosed in if’s body.

We can have any number of statement s inside if’s body.

Syntax:

if(Condition)
  {
  Statement;
  }

Example:

// Program to print series of numbers 1 to 10 using for loop.

 package com.company;
  public class Learner{
    public static void main(String[]args)
    {
     int a=3;
     in b=2;
     int sum=a+b;
     if (a>b)  
    {
     System.out.println(sum);
    }

if – else statement:-

In an if-else Statement, we have two blocks of statements. If the condition inside the if block results true then the if block gets executed, otherwise the statement inside the else block gets executed.

Else can’t exit without if statement.

Syntax:

if (condition1){
   Statement1;
  }else
  {
     Statement2; 
  }

Example:

// Program to print check the number is odd or even. 
package com.company;
import java .util.Scanner;
 public class Learner{
   public static void main(String[]args)
    {  Scanner sc=new Scanner(System.in);
     int n=sc.nextInt();
     if (n%2==0)  
         { 
           System.out.println("Even");
         }else
         {
             System.out.println("Odd");
         }
    }
    }

Nested if – else

When we write an entire if -else construct within either the body of if statement or in the body of else statement is called nested if.

Syntax:

if(condition1)
{
   Statement1;
 if(condition2)
 {
     Statement2;
 }else
 {
     Statement3;
 }
 }
  

Example:

package com.company;
 import java.util.Scanner;
 public class Learner{
   public static void main(String[]args)
    {  Scanner sc=new Scanner(System.in);
    int age=sc.nextInt();
    int weight=sc.nextInt();
     if (age>=18) {
         if(weight>50){
             ystem.out.println("You are eligible to donate blood");
         }else {
             System.out.println("You are not eligible to donate blood");
             }
             }
    }
    }

if – else – if ladder Statement:-

In if – else – if ladder if statement are executed from top to down. As soon as one of the condition of if is true , the Statement associated to it executed  and the rest of the else -if ladder is by passed. And if none of two condition is true then the final else statement.

Syntax:

if(condition1){
   Statement1;
  }else if(condition2){
      Statement2;
  }else{
      Statement3;
  }

Example:

package com.company;
import java.util.Scanner;
 public class Learner{
   public static void main(Static []args)
    { Scanner sc=new Scanner(System.in;
      int button=sc.nextInt();
     if(button==1){
         System.out.println("Hello Learner..");
     }else if(button==2){
      System.out.println("Namaste Learner..");   
     }else{
      System.out.println("Invalid");   
     }
    }
    }