Categories

If Statement

In this article, we will learn about if statement in Matlab. Conditional statements are used to check whether a given condition is true or false and execute the statements accordingly. They are used in many programming languages to execute a line or a code block. If statement is one of the simplest conditional statements. They evaluate an expression or condition and execute the code or statements if the condition is true. If a statement is generally followed by else statement of else if statement in the program.

Working of if Statement in Matlab with Examples

If statement is a conditional statement that checks if the expression is true or false and accordingly execute the statements. Generally, it is followed by else statement. If the condition is true, then it will execute the code after the if statement but if the condition is false then it will execute the else part. If statements in Matlab are also used in a similar way.

Syntax:

``````if condition 1
Statement 1
else
Statement 2
end``````

Let us see some examples:

Example 1:

``````x=5;
If x=5
Y=7;
else
Y=0
end``````

Output : Y = 7

Here we have assigned x value as 5, so the first statement checks whether x value is 5 or not. In Example 1, the x value is 5 which proves that the condition is true and it will execute the statement after that which is Y= 7 and will display the result of we print it.

Example 2:

``````x=6;
if x=5
Y = 7
else
Y = 0
end``````

Output : Y=0

In Example 2, we have assigned the value of x as 6, first statement checks whether the assignment value is correct or not. Since the condition evaluates to false so it will execute the else part in the program and will give the output as Y = 0.

We can also use elseif statement with if statement in the program but the use of elseif is optional and depending on the requirement, we can use it.

Syntax:

``````if condition
Statement 1
elseif
Statement 2
else
Statement 3
end``````

Example 3:

``````x = 5;
y = 3;
if (x<y)
z=0;
elseif (x>y)
z=1;
end``````

Example 4:

``````x=5;
y = 5;
if(x<y)
z=0;
elseif (x>y)
z=1;
else
z=2;
end``````

Output : z=2

Else if block is used between if statement and else statement. Please find the above two examples describing the working of elseif statement. In Example 3, x and y values are assigned as 5 and 3. First statement checks whether the condition is true or not, here the expression is to check whether x<y which is not true. So, it will skip to the second block and checks the second expression as x>y which is true, so it will execute the subsequent statement and display the output as z =1. This marks the end of the program.

In Example 4, x and y values are assigned as 5. The first expression is x<y which is false, so it will jump to the second block which checks for the second expression x>y which is also false. Since neither of the above two expressions are true it will execute the else block and display the output as 2. This marks the end of the execution.

If statement is for multiple purposes like to compare array or character vectors. Please find the below examples for better understanding:

Example 5:

code:

``````limitval = 0.9
x=rand (5,1)
0.921
0.872
0.196
0.223
0.990
If any(x>limit)
Y  = 7;
else
Y = 0;
end``````

Output : Y = 7

In the above example, the limit is assigned a value as 0.9 and rand function is used to generate random numbers. According to the input arguments in the rand function, it will generate 5 random numbers between 0 and 1. If condition checks whether any value is greater than 0.9 or the value assigned to limit. If there are any values greater, than it will give Y = 7 if we print it. If not then, it will give the statement as given in the else part.

Here the output of the above code is Y= 7 if we print it since 0.921 and 0.990 are greater than 0.9.

Example 6:

code:

``````a = 12;
minvalue = 3;
maxvalue = 7;
if (a <= minvalue) &&(a>=maxvalue)
disp (“Hello1”)
elseif (a > maxvalue)
disp(“Hello2”)
else
disp(“Hello3”)
end``````

Output : “Hello2”

Here if statement is used to check multiple conditions. In the first line of the code, since && operator is used, it checks if both the conditions are met and then only it will execute the consecutive statement. After checking, the result is false so it will check the second condition in elseif line and since it is true, it will display the output as “Hello2” ignoring the else part.