Wednesday, October 07, 2015

Iteration Statements

Iteration Statements


Java’s iteration statements are for, while and do-while. These statements create what we commonly call loops. A loop repeatedly executes the same set of instructions until a termination condition is met. As you will see Java has a loop to fit any programming need.

While Loop

The while loop is Java’s most fundamental loop statement. It repeats a statement or block while its controlling expression is true. Here is its general form:

while(condition)
{
// body of loop
}

The condition can be any Boolean expression.

The body of the loop will be executed as long as the conditional expression is true.

When condition becomes false, control passes to the next line of code immediately following the loop.

The curly braces are unnecessary if only a single statement is being repeated.



// Demonstrate the while loop.
class WLWhile
{
     public static void main(String args[])
     {
          int n = 5;
          while(n > 0)
          {
               System.out.println("tick " + n);
               n--;
          }
    }
}

Type following command in command prompt

javac WLWhile.java
java WLWhile

tick 5
tick 4
tick 3
tick 2
tick 1

do while loop

do-while

The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is

do
{
     // body of loop
} while (condition);

Each iteration of the do-while loop first executes the body of the loop and then evaluates the conditional expression.

If this expression is true, the loop will repeat. Otherwise, the loop terminates. As with all of Java’s loops, condition must be a Boolean expression.

Here is a reworked version of the “tick” program that demonstrates the do-while loop.

// Demonstrate the do-while loop.
class WLDoWhile
{
public static void main(String args[])
{
int n = 5;
do
{
System.out.println("tick " + n);
n--;
} while(n > 0);
}
}

type following command in command prompt

javac WLDoWhile.java
java WLDoWhile

tick 5
tick 4
tick 3
tick 2
tick 1

As you just saw 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 loop at least once, even if the conditional expression is false to begin with. In other words, there are times when you would like to test the termination expression at the end of the loop rather than at the beginning.


Sunday, October 04, 2015

For Loop

For Loop

For loop is a powerful and versatile construct. Beginning with JDK 5, there are two forms of the for loop.


  • The first is the traditional form that has been in use since the original version of Java.
  • The second is the new “for-each” form.


for(initialization; condition; iteration)
{
      // body
}

If only one statement is being repeated, there is no need for the curly braces.

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.It is important to understand that the initialization expression is only executed once.

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 iteration 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.

Here is a version of the “tick” program that uses a for loop:

// Demonstrate the for loop.
class WLFor
{
public static void main(String args[])
{
int n=5;
for(n; n>0; n--)
{
System.out.println("tick " + n);
}
}
}

Using the Comma

Java allow two or more variables to control a for loop, Java permits you to include multiple statements in both the initialization and iteration portions of the for. Each statement is separated from the next by a comma.

Using the comma, the preceding for loop can be more efficiently coded as shown here:

// For Loop Using the comma.
class WLForComma
{
public static void main(String args[])
{
int a, b;
for(a=1, b=4; a<b; a++, b--)
{
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}

In this example, the initialization portion sets the values of both a and b. The two comma-separated statements in the iteration portion are executed each time the loop repeats.

The program generates the following output:

javac WLForComma.java
java WLForComma

a = 1
b = 4
a = 2
b = 3