Looping Statements In Java

0





Looping Statements

Looping statements enable you to execute blocks of statements repeatedly. The Java programming language supports three types of loop constructs: for, while, and do loops. The for and while loops test the loop condition before executing the loop body; the do loops check the loop condition after executing the loop body. This implies that the for and while loops might not execute the loop body even once, whereas do loops execute the loop body at least once.

The for Loops

The for loop syntax is:

for ( ; ; )

For example:

for ( int i = 0; i < 10; i++ ) System.out.println(i + " squared is " + (i*i)); However, it is recommended that you place all loop-clause statements into a block. For example: for ( int i = 0; i < 10; i++ ) { System.out.println(i + " squared is " + (i*i)); } In the previous example, int i is declared and defined within the for block. The variable i is accessible only within the scope of this particular for block. Note – The Java programming language permits the comma separator in a for() loop structure. For example, for (i = 0, j = 0; j < 10; i++, j++) { } is legal, and it initializes both i and j to 0, and increments both i and j after executing the loop body. The while Loop

The while loop syntax is:

while ( )

For example:

int i = 0;

while ( i < 10 ) { System.out.println(i + " squared is " + (i*i)); i++; } Ensure that the loop-control variable is initialized appropriately before the loop body begins execution. You must update the control variable appropriately to prevent an infinite loop.
The do/while Loop

The syntax for the do/while loop is:

do

while ( );

For example:

int i = 0;

do {

System.out.println(i + ” squared is ” + (i*i));

i++;

} while ( i < 10 ); As with the while loops, ensure that the loop-control variable is initialized appropriately, updated in the body of the loop, and tested properly. Use the for loop in cases where the loop is to be executed a predetermined number of times. Use the while and do loops in cases where this is not determined beforehand. Special Loop Flow Control

You can use the following statements to further control loop statements:

● break [

Use the break statement to prematurely exit from switch statements, loop statements, and labeled blocks.

● continue [

Use the continue statement to skip over and jump to the end of the loop body, and then return control to the loop-control statement.

The label statement identifies any valid statement to which control must be transferred. With regard to a labeled break statement, the label can identify any statement. With regard to a labeled continue statement, the label must identify a loop construct.

The breakStatement

Here is an example loop with an unlabeled break statement:

1 do {

2 statement;

3 if ( condition ) {

4 break;

5 }

6 statement;

7 } while ( test_expr );

The continueStatement

Here is an example loop with an unlabeled continue statement:

1 do {

2 statement;

3 if ( condition ) {

4 continue;

5 }

6 statement;

7 } while ( test_expr );

Using breakStatements with Labels

Here is an example loop with a labelled break statement:

1 outer:

2 do {

3 statement1;

4 do {

5 statement2;

6 if ( condition ) {

7 break outer;

8 }

9 statement3;

10 } while ( test_expr );

11 statement4;

12 } while ( test_expr );

Using continueStatements with Labels

Here is an example loop with a labelled continue statement:

1 test:

2 do {

3 statement1;

4 do {

5 statement2;

6 if ( condition ) {

7 continue test;

8 }

9 statement3;

10 } while ( test_expr );

11 statement4;

12 } while ( test_expr );