indicated statement will not run

1 view (last 30 days)
Andrew Tubbs
Andrew Tubbs on 27 Jan 2021
Answered: Walter Roberson on 27 Jan 2021
I have run into an issue where my program for some reason cannot run an if statement, my code is posted below and the statement that will not run is 'if o == 2;' (arrowed to) The code is up to over 2000 permeatations so I can't give the array in use. If anyone can tell why it would be appreciated.
a = 1;
b = 1;
c = 1;
d = 1;
e = 1;
f = 1;
v = 1;
w = 1;
x = 1;
o = 0;
if true
if b <= length(B);
while b <= length(B);
V{v} = A{a} + B{b};
b = b + 1;
v = v + 1;
end
end
while true
if b > length(B);
b = 1;
a = a + 1;
if a <= length(A);
while b <= length(B);
V{v} = A{a} + B{b};
b = b + 1;
v = v + 1;
end
else
o = o + 1;
end
end
if o == 1;
if d <= length(D);
while d <= length(D);
W{w} = C{c} + D{d};
d = d + 1;
w = w + 1;
end
end
while true
if d > length(D);
d = 1;
c = c + 1;
if c <= length(C);
while d <= length(D);
W{w} = C{c} + D{d};
d = d + 1;
w = w + 1;
end
else
o = o + 1
end
end
end
end
if o == 2; %<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if f <= length(F)
while f <= length(F)
X{x} = E{e} + F{f};
f = f + 1;
x = x + 1;
end
end
while true
if f > length(F)
f = 1;
e = e + 1;
if e <= length(E)
while f <= length(F)
X{x} = E{e} + F{f};
f = f + 1;
x = x + 1;
end
else
o = o + 1;
return
end
end
end
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 27 Jan 2021
if o == 1;
if d <= length(D);
while d <= length(D);
W{w} = C{c} + D{d};
d = d + 1;
w = w + 1;
end
end
When you get out of that, d > length(D), either because d <= length(D) was originally false, or because the while loop incremented d until it became > length(D)
while true
if d > length(D);
The first time entering that loop, d > length(D) for the reasons described above
d = 1;
c = c + 1;
Suppose you just incremented c to be > length(C )
if c <= length(C);
so that is false
while d <= length(D);
so that is not done
W{w} = C{c} + D{d};
d = d + 1;
w = w + 1;
end
else
the else branch is taken because c > length(C)
o = o + 1
so o is incremented
end
end
end
You are inside a while true so you do the loop again. You assigned 1 to d the last time through, so d > length(D) is false, so you fall through to the bottom of the if and the end of the while true is immediately after that so nothing is done before starting the while true again. But d > length(D) is still false, so you do nothing before starting the while true again...

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!