if

Programming

Conditional execution. Executes the first block whose condition is true (nonzero). Optional elseif and else clauses provide alternative paths.

Syntax

if condition ... end
if condition ... else ... end
if condition ... elseif condition ... else ... end

Examples

x = 5;
if x > 0
  disp("positive")
end
▶ Run
x = -3;
if x > 0
  disp("positive")
elseif x == 0
  disp("zero")
else
  disp("negative")
end
▶ Run

See Also

Try SimLab — Free MATLAB® Alternative

466 functions. Runs in your browser. No install.

Open SimLab

Stay Updated

Get notified about new simulations and tools. We send 1-2 emails per month.