Khurram's World - JAVA Repitition
 
 
..Islam and Religion
 .Computer & IT
  Web Designing TIPS
..HTML & DHTML
..Active Server Pages
..JAVA & JAVA Applets
..JAVA & VB Script
..CGI & Perl
..Electronic Commerce
..Web Sites & Emails
 
Pakistan News Service
Bay Banner
 
 
 
Repitition  

Objective:
   
Learn about:

  • Use of repetition statements or loops
  • How to code the for, while and do-while loops
  • Loops with the continue and break statements
  • Nested Loop statements

Concept:

Loops

Often when writing a program, it is necessary to repeat a statement or statement block many times. This objective is accomplished with a repetition statement. These repetition statements are called loops. Three repetition statements are the for, while and do-while statements.

Every repetition statement has three parts:

  • Initializing loop variable

  • Loop terminating statement

  • Incrementing loop variable

for loop

The for statement is a repetition statement that is particularly well suited for executing the statements of a loop for a specific number of times. A for loop is usually used when we have to execute some statements for a fixed number of times.

Syntax

for (initialization; condition; increment) {
        statement / body;
}

Example

for (int i = 0; i < 10; i++) {
        System.out.println(“Your number is: “ + i);
}

while loop

The while statement is a repetition statement that is particularly used to execute the body of a loop, subject to a certain condition. A while loop is usually used when we want specified statements to be continuously executed to a certain condition.

Syntax

While (condition) {
        Statement / body;
}

Example

int i = 0;
while (i < 10) {
        System.out.println(“Your number is “ + i );
        i++;
}

Difference between for and while Loops

The while loop is good when you don’t initially know how many times you want to execute the loop. The for loop is suitable when executing the body of a loop for a specific number of times.

Do-while loop

The do-while statement is similar to the while statement, except that it’s termination condition is at the end of the body of the loop. The do-while loop executes the statement in the loop until the condition becomes false. The condition is evaluated at the end of the loop; therefore, the body of the loop is always executed at least once. The syntax of do-while loop is:

Syntax

do {
        statement;
}      while (condition);

Difference between while and do-while loops

The difference between the while and do-while loops is obvious from the syntax. In the while loop the loop condition is evaluated first and then the body of the loop executes. In the do while loop the body is executed once and then the condition is checked for the loop execution. In the do-while loop there is a semicolon at the end of the while (condition), statement.

Example

int i = 10;
do {
        System.out.println(“The Number is: “ + i);
        i++;
} while (i < 10);

Nested for statement

In the nested for loop, just like in the nested if statement, we declare a for loop in the body of another loop. This forms a block of for loops. We can declare as many for loops in a block as we want.

Syntax

for (initialization; condition; increment) {
        for (initialization; condition; increment) {
                statement / body;
        }
        statement;
}

Mode of Execution

First of all the outer for loop will execute one time. If the condition is true, then program control will shift to the next for loop. If the conidtion in the second for loop is true then the body of the inner for loop will execute. It is to be noted there that until the inner loop has not executed completely, the control will not shift to the outer loop. When the program control shifts to the outer loop then there will be increment / decrement in the variable as specified in the outer loop.

Example

for (int i = 0; i < 5; i++) {
        for (int k = 0; k < 5; k++) {
                System.out.println(“The number of k is: “ + k);
        }
        System.out.println(“The number of i is: “ + i);
}

Special Loop Flow Controls or Jump Statements

Continue

The continue statement is used to skip over and jump to the end of the loop body. Some times it is useful to force an early iteration of a loop. That is, you might want t continue running the loop, but stop processing the remainder of the code in its body for this particular iteration. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop. In the for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

Example

class Continue {
        public static void main(String args[]) {
                for (int x = 0; x < 10; x++) {
                        System.out.println(x + "  :");

                                if (x % 2 == 0) continue;

                                        System.out.println("  :");
                }
        }
}

Break

In java, the break statement has three uses. First it terminates a statement sequence in a switch statement. Secondly, it can be used to exit a loop. Thirdly, it can be used as a civilized for of goto.

By using break, you can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop.

Example

class BreakLoop {
        public static void main(String args[]) {
                for (int i = 0; i < 100; i++) {
                        if (i == 10) break;
                        System.out.println("i: " + i);
                }
        System.out.println("Loop complete");
        }
}

When used inside a set of nested loops, the break statement will only break out of the innermost loop.

Example

class BreakLoop2 {
        public static void main(String args[]) {
                for (int i = 0; i < 3; i++) {
                       System.out.println("Pass " + i);
                        for (int j = 0; j < 100; j++) {
                               if (j == 10) break;
                               System.out.println(j + " ");
                        }
                        System.out.println();
                }
        System.out.println("Loop Complete");
        }
}