Do Boolean operators shortcut in MATLAB?

조회 수: 6 (최근 30일)
Doug Hull
Doug Hull 2011년 1월 18일
(true or whatever) is always true, so whatever never needs to evaluate. Does it?

채택된 답변

Doug Hull
Doug Hull 2011년 1월 18일
In many programming languages, boolean operators like AND and OR will stop evaluating as soon as the result is known. For instance,
1 | error('Short-circuit')
would never get to the error part, since the 1 is always true.
MATLAB versions >= 6.5 include the new short-circuiting logical operators | | and &&. Use these for all condition tests in loops and similar, and use the old | and & for element-by-element logical operations. You can find details here.
In older versions of MATLAB, the boolean operators | and & are only short-circuit evaluated inside the conditions of IF and WHILE statements. In all other contexts, all parts of the conditional are evaluated.
[From the MATLAB FAQ of Ancient Times]

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by