nippon
nippon

Reputation: 123

Initialize for-loop within if-statement

Is it possible to have the start of a for-loop within an if-statement. For example:

if condition true 

    for j=1:10 

else % condition false

    for j=11:20

end % End of if-statement

    % inner part of for-loop

end % End of for-loop

I know this is not the most beautiful approach, but in some cases it might be easier than simple coding.

Upvotes: 1

Views: 53

Answers (1)

glglgl
glglgl

Reputation: 91017

No, that doesn't work.

But you can do

if condition
    range=1:10
else % condition false
    range=11:20
end % End of if-statement

for j=range
    % inner part of for-loop
end % End of for-loop

Upvotes: 6

Related Questions