Hello,(adsbygoogle = window.adsbygoogle || []).push({});

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

Here is an example code

So I thought the while function does something an infinite amount of times. This is what I think this code is doingCode (Text):A = 0;

while A < 1

for k=1:5

A = A+(-1)^k;

end

A = A+k;

end

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 Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Loops Analysis (MATLAB)

Tags:

Loading...

Similar Threads for Loops Analysis MATLAB |
---|

Mathematica Looping through vectorized functions for a piecewise solution |

Mathematica While Loop in Mathematica |

Excel Error Bars |

MATLAB Weighting data points with fitted curve in Matlab |

**Physics Forums | Science Articles, Homework Help, Discussion**