When does loop end?

10 views (last 30 days)
Mizo Amreya
Mizo Amreya on 17 Aug 2020
Commented: Mizo Amreya on 17 Aug 2020
Hi, sorry for making it a little long but I wanted to ask a clear specific question.
I'm working on this flow dynamics problem and I'm getting different results using my approach than the expected results.
I spent the last few days trouble shooting the problem and I believe the issues is coming from a For Loop nested with IF statements.
Here's a sketch from my 2-D grid where I'm writing code to try to determine boundary conditions for flow between the grids.
My issues has been with the diagonal cells, namely: A,C,G & I.
The IF statements share some conditions with the B,D,F& H as you can see in my code below
Here's my code:
Imax = 15;
Jmax = 15;
for i=1:Imax*Jmax
if i<=Imax || mod(i,Imax)==1 % No-flow boundary conditions
lamdaWA(i)=0;
lamdaTA(i)=0;
else
if P(i,t)>=P(i-Imax,t)
lamdaWA(i)=lamdaW(i,t);
lamdaTA(i)=lamdaT(i,t);
else
lamdaWA(i)=lamdaW(i-Imax-1,t);
lamdaTA(i)=lamdaT(i-Imax-1,t);
end
end
if i<=Imax
lamdaWB(i)=0;
lamdaTB(i)=0;
else
if P(i,t)>=P(i-Imax,t)
lamdaWB(i)=lamdaW(i,t);
lamdaTB(i)=lamdaT(i,t);
else
lamdaWB(i)=lamdaW(i-Imax,t);
lamdaTB(i)=lamdaT(i-Imax,t);
end
end
if i<=Imax || mod(i,Imax)==0
lamdaWC(i)=0;
lamdaTC(i)=0;
else
if P(i,t)>=P(i-Imax,t)
lamdaWC(i)=lamdaW(i,t);
lamdaTC(i)=lamdaT(i,t);
else
lamdaWC(i)=lamdaW(i-Imax+1,t);
lamdaTC(i)=lamdaT(i-Imax+1,t);
end
end
if mod(i,Imax)==1
lamdaWD(i)=0;
lamdaTD(i)=0;
else
if P(i,t)>=P(i-1,t)
lamdaWD(i)=lamdaW(i,t);
lamdaTD(i)=lamdaT(i,t);
else
lamdaWD(i)=lamdaW(i-1,t);
lamdaTD(i)=lamdaT(i-1,t);
end
end
if mod(i,Imax)==0
lamdaWF(i)=0;
lamdaTF(i)=0;
else
if P(i,t)>=P(i+1,t)
lamdaWF(i)=lamdaW(i,t);
lamdaTF(i)=lamdaT(i,t);
else
lamdaWF(i)=lamdaW(i+1,t);
lamdaTF(i)=lamdaT(i+1,t);
end
end
if i>(Imax*Jmax)-Imax || mod(i,Imax)==1
lamdaWG(i)=0;
lamdaTG(i)=0;
else
if P(i,t)>=P(i+Imax,t)
lamdaWG(i)=lamdaW(i,t);
lamdaTG(i)=lamdaT(i,t);
else
lamdaWG(i)=lamdaW(i+Imax-1,t);
lamdaTG(i)=lamdaT(i+Imax-1,t);
end
end
if i>(Imax*Jmax)-Imax
lamdaWH(i)=0;
lamdaTH(i)=0;
else
if P(i,t)>=P(i+Imax,t)
lamdaWH(i)=lamdaW(i,t);
lamdaTH(i)=lamdaT(i,t);
else
lamdaWH(i)=lamdaW(i+Imax,t);
lamdaTH(i)=lamdaT(i+Imax,t);
end
end
if i>(Imax*Jmax)-Imax || mod(i,Imax)==0
lamdaWI(i)=0;
lamdaTI(i)=0;
else
if P(i,t)>=P(i+Imax,t)
lamdaWI(i)=lamdaW(i,t);
lamdaTI(i)=lamdaT(i,t);
else
lamdaWI(i)=lamdaW(i+Imax+1,t);
lamdaTI(i)=lamdaT(i+Imax+1,t);
end
end
end
My specific question is: When does the For loop jump to the next item? That means from i=1 to 1=2?
For example, when i = 5, does the code read the first 3 IF statements for A,B,C or does it just read the first one, jump to the end of the FOR loop and repeat for i=6?

Accepted Answer

Dana
Dana on 17 Aug 2020
Unless you have a continue statement somewhere (which you don't), everything enclosed between the initial for statement and the end statement that encloses the loop is run before advancing to the next index i. What makes you think MATLAB would stop after reading only the first 3 if statements?
  3 Comments
Dana
Dana on 17 Aug 2020
As I said, unless you tell it to do otherwise, MATLAB reads everything inside the loop on each iteration, exactly as if that code wasn't contained in a loop at all. It is definitely not skipping any IF statements.
So you have 8 root-level IF statements inside that loop, each of which also contains a nested IF statement in the else block. All 8 of the root-level IF statements will be executed on every iteration of this loop. The 8 nested IF statements may or may not be executed on a given iteration, depending on whether the corresponding parent IF statement evaluates to a false.
If you're getting some unexpected result, I can assure you it's not because MATLAB is skipping anything.
Mizo Amreya
Mizo Amreya on 17 Aug 2020
Okay, thanks for explaining. I will dig into the problem further.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!