Computer Science MATLAB For and While loops

In summary, the author believes that any problem that can be solved using a while loop could also be solved using a for loop. However, this is false in some languages, such as MatLab.f
  • #1
1,196
0
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 found this in my book
"Any problem that can be solved using a while loop could also be solved using a for loop"
 
Last edited:
  • #2
With MatLab, the for loop behaves differently than in some other languages. While in some other languages, such as those based on C, the behavior of any while loop may also be implemented with a for loop, this is not so for MatLab. The MatLab for loop does not allow infinite looping; it supports executing code repeatedly with a finite number of index values. Any changes to the index variable of the for loop in the body of the loop would not affect the next iteration.
 
  • #3
I found this in my book
"Any problem that can be solved using a while loop could also be solved using a for loop"

Check what computer language(s) the book is talking about. That statement is certainly true in C or Java, but it's false in Fortran or Matlab.

In C or Java, the expressions in the "for" statement can be almost anything in the language (including reading input from the user, if you want to do that). In other languages, the only thing that a "for" statement can do is make an integer variable step through a sequence of values.
 
  • #4
The book is talking about MATLAB it is the book we are using for the course in which we learn MATLAB.

Can anyone provide me with a while loop that cannot be rewritten with a for loop?

I believe that there are two parts to this argument.

A. while loop you can iterate forever

It's true that you can't iterate for ever with a for loop, but you can get very close.
for k=1:Inf
...
end
will make MATLAB complete 9223372036854775807 iterations.

B. You can use a while loop when you don't know how many iterations are needed. Rewriting a while loop with a for loop that uses this aspect of the while loop proposes a problem

for k=1:inf
...
end

will complete 9223372036854775807 iterations no matter if only 5 iterations are needed or 100... but we can get around this by

for k=1:inf
...
if CONDITION
break
else
end

were if a condition is true we can terminate out of the loop and if not we can keep on iterating until the condition is met... this gets around this problem...

9223372036854775807 is an incredible amount of iterations. Can we really have a loop go on forever anyways? Wouldn't a infinite while loop eventually terminate because the computer running the program would eventually crash or break? Just because we can code an infinite while loop doesn't mean it would necessarily go on forever. If I ran an infinite while loop on my computer by no means would it go on forever for 1000+ years because my computer would break by then.
 
Last edited:
  • #5
I believe you can rewrite any while loop of form

while CONDITION
...
end

with

For k=1:Inf
...
if CONDITION
break
else
end

were the CONDITION is exactly the same for both loops

as
For k=1:Inf
...
if CONDITION
break
else
end
well do exactly the same thing as a while loop - iterate as many as times as needed until a condition is met... once it's met terminate from the loop... as I've shown this can be done with a for loop
 
  • #6
I believe you can rewrite any while loop of form

while CONDITION
...
end

with

For k=1:Inf
...
if CONDITION
break
else
end

were the CONDITION is exactly the same for both loops

If you ran MatLab code with "for k=1:inf" you would get a warning message indicating a for loop index is too large, and has been truncated to a specific integer.
 

Suggested for: Computer Science MATLAB For and While loops

Replies
3
Views
172
Replies
3
Views
651
Replies
15
Views
810
Replies
5
Views
828
Replies
1
Views
512
Replies
18
Views
4K
Replies
1
Views
460
Replies
3
Views
721
Replies
3
Views
533
Back
Top