0
$\begingroup$

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. If I remove the disp statement a figure1 window appears. Can anyone explain why?

$\endgroup$

closed as off-topic by Bill Barth, Brian Borchers, Godric Seer, Stefano M, Christian Clason Oct 29 '13 at 7:36

This question appears to be off-topic. The users who voted to close gave these specific reasons:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ Since this question is more about programming than computational science, I would suggest moving it to stack overflow. $\endgroup$ – Dr_Sam Oct 28 '13 at 12:33
5
$\begingroup$

That is simply because flag is a defined command in matlab. Try typing flag just after having started Matlab and you will see a non-empty matrix appearing. If you type help flag you will see the meaning of that function.

Also, the function is selected by Matlab because you do not declare a variable called flag before your code. If you write flag=1 before your code, it works as expected.

Still, the best way is to change the name of your variable in order not to hide the Matlab function. So, use the following code:

myflag = 1;
while( myflag == 1)
    myflag = 0;
    disp('end of loop');
end
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.