DO.…while loop in Java
· In do while loop firstly
initialization is done
· Then body of loop is executed
· At the last while loop check the
condition
Format for
do while loop
Initialization
do
{
Body of the
loop
}
while(condition
check)
Example:
Write the program to print the number table
class Table
{
public static void main(String[] args) {
{
int num=2;
int table=0;
int j=1;
//initialize the value of j
do
{
table=num*j; //body of loop
System.out.println(+table);
j++;
}
while(j<11); //condition check
}
}
}
Here in
above program we print the table of 2
In above
program we initialize the j=1
Then body of
loop is executed
table=num*j;
num=2 and
j=1
num*j=2
hence
teble=2 we get our first out put
j++
value of j
is incremented by 1
now j=2
while(j<11);
means
condition is true
hence again
while loop is executed till condition in while loop get
false
output
2
4
6
8
10
12
14
16
18
20
No comments:
Post a Comment