gudii9
asked on
while loop over for loop
Hi,
I see most of the time real world practical examples use for loop. I wonder where using while loop is advantageous or preferred over for loop. please advise
I see most of the time real world practical examples use for loop. I wonder where using while loop is advantageous or preferred over for loop. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> you know the number of iterations in advance.
>> finite number of iterations to make, use a 'for' loop
Agree with both experts (although I think they are both saying the same thing).
A case where a where-loop is used is where the program is receiving input, say, from a file, or messages from the internet or another program, or from a user input that is prompted for input, and the program doesn't know in advance about how much data will be provided.
In case of a file, you could test for an End Of File mark (EOF). The program where-loop might look like:
You may have already worked with the latter case where you are in a loop that prompts for input data, say student name and grade. By entering a special string for the student name, say, "COMPLETE", or "0", then your program would set done = true, and the while (done) loops completes.
>> finite number of iterations to make, use a 'for' loop
Agree with both experts (although I think they are both saying the same thing).
A case where a where-loop is used is where the program is receiving input, say, from a file, or messages from the internet or another program, or from a user input that is prompted for input, and the program doesn't know in advance about how much data will be provided.
In case of a file, you could test for an End Of File mark (EOF). The program where-loop might look like:
where (number of bytes read is NOT EOF) // there are more details to this, but this is the gist
In the case of messages from the internet or another program, there may be a message that says "no more messages" - i.e., you have now everything you need, so set done = true, and the while (done) loops completes.You may have already worked with the latter case where you are in a loop that prompts for input data, say student name and grade. By entering a special string for the student name, say, "COMPLETE", or "0", then your program would set done = true, and the while (done) loops completes.
This would be more cumbersome for instance if it were attempted using a 'for' loop :
import java.util.Scanner;
class Mod {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Number please . . . ");
int num = scanner.nextInt();
System.out.println("Modulus please . . . ");
int mod = scanner.nextInt();
int orgnum = num;
while(num - mod >= 0){num-=mod;}
System.out.println("Your modulo is "+num);
System.out.println(orgnum +" divided by "+mod+" is "+orgnum/mod);
}
}
ASKER
i see below link...
stackoverflow.com/question s/6710601/ when-to-us e-while-lo op-rather- than-for-l oop
so to summarize flexibility, readability and whether we know ahead no. of iterations
stackoverflow.com/question
so to summarize flexibility, readability and whether we know ahead no. of iterations
That is certainly often true. Often true for while loops as well.
One area where arrays are not involved, and which can be used by a for loop or a while loop is related to mathematical algorithms that converge to an answer within some tolerance. For example, if you are ascending to a mountain top whose topography is digitized, then how do you know when you have reached the summit? The loop might look like:
Open in new window
In this case the test condition is fairly simple, and some prefer a for-loop even though there is no counter involved. For these cases, there has to be analysis performed to prove that we will not have an infinite loop for the kinds of data sets that we are dealing with. This analysis has to be done whether using a for-loop or a while-loop.If this were a while loop, we could have
Open in new window