Loop Analysis in MATLAB: What is the Code Doing?

Click For Summary

Discussion Overview

The discussion revolves around understanding a specific MATLAB code snippet that utilizes a while loop and a for loop. Participants are attempting to decipher the behavior of the code, particularly how the variable A changes throughout the execution and the implications of the loop structure.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the operation of the while loop and its condition, suggesting that it may run infinitely.
  • Another participant argues that the code is nonsensical and can be simplified, claiming that the for loop effectively results in a constant subtraction from A, leading to an infinite loop unless interrupted.
  • A later reply provides a step-by-step breakdown of the code execution, detailing how the value of A changes with each iteration and ultimately results in A equaling 4.
  • There is mention of a potential compile error due to the variable k being undefined outside the for loop, although this is not universally accepted as a definitive issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the validity or functionality of the code. Some view it as flawed or nonsensical, while others attempt to clarify its operation through detailed analysis.

Contextual Notes

There are unresolved questions regarding the behavior of the code, particularly concerning the definition and scope of variables within the loops, as well as the implications of the while loop's condition.

gfd43tg
Gold Member
Messages
949
Reaction score
48
Hello,

I am trying to decipher loop codes and I am having a lot of difficulty doing so

Here is an example code
Code:
A = 0;
while A < 1
    for k=1:5
        A = A+(-1)^k;
    end
    A = A+k;
end

So I thought the while function does something an infinite amount of times. This is what I think this code is doing

It starts with A = 0, (not sure what the while A < 1 is doing), then the for statement does this

A = 0 + (-1)^1 = -1
A = -1 + 1 = 0

for k = 1, then it must repeat this statement (k=2). The A = 0 in this line is not from the first line of code, it comes from the result for k=1,
A = 0 + (-1)^2 = 1
A = 1 + 2 = 3

well I guess somewhere here the A < 1 is violated, thus the code should stop? I run it and I get 4. I don't understand how this code is working.
 
Physics news on Phys.org
This code is pretty much nonsensical and can be simplified:

the for k=1:5 loop

A = A -1 + 1 -1 +1 -1

which basically subtracts one from A which means the outer while loop will run forever except

the statement A = A + k is outside the loop and so k is undefined at this point ie you should get a compile error

Where did you get this code?
 
It was an example code problem in the lesson
 
Maylis said:
Hello,

I am trying to decipher loop codes and I am having a lot of difficulty doing so

Here is an example code
Code:
A = 0;
while A < 1
    for k=1:5
        A = A+(-1)^k;
    end
    A = A+k;
end

<snipped>

I run it and I get 4. I don't understand how this code is working.
Step through it line by line and you can see how it gets a result of 4.

1. A = 0.
2. while A<1 is true, so continue.
3. Enter the for loop, and start with k=1.
4. A =-1
5. now k=2.
6. A = 0
7. now k=3.
8. A = -1
9. now k=4.
10. A = 0
11. now k=5, the last time through the for loop.
12. A =-1.
13. A = A+k, so A = -1+5 = 4.
14. Check if A<1. It evaluates to false so exit the while loop.
15. Return the answer of 4.

Basically, a while loop executes as long as the condition is true. In this case, it executes as long as A<1 is true.

A for loop executes for each specified value. So for k=1:5 means the loop executes for each separate value of k. Notice that the A=A+k line doesn't execute until the for loop is completely done.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
0
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K