10. ITERATION STATEMENT

There are three Iteration statement in java. They are.....
  1. for
  2. while
  3. do-while
while
while loop is the most fundamental loop statement in java. It repeats or block while it's controlling expression is true. Here general form

while(condition){
//body of loop
}

The condition can be boolean expression. The body of the loop will executed as long as the conditional expression is true. If the condition becomes false, then control passes to the next line of code immediately following the loop.

Example:

import java.util.Scanner;
public class TestWhile {
public static void main(String args[]){
Scanner pavel = new Scanner(System.in);
System.out.println("How much do you want the loop execute from 0");
int loop = pavel.nextInt();
int x = 0;
while(x<=loop){
System.out.println("NUmber"+"\t"+x);
x++;
}
}
}

OUTPUT:

How much do you want the loop execute from 0
5
NUmber 0
NUmber 1
NUmber 2
NUmber 3
NUmber 4
NUmber 5

Now look here,we input 5 .then it output us o to 5.Because here x is initialize as 0 and loop execute up to the x<=our inputed number.

In the example initially x=0; when while loop execute first it see, is (x<=loop) is true. Our input is 5.
so loop =5.
Now for x=0 0<=5 is true
So execute the body statement .first found print statement so execute Number 0
then see increment statement x++
So execute this. now x=0+1=1
Now for x=1 1<=5 is true
So execute the body statement .first found print statement so execute Number 1
then see increment statement x++
So execute this. now x=1+1=2
............................................................................................................................
.............................................................................................................................
.............................................................................................................................
Now for x=5 5<=5 is true
So execute the body statement .first found print statement so execute Number 5
then see increment statement x++
So execute this. now x=5+1=6

Now for x=6 6<=5 is false
So loop end.


Some times we think ,what will happen if we set increment statement in the top of the statement ….now we will see that

import java.util.Scanner;
public class TestWhile {
public static void main(String args[]){
Scanner pavel = new Scanner(System.in);
System.out.println("How much do you want the loop execute from 0");
int loop = pavel.nextInt();
int x = 0;
while(x<=loop){
x++;
System.out.println("NUmber"+"\t"+x);
}
}
}

OUTPUT:
How much do you want the loop execute from 0
5
NUmber 1
NUmber 2
NUmber 3
NUmber 4
NUmber 5
NUmber 6

Now see here the same code only increment statement is in the top of the while loop and input is same. But we see here we found a different output. Now I will explain you that.
In the this example also initially x=0; when while loop execute first it see, is (x<=loop) is true. Our input is 5.
so loop =5.
Now for x=0 0<=5 is true
So execute the body statement .First see increment statement x++
So execute this. now x=0+1=1
Then found print statement so execute Number 1
Now for x=1 1<=5 is true
So execute the body statement .First see increment statement x++
So execute this. now x=1+1=2
then found print statement so execute Number 2
............................................................................................................................
.............................................................................................................................
.............................................................................................................................
Now for x=5 5<=5 is true
So execute the body statement .first see increment statement x++
So execute this. now x=5+1=6
Then found print statement so execute Number 6

Now for x=6 6<=5 is false
So loop end.

Penulis : Mostafij ~ Sebuah blog yang menyediakan berbagai macam informasi

Artikel 10. ITERATION STATEMENT ini dipublish oleh Mostafij pada hari Tuesday, April 9, 2013. Semoga artikel ini dapat bermanfaat.Terimakasih atas kunjungan Anda silahkan tinggalkan komentar.sudah ada 0 komentar: di postingan 10. ITERATION STATEMENT
 

0 comments:

Post a Comment