You are on page 1of 4

2.

BRANCHING
AIM:-

To understand different branching statements available in MATLAB

THEORY:

In MATLAB branches are the statements that permit us to select and execute specific
sections of code while skipping other sections of code. They are the if construct, the switch
construct and the try/catch construct where the control expressions are logical expression that
control the operation of the construct. If control_expr_1 is true (non-zero), then the program
executes the statements in block 1, and go to the first executable statement following the end.
Otherwise, the program check for the status of control_expr_2 is true, and then the program
executes the statements in the block associated with the elseif section.

The control expression in each clause will be tested only if the control expression in
every clause are false (0) executed. If all control expressions are false, then the program
executes the statements in the block associated either the else clause, then the execution
continues after the end statement without executing any part of if construct

In most circumstances, the control expressions will be some combination of relational


and logical operators. When an operator is true, its result is non-zero and the corresponding
block of code will be executed.

Syntax of ‘if else’ Construct:

if construct_expr_1
Statements 1
Statements 2
(This forms block1)
elseif control_expr_2
Statements 1
Statements 2
(This forms block 2)
else
Statements 1
Statements 2
(This forms block 3)
end

Note concerning the use of if constructs:


The ‘if’ construct is very flexible. It must have at least one if statement and one end
statement. In between, may be any numbers of elseif clauses, and may also have one else
clause. With this combination of features, it is possible to implement any desired branching
construct. In addition, if construct can be nested. Two if construct can be nested if one of
them lies entirely within a single code block of the other one. The following two ‘if’
constructs are properly nested.

if x > o

……

if y < 0

……

end

end

The MATLAB interpreter always associated a given end statement with the most
recent if statement, so the first end below the closes if y<0 statement, while the second end
the closes if x>0 statement. This works well for a properly written program, but can cause
the interpreter to produce confusing error messages in cases where the programmer makes
a coding error.

Example Using If Construct


The ‘switch case’ Construct:

The switch case construct is another form of branching construct. It permits a


programmer to select a particular code block to execute based on the value of integer,
character or logical expressions.

Syntax:-

switch switch_expr
case case _expr_1
Statements 1
Statements 2
(This forms block 1)
case case_expr_2
Statements 1
Statements 2
(This forms block 2)
otherwise
Statements 1
Statements 2
(This forms block 3)
end
If value of switch_expr is equal to case_expr_1, then the first code block will be
executed, and the program will jump to the first statement following the end of the switch
construct. Similarly, if the value of switch_expr is equal to case_expr_2, then the second code
block will be executed, and the program will jump to the first statement following the end of
the switch construct. The same idea applies for any other case in the construct. The otherwise
code block is optional. If it present, it will be executed whenever the value of switch_expr is
outside the range of all of the case selectors

If many values of the switch_expr should cause the same code to execute, then all of
those values may be included in a single block by enclosing them in brackets, as shown
below. If the switch expression matches any of the case expression in the list, then the
respective case block will be executed.

switch switch_expr
case {case_expr_1, case_expr2, case_expr_3}
Statements 1
Statements 2 (This forms block 2)
.
.
.
.
otherwise
Statements 1
Statements 2
(This forms block n)
end
The switch_expr and each case_expr may be either numerical or string values. If the
switch expression matches more than one case expression, only the first one of them will be
executed.

Examples using switch construct

You might also like