if-elseif condition

When the first if condition fails, we can use elseif to supply another if condition. Syntax :

if (condition)
    statements
    ...
    ...
elseif (condition)
    statements
    ...
    ...
else
    statements
    ...
    ...
end
(endif can also be used)

Example : 

MATLAB




% initializing the variable var
var = 50;
 
% check the if condition
if var < 50,
disp('The variable is less than 50');
 
% check the elseif condition
elseif var > 50,
disp('The variable is greater than 50');
 
% if both the above condition is false else
% statement will execute
else
disp('The variable is 50');
 
% end the if..elseif.. statements
end;


Output:

The variable is 50

Loops (For and While) and Control Statements in Octave

Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements. These structures are used to make a decision after assessing the variable. In this article, we’ll discuss control statements like the if statement, for and while loops with examples.

Similar Reads

if condition

This control structure checks the expression provided in parenthesis is true or not. If true, the execution of the statements continues. Syntax :...

if-else condition

...

if-elseif condition

It is similar to if condition but when the test expression in if condition fails, then statements in else condition are executed. Syntax :...

for loop

...

while loop

When the first if condition fails, we can use elseif to supply another if condition. Syntax :...

break statement

...

Contact Us