フィルターのクリア

bypass an "if" within another using a flag

1 回表示 (過去 30 日間)
Mehrdad Bahadori
Mehrdad Bahadori 2023 年 4 月 8 日
編集済み: Image Analyst 2023 年 4 月 8 日
Hi everyone,
I have the code below:
a=5; b=4; c=3; d=2;
if a>b
if b>c % ====> this
if c>d
disp('i''m in!')
end
end % ======> this
end
Is it possible I bypass the " if b>c" using a flag?
e.g. FLAG=1, include this if, and FLAG=0, bypass this if.
Thanks!

採用された回答

Walter Roberson
Walter Roberson 2023 年 4 月 8 日
You might mean either of two different things:
First possibility:
a=5; b=4; c=3; d=2;
if a>b
if FLAG == 1 && b>c
if c>d
disp('i''m in!')
end
end % ======> this
end
In the above case, if b>c is only tested if FLAG is 1 -- but c>d is not tested if FLAG is not 1.
Second possibility:
a=5; b=4; c=3; d=2;
if a>b
if FLAG ~= 1 || b>c
if c>d
disp('i''m in!')
end
end % ======> this
end
In this case, the b>c test is only done if FLAG == 1, and if FLAG is not 1, then you go on to c>d anyhow.
  2 件のコメント
Mehrdad Bahadori
Mehrdad Bahadori 2023 年 4 月 8 日
the second possibility is exactly what I was saying. thanks!
Image Analyst
Image Analyst 2023 年 4 月 8 日
編集済み: Image Analyst 2023 年 4 月 8 日
Unless you've left out a lot of code (especially after the b>c block, there is no reason to even enter the a>b block. You could simply do
if a>b && FLAG
if b>c
if c>d
disp('i''m in!')
end
end % ======> No more code after this block
end
Of course the b>c and c>d could even be combined into a single test sine the c>d is the only thing in the block (again, assuming you have not left out additional code):
if a>b && FLAG
if b>c && c>d
disp('i''m in!')
end % ======> No more code after this block
end

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2023 年 4 月 8 日
Since the b>c test occurs only inside the a>b block, and it's the only thing in the a>b block, you could simply do
if a>b && b>c
if c>d
disp('i''m in!')
end
end
  1 件のコメント
Mehrdad Bahadori
Mehrdad Bahadori 2023 年 4 月 8 日
編集済み: Walter Roberson 2023 年 4 月 8 日
thanks for your response.
In the way you are saying, if b>c, it goes to next loop and shows the disp message. What I want is, to bypass the b>c, and go to the next if, and the go to disp.
with a flag , I want to change the code that I posted to the one below functionally:
(e.g. if FLAG==1)
a=5; b=4; c=3; d=2;
if a>b
% if b>c
if c>d
disp('i''m in!')
end
% end
end
Is it possible to do so?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by