Java Control Statements Iteration Statements


              Java Control Statements Iteration Statements

Java's iteration statements are for, while, do-while. These statements create loops. A loop repeatedly executes the same set of instructions until a termination condition is met.

While:

While loop is repeats a statement or block while its controlling expression is true. Here is its general form.

while (condition) {
         body of loop
}

The body of loop will be executed as long as the condition is true. When condition becomes false, control passes to the next lie of code immediately following the code. The curly brackets are unnecessary if only a single statement is being repeated.
Here an exp of program using while loop that counts down from 15, printing exactly fifteen lines of "check":

/*
         This is a Simple Program Showing Use of while loop.
          Program developed by #Asad Hussain
         This file name is "Expwhile.java".
*/
import java.util.*;

public class Expwhile {
public static void main(String args[]){

        int n = 15;

        while (n > 0){
               System.out.println("check " + n);
               n--;
}

}
}


Output of this Program:
Java Program Showing use of while loop by Asad Hussain

Do-while:

If the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a while loop at least once, even the condition is false. For that reason so-while loop is used. The do-while loop always executes its body at least once, because  its conditional expression is at the bottom of the loop. The general form is :

do {
       body of loop
} while (condition);

The iteration of do-while loop executes the body of the loop and then evaluates the conditional expression.
The do-while loop is useful when you process a menu selection. because you will usually want the body of a menu loop to execute at least once.

/*
         This is a Simple Program Showing Menu Selection using do-while loop.
          Program developed by #Asad Hussain
         This file name is "Menudowhile.java".
*/
import java.util.*;

public class Menudowhile {
public static void main(String args[])
    throws java.io.IOException{
    char choice;

    do {
        System.out.println("Help on:");
        System.out.println(" 1. if");
        System.out.println(" 2. switch");
        System.out.println(" 3. while");
        System.out.println(" 4. do-while");
        System.out.println(" 5. for\n");
        choice = (char) System.in.read();
    }    while( choice < '1' || choice > '5');
   
    System.out.println("\n");

    switch(choice){
        case '1':
            System.out.println("The if:\n");
            System.out.println("if(condition) statement;");
            System.out.println("else statement");
            break;
        case '2':
            System.out.println("The switch:\n");
            System.out.println("switch(expression) {");
            System.out.println(" case constant");
            System.out.println("  statement sequence");
            System.out.println(" break;");
            System.out.println(" // ...");
            System.out.println("}");
            break;
        case '3':
            System.out.println("The while:\n");
            System.out.println("while(condition) statement;");
            break;
        case '4':
            System.out.println("The do-while:\n");
            System.out.println("do {");
            System.out.println("  statement;");
            System.out.println("} while (condition);");
            break;
        case '5':
            System.out.println("The for:\n");
            System.out.println("for(init; condition; iteration)");
            System.out.println(" statement;");
            break;
    }
   

}
}


Output of this Program:
Java Program Showing use of do-while loop by Asad Hussain

For:
It is a powerful construct. Here is the general form of the for statement:
for (initialization; condition; iteration) {
     body of loop
}
If only one statement is being repeated, there is no need for curly brackets. The for loop operates as follows. When loop first starts, the initialization portion of the loop is executed. Generally, this is an expression that set the value of the loop control variable, which acts as a counter that controls the loop. It is important to understand that initialization is executed once. Then, condition is evaluated. This must be a Boolean expression. It test the loop control variable against a target value. If expression is true, then body of loop is executed. If it is false , the loop terminates. Next, the iteration portion of the loop is executed. This 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, until the controlling expression is false.
Here is an exp showing Use of For loop:
 /*
         This is a Simple Program Showing comparison of two number using for loop.
          Program developed by #Asad Hussain
         This file name is "Expfor.java".
*/
import java.util.*;

public class Expfor {
public static void main(String args[]){

        int x, y;

        for ( x = 1, y = 40; x < y; x++, y--)
            System.out.println("x = " + x);
            System.out.println("y = " + y);

}
}
Output of this Program is:
Java Program Showing comparison of two number using for loop by Asad Hussain

Written By: Asad Hussain

Post a Comment

Previous Post Next Post