简体   繁体   中英

Conditional IF/ELSE Statement in Matlab

I was trying to make a simple statement with Matlab as follows:

if TF==1
  disp('One'), break
else continue
end
... ... ...
... ... ...

But even if TF is not 1, when I run the command, it doesn't CONTINUE to the rest of the script!! Any help would be appreciated-- Thanks

The continue statement has a very different meaning. Within a loop, like a for or while loop, continue instructs to skip the current round and continue with the next iteration in the loop. So if you remove continue , you will see the behavior that you are expecting. Here is an example:

for k = 1 : 10
  if k == 4
    % skip the calculation in the case where k is 4
    continue
  end
  area = k * k;
  disp(area);
end

When the loop iterates at k == 4 , the block calculating the area of the corresponding square is skipped. This particular example is not very practical.

However, imagine you have a list of ten file names, and you want to process each file in this loop " for k = 1 : 10 ". You will have to try and open each file, but then if you find out the file does not exist, an appropriate way to handle it would be to print a little warning and then continue to the next file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM