Duplicate Question
The question on this page has been marked as a duplicate question.
Original Question
I had a test in my computer science course in which I was asked if any while loop can be replaced with a for loop. I put true....Asked by Jhon
I had a test in my computer science course in which I was asked if any while loop can be replaced with a for loop. I put true. I guess the answer was false. My professor said that if you had a while loop in which you asked the user to input a certain value that had to be within a certain range and kept on iterating over the loop until the user inputted a value within the range that you wouldn't be able to do this with a for loop.
I however thought that any for loop can be written with a while loop and any while loop can be rewritten with a for loop. I think I may have even read something about this in my textbook but am unable to come up away with doing what my professor said with a for loop but believe that it's possible. Can anyone please come up with a way to do such a thing with a for loop?
for example a while loop
A=0;
while A<1
x=input('Enter a value');
if x>4 && x<10
A=1
else
end
this would force the user to enter a number between 4 and 10, not including 4 and 10, and would just keep on iterating over the loop until the user does.
I however thought that any for loop can be written with a while loop and any while loop can be rewritten with a for loop. I think I may have even read something about this in my textbook but am unable to come up away with doing what my professor said with a for loop but believe that it's possible. Can anyone please come up with a way to do such a thing with a for loop?
for example a while loop
A=0;
while A<1
x=input('Enter a value');
if x>4 && x<10
A=1
else
end
this would force the user to enter a number between 4 and 10, not including 4 and 10, and would just keep on iterating over the loop until the user does.
Answers
Answered by
MathMate
The while loop exits whenever a user-specified condition is reached, in this case when x≤4 or x≥10.
A for loops exits (normally) when the stated number of cycles is reached, i.e. not an arbitrary user condition.
There are different ways to "misuse" the for loop.
A common one is to create an infinite for-loop and make an independent exit condition, such as:
for(i=0;i<1000;i=i){ // this line actually compiles and runs in Java
// enter x
if(x<=4 || x>=10)break;
}
Another way to "force" a while loop capability in languages such as Java, C and C++ would be:
int x=5;
for(int i=0;x>4&&x<10;i++){ // bad style!
if(i>2)x=0; // or an input for x
System.out.println("printing");
}
But these are exceptional uses of the for-loop and are considered bad style, even the syntax is correct.
A for loops exits (normally) when the stated number of cycles is reached, i.e. not an arbitrary user condition.
There are different ways to "misuse" the for loop.
A common one is to create an infinite for-loop and make an independent exit condition, such as:
for(i=0;i<1000;i=i){ // this line actually compiles and runs in Java
// enter x
if(x<=4 || x>=10)break;
}
Another way to "force" a while loop capability in languages such as Java, C and C++ would be:
int x=5;
for(int i=0;x>4&&x<10;i++){ // bad style!
if(i>2)x=0; // or an input for x
System.out.println("printing");
}
But these are exceptional uses of the for-loop and are considered bad style, even the syntax is correct.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.