Custom Search

Friday, May 20, 2011

Intro to the Do While loop

In this post we will be covering the do while loop.  This loop is useful when you want to do something at least once but might have to do it more then once.  Well, lets begin!

public class Main{
public static void main(String[] args){
boolean x = true;
do{ 
System.out.println(x);

}while(x = false);
}
}


In this code we are creating a boolean(true or false) variable x and declaring it true.  We then create a do while loop thats says do this while this is true or false.  Because the JVM(java virtual machine, the thing that runs your application) reads from top to bottom it will print the value of x(true) and then look at the while condition.  The JVM stops looping because it sees that x = true and not x = false but it read from top to bottom,so it did it at least once before stopping.

If you have any questions on do while loops or any ideas for future posts feel free to leave a comment below.
Well, that wraps it up for this post, see you in the next!

No comments:

Post a Comment