Using if statements correctly

4 ビュー (過去 30 日間)
Janna Hinchliff
Janna Hinchliff 2019 年 2 月 13 日
編集済み: madhan ravi 2019 年 2 月 13 日
I am using an if statement to determine whether entries in matrices are non-zero. If I write the code
if matrixarray(1,1)~=0
% do some operation
elseif
matrixarray(2,1)~=0
% do some other operation
end
I realise that there is the possibility that the first matrix element might be non-zero and the second might also be non-zero. If this happens, will the code just run the first part of the if statement as this is what it sees first or will it run both conditions?

採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 2 月 13 日
編集済み: madhan ravi 2019 年 2 月 13 日
If this happens, will the code just run the first part of the if statement as this is what it sees first or will it run both conditions?
Yes code just pass the next if else condtion.
It runs the one condition only, it executes the first true condition.
More understanding
a=6;
if a>3
disp('hello1');
elseif a>4
disp('hello2');
end
Output:
hello1
>>

その他の回答 (0 件)

カテゴリ

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