Thursday, March 31, 2016

Do-While Loop

Hey guys, now we're gonna learn about Do-While Loop. Yeah I know I know you may wonder the heck is this do-while & what's the difference between while and Do - while loop.

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;                                             
}

}
}

  • 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