When would you use a while loop rather than a for loop and vice versa?
Bagi teman2 yang ingin tahu atau mau mengerjakan tugas kampus, berikut jawaban yang membedakan anatara While loop dan For loop di java, dikarenakan saya cuma copas, tolong diterjemahkan melalui mbah google atau mungkin yang jago bahasa inggris sok mangga di terjemahkan :
Answer:
The golden rule in iteration: everything done with a for loop can be done with a while loop, BUT not all while loops can be implemented with a for loop.
for(int x = [grab input]; x < 0; x = [grab input]){
// Do Code
}
The above for loop behaves exactly like the while loop in the previous heading. A better example of a while loop that should not be a for loop might be:
while(true){
for-loops are just a short-cut way for writing a while loop, while an initialization statement, control statement (when to stop), and a iteration statement (what to do with the controlling factor after each iteration).
Examples of for-loops
The most basic use for using for-loops is to do something a set number of times:
for(int k = 0; k < 10; k++); // this loops runs for 10 times
another less common use of the for-loop is traversing raw listNodes, since it does contain an initialization(finding the first node), control (as long as there is a next node), and a iteration statement (get my next node). i.e.:
for(ListNode temp = startingNode; temp != null; temp = temp.getNext); // this traverses the entire ListNode list and stops when it has exhausted the list
How to implement for-loops using while loop
Basically for loops are just short hand for while loops, any for loop can be converted from:
for([initialize]; [control statement]; [iteration]);
to
[initialize];
while([control statement]) { //Do something [iteration]; }
These two does the exact same thing.
For When Only while Loop can be used
while-loops are used when the exiting condition has nothing to do with the number of loops or a controll variable, maybe you just want to keep prompting the user for an input until the given input is valid, like the following example which demands a positive number:
int x = [grab input];
while(x < 0) {
// Do code
x = [grab input];
}
But For Loops Can Do Anything A While Loop Can!
It is true that, when used as intended, a for loop cannot do everything a while loop can, however, in reality, for loops are just as versatile. For example, the above while loop can easily be rewritten to be a for loop as so:
for(int x = [grab input]; x < 0; x = [grab input]){
// Do Code
}
The above for loop behaves exactly like the while loop in the previous heading. A better example of a while loop that should not be a for loop might be:
while(true){
// Do some processing
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
}
Here, the checking of the condition comes in the middle of the processing for the while loop, whereas the condition checked in a for loop is always done at the beginning of the loop. Also, the "iteration" statement is non-existant and is a factor of processing done somewhere else in the while loop. Finally, there was no initialization for this while loop. However, this while loop can still be written as a for loop:
for(;true;){
Here, the checking of the condition comes in the middle of the processing for the while loop, whereas the condition checked in a for loop is always done at the beginning of the loop. Also, the "iteration" statement is non-existant and is a factor of processing done somewhere else in the while loop. Finally, there was no initialization for this while loop. However, this while loop can still be written as a for loop:
for(;true;){
// Do some processing
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
// Check some condition. If condition is met, break out of loop.
// Do some more processing.
}
As you can see, a for loop is exactly like a while loop if you leave out the initialization and iteration sections (you still needs the semicolons, to signify those parts of the for loop are still there, they just do nothing). However, it is clear that when you do not need the extra portions of the for loop, why not just use a while loop?
As you can see, a for loop is exactly like a while loop if you leave out the initialization and iteration sections (you still needs the semicolons, to signify those parts of the for loop are still there, they just do nothing). However, it is clear that when you do not need the extra portions of the for loop, why not just use a while loop?
sumber :
