After reading this MATLAB conditional statements topic, you will understand conditional statements classification and you will know the theory, and examples of MATLAB Conditional statements.
The conditional statements execute different statements based on different conditions.
MATLAB – if statement
General Form:
if expression(1) statements end
The if statement executes statements as expression(1) evaluates to the true condition.
Example:
Program (1): To check enter number is even or odd.
a=input('enter a = '); if rem(a, 2) == 0 disp('a is even') end
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
enter a = 30 a is even
MATLAB VIEW – Output (1):
MATLAB – if-else statement
General Form:
if expression(1) statements else statements end
- The if-else statement executes if statements when expression(1) evaluates to true condition and skip else statements.
- The if-else statement executes else statements when expression(1) evaluates to false condition and skip if statements.
Example:
Program (1): To check the number, enter by the user is even or odd.
a=input('enter a = '); if rem(a, 2) == 0 disp('a is even') else disp('a is odd') end
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
enter a = 20 a is even
MATLAB VIEW – Output (1):
MATLAB – if-else-if statement
General Form:
if expression(1) statements elseif expression(2) statements else statements end
- The if-else-if statement executes if statements when expression(1) evaluates to true condition and skip else and else-if statements.
- The if-else-if statement executes else-if statements when expression(2) evaluates to true condition and skip if and else statements.
- The if-else-if statement executes else statements when expression(1) and expression(2) both evaluate to false condition and skip if and else-if statements.
Example:
Program (1): To check the number, enter by the user is positive, negative or zero.
a=input('enter a = '); if a>0 disp('a is positive') elseif a<0 disp('a is negative') else disp('a is zero') end
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
enter a = 20 a is positive
MATLAB VIEW – Output (1):
MATLAB – switch statement
General Form:
switch (scalar or string) case value(1) statements case value(2) statements otherwise statements end
To switch among several cases, based on the expression, and the expression can be scalar or string.
- The switch statement executes case value(1) statements when scalar or string match to value(1) and skip remaining case statements.
- The switch statement executes case value(2) statements when scalar or string match to value(2) and skip remaining case statements.
- The switch statement executes otherwise statements when scalar or string do not match with both the value(1) and value(2) & skip both case statements.
Example
Program (1): To enter a choice from the user for playing one game out of Cricket, Football, and Hockey.
n=input('enter your choice,1 for playing Cricket, 2 for playing Football, 3 for playing Hockey : '); switch n case{1} disp('you will play Cricket') case{2} disp('you will play Football') case{3} disp('you will play Hockey') otherwise disp('enter choice is not valid') end
MATLAB VIEW – Program (1):
Create a script file in MATLAB and type the following code –
Output (1):
enter your choice,1 for playing Cricket, 2 for playing Football, 3 for playing Hockey : 2 you will play Football