Another simple thing...
- While Loop runs the code inside loop, if the condition is true. It's checked from very beginning.
- But Do - While Loop runs the code inside loop once, then check whether the condition is true/false.
I think you guys have a good knowledge about while loop & done the exercise that I've given in the previous post.
- Let's recap...
public class main {
public static void main(String []args){
int counter = 0;
while (counter < 10 ){
System.out.println("Hello World!!");
counter = counter + 1;
}
}
}
public static void main(String []args){
int counter = 0;
while (counter < 10 ){
System.out.println("Hello World!!");
counter = counter + 1;
}
}
}
- Now Do- While....
public class main {
public static void main(String []args){
int counter = 0;
do{
System.out.println("Hello World!!");
counter = counter + 1;
}
while(counter < 10);
}
}
No difference in with previous the one and this.
Now see this
public class main {
public static void main(String []args){
int counter = 20;
do{
System.out.println("Hello World!!");
counter = counter + 1;
}
while(counter < 10);
}
}
Amazing huh !!! It displays "Hello World" once though the condition is false.. That's the Specialty of this Do -While loop
No comments:
Post a Comment