Control Statements

These provides the flow of execution in advance depend on the state of program.

3 categories of these are

selection, iteration, jump.

Selection

allows us to choose the different paths of execution of the expression or a state of variable.

if,switch

  • If

It provides execution of program through two different paths.

syntax:

if(condition)

statement 1;

else

statement 2;

condition is the expression which produces boolean value either true or false.

statement may be a single statement or a set of statements.

else is optional.

//example for if condition
class IfConditionExample{

     public static void main(String []args)
     {
         int a=24;
     if(a%2==0)
     {
         System.out.println("a is even number");
         System.out.println("value of a is::"+a);
     }
     else
     {
         System.out.println("a is odd number");
         System.out.println("value of a is::"+a);
     }
     }
}

if we uses two or more statements in condition we have to enclose those statements within curly braces.

Output::

a is even number

value of a is::24+

  • Nested If

one if statement have another if or else statement.

else statement is the reference to the nearest if statement within the same block.

//example for Nested if condition
class NestedIfExample{

     public static void main(String []args)
     {
        int a=5,b=50,c=15,d;
        if(a<=5)
        {
            if(b<40)
            d=b; // no else condition

            if(c>=10)
            d=c;
            else //for if(c>=10)
            d=a;
        }
        else
        d=10; // for if(a<=5)

        System.out.println("value of d is::"+d);
     }
}

Output:

value of d is::15

  • If-else-if ladder

The programming structure based on the sequence if-else conditions.

syntax:

if(condition)

statements;

else if(condition)

statements;

else if(condition)

statements;

|

|

else

statements;

This sequence will execute till any one of the if conditions is true then it will execute that corresponding if statements and then bypassed the rest of ladder.

If no one condition is true then it will execute the else statements which acts like a default statements.

If no else statement is there then no statements will execute.

//example for if-else-if ladder biggest of four numbers
class IfElseIfLadderExample{

     public static void main(String []args)
     {
        int a=5,b=50,c=15,d=20;;
        if(a>=b&&a>=c&&c>=d)
        {
            System.out.println("a is the biggest number "+a);
        }
        else if(b>=a&&b>=c&&b>=d)
        {
            System.out.println("b is the biggest number "+b);

        }
        else if(c>=a&&c>=b&&c>=d)
        {
            System.out.println("c is the biggest number "+c);
        }
        else
        {
            System.out.println("d is the biggest number "+d);
        }

     }
}

Output

b is the biggest number 50

  • Switch

It provides the easy way to select different paths based on the value of expression which makes the alternative of if-else-if ladder.

syntax:

switch(expression) // main diff is expression not condition like in if

{

case value1:

                sequence of statements;

                break;

case value2:

                sequence of statements;

                break;

|

|

case valueN:

                sequence of statements;

                break;

default:

              sequence of default statements;

}

In earlier versions the type of expression that allowed by java is byte, short, int, char.

But in JDK8 it allows us to compare String type also.+

No duplicate values are allowed in case values.

The type of expression and the values in the cases should be same.

It executes the expression first then it compare that value with the values of cases.

when it founds a same value then it executes that sequence of statements of that particular case and then bypassed, if no match is there then it will execute the default statements, if there is no default statements it will not do any function.

The break works like termination from that scope of switch. "jump out" of switch.

switching strings is more expensive than switching integers.

//example for switch condition
class SwitchExample{

     public static void main(String []args)
     {
        String str=new String("hello");
        switch(str)
        {
           case "hi":
                     System.out.println("hi"); // not executed
                     break;
            case "hello":
                     System.out.println("hello"); // will execute
                     break;
            default:
                    System.out.println("This is default block"); // not executed

        }


     }
}

Output:

hello

//example foe switch condition if there is no break

class SwitchExample{

     public static void main(String []args)
     {
        String str=new String("hello");
        switch(str)
        {
           case "hi":
                     System.out.println("hi"); // not executed
                     //break;
            case "hello":
                     System.out.println("hello"); // will execute
                     //break;
            default:
                    System.out.println("This is default block"); //will executed

        }


     }
}

Output:+

hello

This is default block

  • Nested Switch

A switch statement within a switch statement.

//example for nested switch
class NestedSwitchExample{

     public static void main(String []args)
     {
        int a=1,b=12;
        switch(a)
        {
            case 1:
                  System.out.println("value of a is::1");
                  switch(b)
                  {
                      case 11:
                               System.out.println("value of b is::11");
                               break;
                      case 12:
                              System.out.println("value of b is::12");
                              break;
                      default:
                             System.out.println("no matches found for b");
                  }
                  break;
            case 2:
                  System.out.println("value of a is::"+a);
                  break;
           default:
                 System.out.println("no matches found for a");

        }

     }
}

Output:

value of a is::1

value of b is::12

  • Difference between If and Switch control statements

If Switch
It evaluates the Boolean expression depends on that result it will executes set of statements. It checks equality of the expression with the case values, if same value found then it will execute the corresponding case statements.
No two values of case will be same.
More efficient than nested if.

results matching ""

    No results matching ""