Computer Science MATLAB For and While loops

Click For Summary

Discussion Overview

The discussion revolves around the interchangeability of while loops and for loops in MATLAB, particularly whether any while loop can be replaced with a for loop. Participants explore the implications of this interchangeability in the context of user input and infinite loops.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant asserts that any while loop can be rewritten as a for loop, citing a statement from their textbook, but struggles to provide an example that meets this criterion in MATLAB.
  • Another participant notes that MATLAB's for loop behaves differently from those in other programming languages, emphasizing that it does not support infinite looping, which complicates the replacement of while loops with for loops.
  • A participant points out that while the statement about the interchangeability of loops may hold true in languages like C or Java, it does not apply to MATLAB or Fortran, where the for loop has more restrictions.
  • Some participants discuss the ability of while loops to iterate indefinitely, while suggesting that for loops can approach this behavior but are limited by MATLAB's implementation.
  • One participant proposes a method to rewrite a while loop using a for loop with a break statement, arguing that this can replicate the behavior of a while loop under certain conditions.
  • Another participant mentions that using a for loop with a large upper limit in MATLAB may result in a warning about the index being too large, indicating practical limitations in the implementation.

Areas of Agreement / Disagreement

Participants express differing views on whether all while loops can be rewritten as for loops in MATLAB. While some believe it is possible under certain conditions, others argue that the inherent differences in loop behavior in MATLAB create limitations that prevent such interchangeability.

Contextual Notes

Participants highlight the limitations of MATLAB's for loop regarding infinite iterations and the handling of user input, which may not align with the behavior of for loops in other programming languages.

GreenPrint
Messages
1,186
Reaction score
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:
Physics news on Phys.org
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.
 
GreenPrint said:
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.
 
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:
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
 
GreenPrint said:
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.
 

Similar threads

Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 32 ·
2
Replies
32
Views
5K