简体   繁体   English

Matlab中的条件IF / ELSE语句

[英]Conditional IF/ELSE Statement in Matlab

I was trying to make a simple statement with Matlab as follows: 我试图用Matlab做一个简单的声明,如下所示:

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!! 但是,即使TF不为1,当我运行命令时,它也不会继续执行脚本的其余部分! Any help would be appreciated-- Thanks 任何帮助将不胜感激-谢谢

The continue statement has a very different meaning. continue语句的含义非常不同。 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. 在一个循环中,例如forwhile循环, continue指示跳过当前回合并继续循环中的下一个迭代。 So if you remove continue , you will see the behavior that you are expecting. 因此,如果删除continue ,您将看到预期的行为。 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. 当循环在k == 4处迭代时,将跳过计算相应正方形区域的块。 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 ". 但是,假设您有一个包含十个文件名的列表,并且您想在此循环中“ 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. 您将不得不尝试打开每个文件,但是如果您发现该文件不存在,则处理该文件的适当方法是打印一些警告,然后continue下一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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