MATLAB Answers

prasanna
0

What is happening in this while loop

Asked by prasanna
on 28 Oct 2013
Latest activity Commented on by Image Analyst
on 28 Oct 2013
this is a peice of code written in matlab.
while(flag==1)
flag=0;
disp('end of loop');
end
surprisingly, this does not display the string and if i remove disp statement and figure1 window appears. can anyone explain why?

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Andreas J. on 28 Oct 2013
 Accepted Answer

flag = true
while flag
flag=0;
disp('end of loop');
end
see: doc flag

  1 Comment

I bet a lot of people don't know flag is the name of a built-in function - I didn't. So it's best to use some other name instead of flag, like keepLooping, or keepGoing, or something. And your point of initializing flag prior to entering the loop is really the key issue here (because you could actually overwrite the flag function with your own variable of the same name if you wanted to, though it's not advised).

Sign in to comment.