Control Flow Introduction (Part1)
The while statement
The while statement is one way of allowing us to execute program statements a certain number of times. This is useful for cutting down on the amount of code u need to write. Its use is to control the execution of code given a test. Unlike the if statement the while statement
The while statement
The while statement is one way of allowing us to execute program statements a certain number of times. This is useful for cutting down on the amount of code u need to write. Its use is to control the execution of code given a test. Unlike the if statement the while statement will continue to executite the code it contains until it evaluates to false. For example if we wanted to list the numbers 1 to 10 and print them to the console there are several ways to achieve it. The first way, which is basically lame and takes a while bunch of time and code is like this.
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
System.out.println("6");
System.out.println("7");
System.out.println("8");
System.out.println("9");
System.out.println("10");
The while statements uses the following syntax.
while (expression) //evaluates the expression to a boolean {
code to be executed; // executes while expression = true
}
So a better way of counting 1 to 10 then printing it would be
int topNumber = 10; // Number to we want to count to
int currentNumber = 1; // Number we want to count from
while (currentNumber <= topNumber)
{
System.out.println("currentNumber"); //print currentNumber
currentNumber++; // increase currentNumber by 1
}
Not only is this a more elegent solution it also gives us the advantage of being more reusable. This code can print out from any integer to any valid integer just by changing topNumber and currentNumber. For example:
int topNumber = 10000; // Number to we want to count to
int currentNumber = 55; // Number we want to count from
... would print out between 55 and 10000. So we can use these 8 lines of simple code instead of 9945 lines of code executing System.out.println("n");
Basically while loops kick ass, and you need to know them.
With while loops we have to make sure the conditional will always be met at some point. or the code will keep on executing forever. For example this peice of code will continue to add 1 and print forever. This isnt good.
int topNumber = 54; // Mmm is less than currentNumber???
int currentNumber = 55; // Number we want to count from
Here the number we are counting to is smaller than the initial number we are counting from. So currentNumber will always be larger than topNumber, so will never stop counting. Watch out for these kinds of mistakes. They are easy to make and often hard to see. Here the reason the code will alwasy execute is because of the > sign. Did you spot it? Changing the Sign to a < means the code will [i]never[/i] execute. This should also be watched out for.
Just as if statements i alwasy use braces { } to enclose the excecutable code for the same reason. We can aslo have as many lines of code excecuted within the block as we want.
While loops can also be nested to any depth. This is useful if we want to execute whole while loops several times. For example.
int topNumber = 10; // Number to we want to count to
int currentNumber = 1; // Number we want to count from
int numberOfLoops = 5; //the number of times we want to print.
int currentLoop = 1;
while (curremntLoop <= numberOfLoops ) {
while(currentNumber <= topNumber) {
System.out.println(currentNumber);
}
currentLoop ++;
}
This code print out the numbers between currentNumber and topNumber, but it prints them out 5 times. Again this can be changed by changing NumberOfLoops.
Simple, elegant and sexy.
And that concludes our look at the while loop.
Don't just sit there like a lemon! Reply!
Got something to say? Now's the time to share it with the author and everybody else that reads this posting! Lemons need not apply.