Custom Search

Friday, May 20, 2011

The While loop

In this post we will be covering the while loop.  A while loop is a block of code you want to loop through, but you don't know how many times you want to loop through.  A while loop is good for looping when you only want to loop when a condition is true.

public class Main{
public static void main(String[] args){
boolean print = true;
while(print = true){
System.out.println("Print = true);


}


}
}
We don't know for how long we want the print variable to remain true, so we use a while loop(While print = true) print  "Print = true"(because it is).  So once again while loops are good when you have a variable that is always changing and you only want to do something if something else is true.  This code would run forever though, because we keep the print variable true forever but for the purpose of this example that's ok.

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

No comments:

Post a Comment