% example if statement if ( 0 ) fprintf('I''m in the first clause.\n'); else fprintf('I''m in the second clause.\n'); end % example elseif statement a = rand(); if ( a < 0.2 ) fprintf('a is less than 0.2\n'); elseif ( a <= 0.5 ) fprintf('a is between 0.2 and 0.5\n'); else fprintf('a is greater than 0.5\n'); end % example while loop iter = 0; while (1) iter = iter + 1; fprintf('This is iteration %d\n', iter); if ( rand() < 0.1 ) break; end end % example for loop for i = 1 : 3 fprintf('This is a simple for loop. iteration = %d\n'); end % another example for loop xvalues = [ 1 : 10 ]; for x = xvalues, fprintf('This is a new iteration\n'); x end % another example for loop % the for loop iterates over columns, not rows. xvalues = [ 1 : 10 ]'; for x = xvalues, fprintf('This for loop doesn''t do what I thought it would.\n'); x end