Java: When to use a While Loop?
How do i do while loops in Java? - Stack Overflow
java basics while loops
While loop condition in java - Stack Overflow
Videos
My Intro to programming class has covered how to use both for and while loops. As for the code I am good to go with it but I am having a little trouble understanding them.
When would I use a while loop? Why would that be the better choice for the loop over a for loop?
Though I know how to code them I do not quite have a full comprehension on determining which to use and why one would be better than the other.
Any examples and/or like laymen's break down would be much appreciated!
In a while cycle you must put a condition. While that condition is true the cycle loop continue. An easy example should be:
int a = 0;
while(a<5){
System.out.println(a);
a++;
}
This cycle will be executed untill a is lower than 5. Each time that this cycle is executed the variable increase. The output will be:
0
1
2
3
4
So you should place a condition in your while loop that should not be always true (that cause an infinite loop) or always false (that will never execute your code).
First of all, don´t be discouraged because you might not understand everything at the first instance, this all comes with time and a lot of practice. I will do my best to explain the while loop for you.
A while-loop executes everything inside of the curly braces over and over again until the condition inside the parentheses evaluates to false. If your condition is always true, the loop will continue forever. Here is an example:
public static void main(String[] args) {
int i = 0;
while(i < 5) {
System.out.println("Looping...");
i = i + 1;
}
}
This loop would print "Looping..." five times, since i isn´t smaller than 5 after 5 iterations.
You can basically put any expression inside the parentheses that evaluates to a boolean. I would love to get more specific, but for that, it would be nice to know where your variables ja and nein are defined.
I would recommend you to go and practice the basics again, here is a good example that I used a while ago:
http://openbook.rheinwerk-verlag.de/javainsel/
What they're doing there is for each iteration of i, they're multiplying the result by 2, i times.
The walkthrough would be
i = 0, e = 0, result not multiplied, output 2 to the power of 0 is 1
i = 1, e = 1, result multiplied one time by 2, 2 to the power of 1 is 2
etc
They're decrementing e there to "count i backwards" down to 0. E is reset each time, and will always enter the while loop on iterations after the first one, and will always exit the while loop once it counts down to zero.
If you do not do e-- you will get stuck in an endless while loop.
Look at it like this:
Let's go into a random iteration of the for loop.
Let's say i=5.
Then e=i so e=5.
We now jump into the while loop.
while (e >0) {
result*=2;
e--;
}
If we did NOT do e--, e would always stay at 5 and the while loop would never terminate.
By doing e--, e will start at 5, then 4, .... then 0 where the while loop will terminate and we will jump back into the for loop, raise i (and consequently e) and repeat the whole process.