Image

Java - Core Java - Control Statement

Control Statement

Control statements enable us to specify the flow of program control; ie, the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another. There are two types of control statements:

  • Branching Statments
  • Looping Statements
  • Branching Statements

    There are four types of if statement are used they are given below.

  • Simple if
  • If Else
  • Nested if else
  • Ladder else if
  • a. Simple If

    The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement

    Syntax:           If(condition)
                      {
                      Statement  1
                      ....
              ....
                      Statement  n
                      }
    
    EX: Program to check whether the entered number is positive negative or zero
    class IfPro{
        public static void main(String args[])
        {
            int n=0;
        if(n<0)
            System.out.println("Number is Positive");
        if(n<0)
            System.out.println("Number is Negative");
        if(n==0)
            System.out.println("Number is zero");
        }
    }   
    
    O/P
    D:\javap>javac IfPro.java
    D:\javap>java IfPro
    Number is zero
    

    b. If Else

    The if-else statement is probably the most basic way to control program flow. The else is optional, so you can use if in two forms:

    Syntax: If(condition)
            {
     Statement 1;
    }
    else
    {
    Statement 2;
    }
    
    Ex: program to check whether the entered number is even or odd.
    class IfElsePro
            {
            public static void main(String args[])
            {    int n=10;
                if(n%2==0)
                {
                System.out.println("Number is even");
                }
                else
                {
                System.out.println("Number is odd");
                }
            }
            }
    O/P
    D:\javap>javac IfElsePro.java
    D:\javap>java IfElsePro
    Number is even
    

    The conditional must produce a boolean result. The statement means either a simple statement terminated by a semicolon or a compound statement, which is a group of simple statements enclosed in braces. Any time the word “statement” is used, it always implies that the statement can be simple or compound.

    c. Nested else if

    A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.

    Syntax:      if(condition  1)
            {
                if(condition  2)
                    Statement  1;
                else
                    Statement  2;
            }
            else
            {
                if(condition  3)
                    Statement  3;
                else
                    statement  4;
            }
    
    EX: Program to check biggest number among three number.
    class Pro
            {
            public static void main(String args[])
            {    int a=3,b=7,c=22,big;
                if(a>b)
                {
                    if(a>c)
                        big=a;
                    else
                        big=c;
                }
                else
                {
                    if(b>c)
                        big=b;
                    else
                        big=c;
                }
                System.out.println("Big value is "+big);
            }
            }
    O/P
    D:\javap>javac Pro.java
    
    D:\javap>java Pro
    Big value is 22
    

    d. Ladder else if

    A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder. It looks like this:

    Syntax:     if (Test Condition -1)
                Statement -1;
            else if ( Test Condition -2)
                Statement -2;
            else if ( Test Condition -3)
                Statement -3;
                :
                :
            else if ( Test Condition –n)
                Statement –n;
            else
              default statement;
    
    EX: Program to find out the grade of a student when the marks of 4 subject are given.
    class Pro1
            {
            public static void main(String args[])
            {
            double m1=60,m2=75,m3=66,m4=84,total,per;
            char grade=' ';     
            total = m1+m2+m3+m4;
            per = total/4.0;
            if(per>85)
                grade='A';
            else if(per>=75)
                grade='B';
            else if(per>=55)
                grade='C';
            else if(per>=40)
                grade='D';
            else if(per>=40)
                grade='E';
           System.out.println("percentage "+per+" Grade is "+grade);
           }
           }
    O/P
    D:\javap>javac Pro1.java
    D:\javap>java Pro1
    percentage 71.25 Grade is C   

    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. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

    Switch Case Statement

    The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements.

    switch(expression)
            {
                  case1: statement  1;
            break;
                  case 2: statement  2;
            break;
                  case 3: statement 3;
            break;
                  case 4: statement N;
                  ....
                  ....
            break;
                  default:  default statement;  
        }
    Ex:The default statement is executed when no statement is executed.
    import java.util.Scanner;
    
    public class AJTD1 {
        public void findWeek(){
             Scanner sc=new Scanner(System.in);     
                System.out.println("Enter the number : ");
                int num=sc.nextInt();
                switch (num) {
                case 1:
                    System.out.println("Sunday");
                    break;
                case 2:
                    System.out.println("Monday");
                    break;
                case 3:
                    System.out.println("Tuesday");
                    break;
                case 4:
                    System.out.println("Wednesday");
                    break;
                case 5:
                    System.out.println("Thusday");
                    break;
                case 6:
                    System.out.println("Friday");
                    break;
                case 7:
                    System.out.println("Saturday");
                    break;            
                default:
                    System.out.println("Invalid month");                
            }
        }
        public static void main(String[] args) {
            AJTD1 a=new AJTD1();
            a.findWeek();
        }    
    }
    O/P
    D:\javap>javac AJTD1.java
    
    D:\javap>java AJTD1
    Enter the number :
    6
    Friday