Thursday, July 11, 2019

Looping In Java


            Conditional and Lopping Construct



There may be a situation when you need to execute a block of code several number of times. In general, statement are executed sequentially: the first statement in a function  executed first, followed by the second, and so on.
Programming languages provide various control structure that allow for more complicated execution path.
Loop statement allow us to execute a statement or group of statement multiple and following in the general form of a loop statement in most of programming languages 


Java programming language provides the following type of loop to handle looping requirement.
While Loop 
 Repeat  a statement or group of statements while a given condition is true. It test the condition before executing the loop body.

Syntax of while loop

while(Boolean_expression){
//statement
}
here, statement may be a single statement or a block of  statement. The condition may be any expression, and true is any non zero value.
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.
When the condition become false, program control passes to the line immediately following the loop.

Flow Chart of while loop


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

Example  of while loop 

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

     while(x<20){
      System.out.println("value of x :" +x);
      x++;
System.out.print("\n");
            }
       }
}
Output
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

For Loop

Execute a sequence of statements multiple times and abbreviates the code that manage the loop of variable.
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed specific number of times.

Syntax of for loop

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

Here .....
  • The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables and this step ends with a semicolon(;).
  • Next, the Boolean_expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of loop will not executed and controls jumps to the next statement past for the loop.
  • After the body of the loop gets executed, the control jumps back up to update statement. This statement allows to update any loop control variables. This statement can be left blank with a semicolon at the end.
  • The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeat(body of loop, then update step, then Boolean_expression).After Boolean_expression is false, the for loop terminates.

Flow Chart of for loop




Example of for loop

public class Test{

public static void main(String args[]){
   for(int x=10; x<20; x=x+1){
      System.out.println("value of x :" +x);
       System.out.print("\n");
                            }
              }
      }
Output :
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

Do While Loop

Like a while statement, expect that it tests the condition at the end of the loop body.
Syntax oh do while loop

do{
    //statements
}while(Boolean_expression)


  • Notice that the Boolean expression appear 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 control jumps back up to do statement, and the statements int the loop execute again. This process repeats until the Boolean expression is false.

Flow  Chart of do while loop



Example of do while loop:

public class test{
public static void main(String args[]){
int x=10;
do{
     System.out.println("value of  x :" +x);
       x++;
     System.out.print("\n");
       }while(x<20);
    }

}
Output :
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

No comments:

Post a Comment

                                    ARRAY IN JAVA Normally, array is a collection of similar type of elements that have contiguous memo...