JAVA LOOP Statements

In the world of programming there many tasks or action which need to be repeated over and over again for some specific number of times,infinite,for some specific period of time or until some condition is needed to meet. Such an action or task is called loop. A loop may contains  single statement or a block of the statements.

For example salary of 1000 employees is increased by 5%. In this case  we have add increased 5% salary to list 1000 employees. Here we are repeatingthe same task for 1000 time for each employee in the organization.

Normally JAVA program execute sequentially from top to bottom but a loop statement causes a computer program to return to the same placemore than once, like a stunt plane completing an acrobatic loop.

In the world of JAVA there are three types of loops statements in Java: for, do, and while. Each can worklike the others, but it’s beneficial to learn how all three operate. You often can simplify a loop section of a program by choosing the right statement.

FOR LOOP


Java’s most complex loop statement is for. A for loop repeats a section of a program a fixed number of times. The following is an example:




              for (int i = 1; i <= 1000; i++) {
             System.out.println(“#: “ + i);
               }
               }


This is very simple loop statement, not more technical but clearly demonstrate the implementation of for loop. The above loop statement just prints
 integer from 1 to 1000.



In the above program , program prints the integer from 1 to 1000 and then execute the statement which prints For loop reached to finished line
Every for loop has a variable that determines when the loop should begin and end. This variable is called the counter (or index). The counter in the
preceding loop is the variable i.

The example illustrates the three parts of a for statement:

1. The initialization section: In the first part, the i variable is given an initial value of 1.

2. The conditional section: In the second part, there is a conditional test like one you might use in an if statement: i <= 1000.

3. The change section: The third part is a statement that changes the value of the i variable, in this example by using the increment operator.

Please note that these three parts are written in parenthesis and separated by semicolon after the keyword for.

The conditional section contains a test that must remain true for the loop to continue looping. When the test is false, the loop ends. In this example,the loop ends when the i variable is greater than 1,000.

Moreover statements inside the parenthesis is called the body of the loop which are executed for the specified no of times.



WHILE LOOP


While loop can do the same thing like for loop but while does not contain an initialization variable or couter.The while loop does not have as many different sections as a for loop. The only thing it needs is a conditional test, which accompanies the while statement.


The following is an example of a while loop:

    while (gameLives > 0) {
    // the statements inside the loop go here
    }

This loop continues repeating until the gameLives variable is no longer greater than 0.
The while statement tests the condition at the beginning of the loop before any statements in the loop have been handled. If the
tested condition is false when a program reaches the while statement for the first time, the statements inside the loop are ignored.
If the while condition is true, the loop goes around once and tests the while condition again. If the tested condition never changes inside the
loop, the loop keeps looping indefinitely.

The following statements cause a while loop to display the same line of text several times


int limit = 5;
int count = 1;
while (count < limit) {
System.out.println(“This is while loop”);
count++;
}




A while loop uses one or more variables set up before the loop statement. In this example, two integer variables are created: limit, which has a value of 5, and count, which has a value of 1. The while loop displays the text “This is while loop” five times. If you gave the count variable an initial value of 6 instead of 1, the text never would be displayed.


DO WHILE


do-while loop has the same function as that of for and while loop. it is similar to while loop except that the condition test is performed at
the end of the loop.

A general syntax is

        do {
        body of the statement.... }
        while (boolean experation)



The following statements cause a do-while loop to display the same line of text several times:

    int limit = 5;
    int count = 1;
    do {
    System.out.println(“i am executed at least once”);
    count++;
    } while (count < limit);




There are two differences between the while and do while loop


1. In do while loop the condition is tested at end of the loop
2. Body of the loop is executed at leat one after the condition is tested for repition, where as in while and for loop condition is test before the execution of the statment forming the body of the loop.


Like the while loop, this loop continues looping until the gameLives variable is no longer greater than 0. The do-while loop is different because the conditional test is conducted after the statements inside the loop, instead of before them.

When the do loop is reached for the first time as a program runs, the statements between the do and while are handled automatically, and then the while condition is tested to determine whether the loop should be repeated. If the while condition is true, the loop goes around one more time. If the condition is false, the loop ends. Something must happen inside the do and while statements that changes the condition tested with while, or the loop continued indefinitely. The statements inside a do-while loop always are handled at least once.

Like a while loop, a do-while loop uses one or more variables that are set up before the loop statement. The loop displays the text “i am executed at least once” four times. If you gave the count variable an initial value of 6 instead of 1, the text would bedisplayed once, even though count is never less than limit. In a do-while loop, the statements inside the loop are executed at least
once even if the loop condition is false the first time around.