if

Logical decision-making if statement. Can be combined with 'and' 'or' operators to create complex logical tables.

Syntax

if (condition)
  statement
end
if (condition)
  statement
else
  statement
end
if (condition)
  statement
elseif (condition)
  statement
else
  statement
end

Inputs

condition
Anything that can be logically tested.
Type: double | char | logical | struct | cell | integer
Dimension: scalar | vector | matrix

Outputs

statement
Executed if the condition returns true.

Examples

Simple if example:
x= 4
if x == 4
  b=2
end
b

x = 4
b = 2
b = 2
Simple if-else example:
a=2
if (a==0)
  b=0
else
  b=1
end
b

a = 2
b = 1
b = 1
Simple elseif example:
a=1
if (a==3)
  b=4
elseif a ==2
  b=7
elseif a == 9
  b=12
else
  b=-a
end
b

a = 1
b = -1
b = -1
Complex elseif example:
a=1
x = 'hello'
if a==3 && x == 'world'
  b=4
elseif a==2 || x=='hello'
  b=7
    disp('Hit the second elseif')
elseif a==9
  b=12
else
  b=-a
end
b

a = 1
x = hello
b = 7
Hit the second elseif
b = 7