Experiment 6 Flow Control
Experiment 6 Flow Control
______________________________________________________________________________________________
Experiment No. 6
Flow Control
Computer programming languages offer features that allow you to control the flow
of command execution based on decision making structures. MATLAB has several
flow control constructions:
if statement.
switch and case statement.
for statement.
while statement.
break statement.
if statement
The if statement evaluates a logical expression and executes a group of statements
when the expression is true. The optional elseif and else keywords provide for the
execution of alternate groups of statements. An end keyword, which matches the if,
terminates the last group of statements.
The general form of if statement is:
if expression 1
group of statements 1
elseif expression 2
group of statements 2
else expression 3
group of statements 3
end
Example:
A=input ('A=');
B=input ('B=');
if A > B
'greater'
elseif A < B
'less'
elseif A == B
'equal'
else
error ('Unexpected situation')
end
(6 - 2)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
The general form of switch statement is:
switch expression
case 0
statements 0
case 1
statements 1
otherwise
statements 3
end
Example:
clear all
close all
a = input('a = ');
b = input('b = ');
switch a + b
case 50
('Fail')
case 70
('Good')
case 85
('Very good')
otherwise
('Excellent)
end
for statement
The for loop repeats a group of statements a fixed, predetermined number of times.
The general form of for statement is:
(6 - 3)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
for variable = initial value: step size: final value
statement
…………………
statement
end
Example:
for i=1:5
for k=5:-1:1
m (i,k) = i*k;
end
end
>> m
m =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
A for loop cannot be terminated by reassigning the loop variable within the for
loop:
for i=1:10
x(i) = sin(pi/i);
end
x
i
(6 - 4)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Execution result is:
x=
0.0000 1.0000 0.8660 0.7071 0.5878 0.5000
0.4339 0.3827 0.3420 0.3090
i=
10
while statement
Repeat statements an indefinite number of times under control of a logical
condition. A matching end delineates the statements.
The general form of while statement is:
while expression
statement
…………………
statement
end
Example:
Here is a complete program, illustrating while, if, else, and end, that uses
interval bisection method to find a zero of a polynomial.
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if sign (fx) = sign (fa)
a = x;
fa = fx;
else
(6 - 5)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
b = x;
fb = fx;
end
end
break
Terminate execution of while or for loop. In nested loops, break exits from the
innermost loop only. If break is executed in an IF, SWITCH-CASE statement, it
terminates the statement at that point.
Example:
We can modify the previous example by Using break command.
b = 3;
fb = Inf;
while b-a > eps*b
x = (a+b) /2;
fx = x^3-2*x-5;
if fx == 0
break
elseif sign(fx) _= sign(fa)
a = x;
fa = fx;
else
b = x;
fb = fx;
end
end
(6 - 6)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
Example:
Without using the max command, find the maximum value of matrix (a) where
a =[11 3 14; 8 6 2; 10 13 1]
a=[11 3 14;8 6 2;10 13 1];
temp = a(1);
[n, m] = size(a)
for i = 1:n
for j = 1:m
if a(i,j) > temp
temp = a(i,j);
end
end
end
temp
Execution result is:
14
Exercises:
1. The value of s could be calculated from the equation below:
y 2 4 xz If y ≥ 4xz
s
inf If y < 4xz
(6 - 7)