The IF-ELSE Statement:
The if statement selects whether an action is executed or not. Selecting between the two actions, or selecting from numerous actions, is accomplished by using if-else, nested if, and switch statements.
The if-else statement is used to select between the two statements, or sets of statements. The common form is as shown below:
if condition
action1
else
action2
end
At First, the condition is computed. If it is theoretically true, then the set of statements elected as action1 is executed, and that is it for the if-else statement. If rather the condition is theoretically false, the second set of statements elected as action2 is executed, and that is it. The first set of statements is known as the action of the if clause; it is what will be executed if the expression are true. The second set of statements is known as the action of the else clause; it is what will be executed if the expression is false. One of these actions, and only one, will be executed-that one depends on the value of the condition.
For illustration, to establish and print whether or not a random number in the range from 0 to 1 is less than 0.5, an if-else statement can be used:
if rand < 0.5
disp('It was less than .5!')
else
disp('It was not less than .5!')
end