フィルターのクリア

And function in While loop

1 回表示 (過去 30 日間)
zhe li
zhe li 2011 年 12 月 19 日
I am writing a code using while loop. I would like to use AND function in the condition,however, only the first part(before AND function) condition has been taken into calculation, I don't know how to get the second part(after AND sign "&&") involved into the calculation. The simple example would be while 1*a+2*b<100&&1*a<30 ... ... ... a=a+1 end
Thanks in advance
  1 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 12 月 19 日
Your example is not clear. Can you clarify it please (and use code formatting.)

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

回答 (3 件)

Jan
Jan 2011 年 12 月 19 日
The && and the & operators do a short-circuiting in IF and WHILE conditions. To avoid the short circuting and force both expressions top be evaluated, use the and() function.
Examples: This does not print "i = 10".
i = 0;
while i<10 & fprintf('i = %d\n', i)
i = i + 1;
end
This does print "i = 10":
i = 0;
while fprintf('i = %d\n', i) & i<10
i = i + 1;
end
or:
while and(i<10, fprintf('i = %d\n', i))
But please consider that using side-effects in IF or WHILE conditions is a bad programming habit. It is prone to mistakes and hard to debug. If you really have a good reason for short-circuting, add a comment:
while i<10 & fprintf('i = %d\n', i) % Short-circuit!

Daniel Shub
Daniel Shub 2011 年 12 月 19 日
If you use & instead of && both parts will be evaluated, even if the first part is false. Although this seems like a waste of time ...
clear x y
x = 10;
x < 5 && y < 5
This works, but this does not
x < 5 & y < 5
since y is undefined. If you define y, then it is fine
y = 10
x < 5 & y < 5
  1 件のコメント
zhe li
zhe li 2011 年 12 月 19 日
Thanks for your response. However,even though I did change it to &, still only the first part has been evaluated.Matlab seems to ignore the second part, but, I would consider the second part is part of a constrain in the code.

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


Nirmal Gunaseelan
Nirmal Gunaseelan 2011 年 12 月 19 日
As is the case with any programming language, MATLAB evaluates the second operand of an AND operation only when the first operand is TRUE. This is because if the first operand evaluates to a FALSE, there is no need to evaluate the second operand because the final result is already FALSE due to AND semantics.
Considering there is no variable called noVar in the workspace,
>> if (1>2 && noVar)
end
>> if (1<2 && noVar)
end
Undefined function or variable 'noVar'.
  3 件のコメント
zhe li
zhe li 2011 年 12 月 19 日
Thanks for your response. I not not too sure if I fully understand what you mean. I would consider both first part and second part are constrains for the calculation,however, the Matlab only evaluated the first part and left out the second part. The first part is most likely to be TRUE,hence I am hoping the second part can be evaluated and eliminate the iteration.
Jan
Jan 2011 年 12 月 19 日
@Daniel: Inside a IF-condition, the & operator does short circuiting. Try this:
if 1>2 & asdasdasd, disp(8), else, disp(9); end
The & operator behaves differently when used inside or outside an IF or WHILE condition. This is a backward compatibility issue to Matlab 6.

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

カテゴリ

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