If I'm interpreting your question correctly you'd like real life examples that translate to code. Maybe something like the following.
You want to drive or ride your bike to the ice-cream shop a few miles away. You don't know how far it is exactly but you will recognize it when you get there (while), VS You want to ride your bike 15 miles out and then return and you have an odometer on the bike (for)
You want to shoot baskets until you have made 100 attempts (for) VS You want to shoot baskets until you have made 50 successful shots (while). Outside the US, substitute free kicks.
You want to write a 15 page paper on looping in Java (for) VS You want to work on your "looping in Java" paper until you are satisfied with the result (while).
You want to boil potatoes until a fork can be easily inserted (while) VS You want to boil potatoes for 11 minutes (for).
To translate these into code you need some imagined functions for the actual activities (void boilPotatoes()).
For some of them it is useful to discuss the empty case: you are already at the ice-cream shop, etc. For others, a discussion of "at least one iteration" might be useful to have.
If you want to include foreach in the game:
- You want to polish all of your mom's teacups (foreach) VS You want to polish her ten most valuable teacups (for) VS You want to polish teacups until you run out of polish (while).
Videos
How many types of loops are there in Java?
What are the types of looping statements in Java
When should I use a for loop versus a while loop in Java?
If I'm interpreting your question correctly you'd like real life examples that translate to code. Maybe something like the following.
You want to drive or ride your bike to the ice-cream shop a few miles away. You don't know how far it is exactly but you will recognize it when you get there (while), VS You want to ride your bike 15 miles out and then return and you have an odometer on the bike (for)
You want to shoot baskets until you have made 100 attempts (for) VS You want to shoot baskets until you have made 50 successful shots (while). Outside the US, substitute free kicks.
You want to write a 15 page paper on looping in Java (for) VS You want to work on your "looping in Java" paper until you are satisfied with the result (while).
You want to boil potatoes until a fork can be easily inserted (while) VS You want to boil potatoes for 11 minutes (for).
To translate these into code you need some imagined functions for the actual activities (void boilPotatoes()).
For some of them it is useful to discuss the empty case: you are already at the ice-cream shop, etc. For others, a discussion of "at least one iteration" might be useful to have.
If you want to include foreach in the game:
- You want to polish all of your mom's teacups (foreach) VS You want to polish her ten most valuable teacups (for) VS You want to polish teacups until you run out of polish (while).
You would know beforehand when the for loop would terminate, this is not clear in a while loop. I basically tell my students "if you know when it ends, it's a for". (Sure, one can construct pathological cases and there is always this for (;;), but for basic understanding this issue is the crucial difference between for and while.)
As for examples:
- print all elements of this array is a
for, - play so many rounds of tic-tac-toe until the human player quits:
while.
You might want to force them to convert for to while in an exercise and demostrate that the converse is not always possible.
The first is the original for loop. You initialize a variable, set a terminating condition, and provide a state incrementing/decrementing counter (There are exceptions, but this is the classic)
For that,
for (int i=0;i<myString.length;i++) {
System.out.println(myString[i]);
}
is correct.
For Java 5 an alternative was proposed. Any thing that implements iterable can be supported. This is particularly nice in Collections. For example you can iterate the list like this
List<String> list = ....load up with stuff
for (String string : list) {
System.out.println(string);
}
instead of
for (int i=0; i<list.size();i++) {
System.out.println(list.get(i));
}
So it's just an alternative notation really. Any item that implements Iterable (i.e. can return an iterator) can be written that way.
What's happening behind the scenes is somethig like this: (more efficient, but I'm writing it explicitly)
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String string=it.next();
System.out.println(string);
}
In the end it's just syntactic sugar, but rather convenient.
There is an excellent summary of this feature in the article The For-Each Loop. It shows by example how using the for-each style can produce clearer code that is easier to read and write.