www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ConditionalCodeExecutionExample.m

    %% Conditional Code Execution
% Execute code based on a condition using the logical not operator in the
% context of an |if| loop.
%
% Create a logical variable |A|.
A = false;

%%
% Use |A| to write an if/else code block. Wrap the if/else block in
% a |for| loop so that it executes four times.
for k = 1:4
    if ~A
        disp('IF block')
        A = true;
    else
        disp('ELSE block')
    end
end

%%
% On the first iteration, |A| is |false|, so the |if| block executes since
% |~A| is |true|. However, the |if| block also changes the value of |A| to
% |true|. In the remaining iterations, |~A| is |false| and the |else| block
% executes.