[TOC]

If-else Statement

An if-else statement chooses between multiple sets of statements to execute, based on the logical value returned by a given statement. The following example shows the simplest form of an if-else statement:

i = 5;
if i > 4
	disp('Greater than 4')
end

In this example, it checks whether i > 4 is true. It prints a message if it is true, and does nothing if false. An if-statement starts with the keyword if and ends with the keyword end. Statements enclosed by them will be executed if the condition is true.

If another set of statements needs to be executed when the condition is false, the else keyword is used:

i = 3;
if i > 4
	disp('Greater than 4')
else
	disp('Less than or equal to 4')
end

In the above example, statements enclosed by else and end will be run when the condition i > 4 evaluates to false. Hence, “Less than or equal to 4” will be printed.

If multiple conditions need to be checked one after another, we use the keyword elseif. One can use as many elseif as necessary. For example, the following step-wise function

can be implemented by the following if statement:

t = 0.15
if t <= 0
	a = 0;
elseif t <= 0.1
	a = 1;
elseif t <= 0.2
	a = 2;
else
	a = 3;
end

In this example, the conditions are checked successively, starting from the top one:

Note that t > 0 must be true when the second condition is checked. Therefore, it is not necessary to include t > 0 in the second condition. For the same reason, t > 0.1 is not necessary in the third condition.

Syntax

An if-else statement has the following syntax:

if condition_0
	statement_list_0
	
elseif condition_1
	statement_list_1
	
elseif condition_2
	statement_list_2
	
elseif ...
	...
	
elseif condition_n
	statement_list_n
	
else
	last_statement_list
end 

Each of the conditions condition_0, condition_1, ... should evaluate to a logical value. When a logical value is true, the corresponding statement list will be executed. The elesif and else groups are not mandatory, which can be omitted.

One-line Form

The syntax shown above is the most commonly used form. In fact, an if-else statement can be written in one single line by inserting separators between different components of the if-else statement. A separator can be a comma, a semicolon, or a newline character. In the general form above, the separators are newline characters. In the following example, the separators are commas:

Input
i = 1;
if i < 10, 100, else, -100, end
Output
ans = 
 100.00

The following example uses both semicolons and commas as separators:

Input
i = 1;
if i < 10; 100, else; -100, end
Output
ans = 
 100.00

⚠️⚠️⚠️ The end keyword should be followed by a newline character. This is different from MATLAB in which the end can be followed by other separators.

Example

Suppose you have $200 at the beginning of a game. In this game, you flip a fair coin 50 times. Every time when the head is up, you lose ​$5; otherwise you win $5. At the end of the game, how much money do you have?

Input
money = 200;
for i = 1:50
    if rand > 0.5
        money = money - 5;
    else
        money=money + 5;
    end
end
money 
Output
money = 
 210.00