Looping statements

while () Statement:

         This executes a block of statements as long as a specified condition is true.

Syntax:

while(condition)

{

statement(s);

}

next_statement;

The (condition) may be any valid Java expression.

The statement(s) may be either a single or a compound (a block) statement.

Execution:

When program execution reaches a while statement, the following events occur:

The (condition) is evaluated.

If (condition) evaluates as false the while statement terminates and execution passes to the first statement following statement(s) that is the next_statement.

If (condition) evaluates as true the  statement(s) iside the loop are executed.

Then, the execution returns to step number 1.

Example:

int i=0;  //Initialize loop variable

System.out.println("Even numbers\n");

while(i<=10)

{

System.out.println(i);

i+=2;

}

do..while() Statement

do..while loop is used when you need to run some statements and processes once at least before checking the condition to execute the loop.

Syntax:

do

{

     statements;

}

while(condition);

next Statement;

Execution:

1. The statement in the do..while block is executed once.

2. Evaluate the condition.

3. If the condition evaluates to true goto step1. If the condition is false then goto step 4.

4. Execute the statement following the do..while statement.

Example:

int n = 12345;

int t,r = 0;

System.out.println("The original number : " + n);

do

{

t = n % 10;

r = r * 10 + t;

n = n / 10;

}while (n > 0);

for loop()

 The for loop statement is similar to while loop. Usually it is used to execute set of statements repeatedly a known number of times.

Syntax:

for( initialization; termination; increment/decrement)

{

statements;

}

next Statement;

Execution

When the loop first starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control variable, which acts as a counter that controls the loop.The initialization expression is only executed once.

Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates.

Next, the increment/ decrement  portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable.

The loop then iterates, first evaluating the conditional expression, then executing the body of the loop, and then executing the iteration expression with each pass. This process repeats until the controlling expression is false.



Example:

for (int i=1; i<=5; i++)

{

System.out.println("Value of i :" + i);

}

Enhance for loop()

Since JDK 1.5 Java introduce one more loop called Enhance for loop. Enhanced for loop allows you to iterate through a collection without having to create an Iterator or without having to calculate beginning and end conditions for a counter variable. 

The enhanced for loop was created as a shortcut for when you don't need the index of the element. It streamlined things.

int a[]= {10,20, 30, 40, 50};  

for(int x: a)  

{  

System.out.println(x);  

}   

Nested for loop()

Nested for Loops:

When we write a for loop under another for loop then it is called nested loop. The first for loop is called outer loop and the second for loop is called inner loop.

In case of nested loop the inner loop executes each time the outer loop got executed.

Example:

public class  ForLoop

{

  public static void main(String[] args)

  {

    for(int i = 1;i <;= 5;i++)  // Outer Loop

    {

      for(int j = 1;j <;= i;j++) //Inner Loop

      {

        System.out.print(i);

      }

      System.out.println();

    }

  }

}

Output:

D:/JAVA>javac ForLoop.java

D:/JAVA>java ForLoop

1

22

333

4444

55555 

Branching / Jump Statements:                  

break Statement:

When we need to exit from a loop before the completion of the loop then we use break statement.

When a break statement is encountered in the body of a loop, your program jumps to the statement immediatly following the loop body

The break statement is used in while loop, do - while loop, for loop and also used in the switch statement.

The break statement is normally used with if statement.

Example:

/* Example for break    */

class brk

{

public static void main(String args[])

{

for(int i=1;i<=100;i++)

{

if (i==5) break;

System.out.println(i);

}

}

Output:

D:\java>javac brk.java

D:\java>java brk

1

2

3

4

5

Continue Statement:

The continue statement is used to skip part of the loop when a condition is true, and continue remaining iterations of the loop.

The continue statement is normally placed within if statement.

Example:

/* Example for continue   */

class cont

{

public static void main(String args[])

{

          for (int i=0;i<=10;i++)

    {

if (i%2!=0) continue;

System.out.print(i+"\t");

     }

}

}

Output:

D:\java>javac cont.java

D:\java>java cont

0       2       4       6       8       10

2.9 labelled  statements

Labelled break

          The labelled break statement can be used as goto statement in Java. When the labelled break statement is encountered the control is transferred out of the named block.

Syntax

          break label;

         Here label is the name of the label that identifies the block of code. The block should be named before using the break statement.

The syntax for declaring a Label

         identifier:

Example:

/* Example for labelled break    */

class brklab

{

public static void main(String args[])

{

      outer: for(int i=0;i<=;5;i++)

{

    for (int j=0;j<=;5;j++)

    {

System.out.print("*");

if (i==j) break outer;

    }

    System.out.println();

}

}

}

Output:

D:\java>javac brklab.java

D:\java>java brklab

*

D:\java>

Labelled continue

 The labelled continue statement is used to skip part of the loop and continue iteration with the labelled loop

 

Syntax:

    continue label;

Example:

class brkcon

{

public static void main(String args[])

{

      outer: for(int i=1;i<=50;i++)

{

    if (i==6) break;

    System.out.println();

    for (int j=1;j<=;50;j++)

    {

System.out.print("* ");

if (i==j) continue outer;

    }

 

}

}

}

Output:

D:\java>javac brkcon.java

D:\java>java brkcon

*

* *

* * *

* * * *

* * * * *

* * * * * *

D:\java>

Comments

Popular posts from this blog

History of C language

Lab programs of C

Algorithm Pseudopods Flow-chat