Matlab

What is the Difference Between if-else and else if in MATLAB

In MATLAB, conditional statements enable the users to write the programs that make decisions. The conditional statement consists of one or more than one if-else or else-if statement. The end of the conditional statements is denoted by the end keyword. If the first condition is true, then statements within the if block are executed otherwise the control is sent to the else statements. If multiple conditions are used in the code, then else-if statements are used in MATLAB.

In the following guide, we have discussed the if-else and else-if statements in MATLAB.

What is if-else in MATLAB?

In MATLAB, the if-else statement is a way to make decisions in your program and it tests the given condition and executes different blocks of code based on results. If the specific condition is true, the statements in the if block are executed, and if the condition is false, the control is sent to the else block and the statements within the else block are executed.

Syntax

The following is the format for using the if-else statement in MATLAB:

if (condition)
Statement
else
Statement
end

 

Example

In the following example, we have checked the random number whether it is even or odd, between 1 and 100 using the if-else statements.

a = randi(100,1);
if rem(a,2) == 0
disp('The number is even')
else
disp('The number is odd')
end

 

What is else-if in MATLAB?

If you have multiple conditions to verify, you can use the else-if statement in MATLAB. In these statements, multiple conditions can be verified. If the given first condition is true, the statements in the if block will be executed terminating the other statements and If the if condition is false, the else-if block will be executed and the code will execute the multiple else-if conditions one by one until the conditions inside any else-if block will not be satisfied. If any else-if condition is satisfied, the program will execute that block.

Syntax

The basic format for using the else-if statement in MATLAB is as follows:

if condition 1
Statement 1
else if condition 2
Statement 2
else if condition 3
Statement 3
else
Statement 4
end

 

Example

In the following example of else-if in MATLAB, we have taken two numbers from the user. We then applied the three conditions in else-if statements:

number1 = input('Enter number 1: ');
number2 = input('Enter number 2: ');
if (number1 > number2)
disp('number1 is greater than number2')
else if (number1 < number2)
disp('number1 is less than number2')
else if (number1 == number2)
disp('number1 is equal to number2')
end
end
end

 

What is the Difference Between if-else and else-if in MATLAB?

The following is the difference between if-else and else-if statements in MATLAB:

if-else Statement else-if Statement
It is used when there is only one condition to be evaluated. It is used when there are multiple conditions to be checked in the code.
Execute the if block of code if the statement is true otherwise the control is sent to else block. Conditions are verified in order, and only the next condition is checked if the previous is false.

 

Bottom Line

The if-else and else-if are powerful conditional statements in MATLAB, used for making decisions and executing different blocks of code based on specific conditions. The if-else in MATLAB is useful when there is a single condition that needs to be checked and the else-if is used when multiple conditions are used in the code.

About the author

Zainab Rehman

I'm an author by profession. My interest in the internet world motivates me to write for Linux Hint and I'm here to share my knowledge with others.