While statement in JAVA
Sometimes we like to go round and round just for fun, like here on a Ferris wheel.
And some other times we go round to achieve something or improve something, like here where we try to cut a bar.Going round and round, each time advancing something, can take us very far.
Imagine now that we want to divide an integer number m by another integer n.
And we just want to know the integer division, that is the largest number of times that n fits into m.
the integer division of 7 divided by 2 is 3, because 2 times 3 is 6. And the remainder is 1, which is always less than 2. And imagine also that we don’t have any built-in operation that solves this for us,So we need to do a repeated subtraction.
And if we manage to subtract 2 from 7 three times, this means that the integer division is 3.
The integer division y and the integer remainder x are such that m equals y times n plus x holds.
Assume that we’re given integer numbers m and n, and that in x we store the remaining value after subtractions.
So let’s start with x equals m.
y will hold the integer division, we initialize it with 0 and increment y one by one each time we subtract n from x, and we continue subtracting n from x if x is at least n. So let’s see, if x is greater than or equal than n, then we subtract n from x and increment y by 1, and we do this as many times as possible.
This is why we repeat the same if statement many times.
If n cannot be subtracted from x because it doesn’t fit, nothing will be done.
So let’s run an example.
Assume m=7 and n=2. n fits 3 times in 7, and the rest is 1. So after initialization of x and y, we execute the first statement. Since 7 is greater or equal than 2, we subtract 2 from 7 storing 5 in x and we increment y by 1.
The resulting values 5 and 1.
Next statement, the same thing. Since the condition holds, x becomes 3 and y 2. Once again, 1 and 3, x equals 1 and y equals 3. But the next time 1 is less than 2, and therefore the statements are not executed and the values stored in x and y remain the same. If we try again, the same happens. You can see that there’s always a property that always holds.
Namely, m equals x times n plus y.
Now let’s add one column that computes y times n plus x, and we see that in fact we always get 7, that is m.
So this program does what we want but there are some problems. We have added many if statements just in case y becomes large enough. We don’t know how many of them we have to add because it depends on the actual numbers on the actual values of m and n.
For example, with 7 and 2 three statements three if statements would have been enough.
Now let’s add one column that computes y times n plus x,Wouldn’t it be nice to be able to say while something holds do something repeatedly until it doesn’t hold anymore? Well there is such a statement in Java: the While statement in JAVA.
Now this program does the same as the one before, it repeats the statement as many times as needed as long as the condition holds. But it has one big advantage.
The assignment statements are repeated as many times as needed, You don’t have to know that in advance, They are repeated while the condition x is greater or equal than n holds. And this can be three times, as in our example, or it can be 20 times, or one time, or never if the condition doesn’t hold from the very beginning.
Now the While statement in JAVA is very powerful, but you have to be very careful when writing the while condition. If you’re not careful you might never stop the repeated execution.
Watch out not to become dizzy if you turn around and around without end.