Saturday, 4 January 2014

Iteration statement:while loop


How to use Java while:

Using while loop

While loop:

The while loop is java’s most fundamental looping statement.It repeats a statement or block while its controlling expression is true.In while statement 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.

Let’s look at syntax of while loop

while (expression)
{
   // Statement;
}


Example for simple while loop

//Demonstrate the while loop.
Class While Sample
{
Public static void main(string args[])
{
int n=10;
        while(n>0)
           {
               System.out.println(“Tick”+n);
            }
}
}

When you run this program,it will “Tick” ten times:
Tick 10
Tick 9
Tick 8
Tick 7
Tick 6
Tick 5
Tick 4
Tick 3
Tick 2 
Tick 1


Control Flow of If Statement



No comments :

Post a Comment