Flow Control Statements in Java

Flow Control Statements in Java

 In a java program the statements will executes in sequential order.If we do not want the sequential order of execution that is we want to execute the statements in a random order or according to programmer choice then we need to take the help of control statements.

   The control statements will helpers to decide the order of executing the statements.The different categories of control statements are

I.conditional statements
  i.if-else
  ii.switch

II.Iterating statements
 i.for loop
 ii.while loop
 iii.do-while loop

III.Transfer statements
 i.break
ii.continue

I.conditional statements

 i.if-else:

 The if-else statement  will execute a group of statements based on the result of a condition.

 Syntax: 
                if(condition){
                   Statrement---->(if block)
                    }
                   else{
                   statements2----->else block
                    }
Statement:If the condition is true will execute if block,if the condition is false will execute else block.

   Eg: class Sample{
          public static void main(String[]args){
                     int x=61;
                    if(x%2==0){
   System.out.println("even number");
}
else {
        System.out.println("odd numbr");
}
}
}

ii.switch:

  The switch statement must be used when we have multiple choices and the user has to select any one of the choice.

   Syntx:  Switch(argument){
 
   case value1:statement 1
                     break;
  case value2 : statement2
                     break;
        default  :default statement
}

Eg:

         class SwitchDemo{
public static void main(String[]args){
           int x=2;
           switch(x){

case1: System.out.println("first choice");
          break;
case2: System.out.println("second choice");
          break;
default:System.out.println("wrong choice");
}
}
}

The following rules apply to a switch statement:

i.The variable used in a switch statement can only be a byte, short, int, or char.
ii.You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
iii.The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
iv.When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
v.When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
vi.Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
vii.A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


II.Iterating statements

i.for loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

A for loop is useful when you know how many times a task is to be repeated.
Syntax:

The syntax of a for loop is:

for(initialization; Boolean_expression; update)
{
   //Statements
}

Here is the flow of control in a for loop:

The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop.
After the body of the for loop executes, the flow of control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression.
The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.

Example:

public class Test {

   public static void main(String args[]) {

      for(int x = 10; x < 20; x = x+1) {
         System.out.print("value of x : " + x );
         System.out.print("\n");
      }
   }
}

This would produce the following result:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

ii.while loop

  A while loop is a control structure that allows you to repeat a task a certain number of times.
Syntax:

The syntax of a while loop is:

while(Boolean_expression)
{
   //Statements
}

When executing, if the boolean_expression result is true, then the actions inside the loop will be executed. This will continue as long as the expression result is true.

Here, key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Example:

public class Test {

   public static void main(String args[]) {
      int x = 10;

      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

This would produce the following result:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

iii.do-while loop

 A do-while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
Syntax:

The syntax of a do...while loop is:

do
{
   //Statements
}while(Boolean_expression);

Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.

If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.
Example:

public class Test {

   public static void main(String args[]){
      int x = 10;

      do{
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }while( x < 20 );
   }
}

This would produce the following result:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

III.Transfer statements

 These statements are used for transfering the control from one location to another location.

i.break statement:
  This statements can be used either in switch statms or loops.The break stmt is used in the switch stmt then it will stop the execution of remaining stmts.The break stmt is used with in the loop then it is used to stop the remaining iterations avavilable in the loop.

Ex: class breakDemo{

 public static void main(String[]args){
                 int sum=0;
                 int capacity=30;
                 for(int i=1;i<=10;i++)
    System.out.println(i);
                sum=sum+i;
               if(sum>=capacity)
               break;
}
}
}

ii.continue statement:

  The continue statement can we used in only loops.This stmt will skip the current iteration and continue with the remaining iterations in the loop.

 Ex: class ContinueDemo{
 public static void main(String[]args){
           for(int i=1;i<=10;i++)
             if(i==3|i==6)
             continue;
    System.out.println(i);
}
}
}

0 comments:

Post a Comment