do while loop - Java Basic
Is it me, or is java for loops and while loops pretty much the same thing(Java)
Need help with Java while loops
Java: When to use a While Loop?
How do you exit a 'while loop' in Java?
What is the difference between a 'while loop' and a 'do-while loop' in Java?
How do you create an infinite 'while loop' in Java?
Videos
So i am in the middle of learning Java, and today i worked on loops. Specifically for and while loops. However, when i was given this problem to solve on codeacdemy, i noticed this.
While Loop
Examples 2:
class Coffee {
public static void main(String[] args) {
// initialize cupsOfCoffee
int cupsOfCoffee = 1;
// add while loop with counter
while(cupsOfCoffee <= 100) {
System.out.println("Fry drinks cup of coffee #" +cupsOfCoffee );
cupsOfCoffee ++;
}
}
}
Compared to:
For Loops:
class Coffee {
public static void main(String[] args) {
for (int cupsOfCoffee = 1;cupsOfCoffee <= 100;cupsOfCoffee++) {
System.out.println("Fry drinks cup of coffee #" + cupsOfCoffee);
}
}
}
For some reason, even though they pretty much resulted in the same output, for loops and while loops do pretty much the same thing, but for loops feel a tiny bit more simple(imo).
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!