0% found this document useful (0 votes)
88 views

Experiment 6 Flow Control

This document describes various flow control statements in MATLAB including if/elseif/else statements, switch/case statements, for loops, while loops, and break statements. It provides examples of how to use each statement type to control program flow and execute code conditionally. The document also discusses some important considerations for using relational operators like == with matrices in if statements.

Uploaded by

kurddoski28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Experiment 6 Flow Control

This document describes various flow control statements in MATLAB including if/elseif/else statements, switch/case statements, for loops, while loops, and break statements. It provides examples of how to use each statement type to control program flow and execute code conditionally. The document also discusses some important considerations for using relational operators like == with matrices in if statements.

Uploaded by

kurddoski28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

University of Zakho Petroleum Engineering Department MATLAB Laboratory

______________________________________________________________________________________________

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

It is important to understand how relational operators and if statements work with


matrices. When you want to check for equality between two variables, you might
use
if A == B
(6 - 1)
University of Zakho Petroleum Engineering Department MATLAB Laboratory
______________________________________________________________________________________________
This is legal MATLAB code, and does what you expect when A and B are scalars.
But when A and B are matrices, A = = B does not test if they are equal, it tests
Where they are equal; the result is another matrix of 0's and 1's showing element-
by-element equality. In fact, if A and B are not the same size, then A = = B is an
error. The proper way to check for equality between two matrix is to use the isequal
function,
if isequal (A, B)

Example:
A=input ('A=');
B=input ('B=');
if A > B
'greater'
elseif A < B
'less'
elseif A == B
'equal'
else
error ('Unexpected situation')
end

switch and case statement


The switch statement executes groups of statements based on the value of a
variable or expression. The keywords case and otherwise delineate the groups.
Only the first matching case is executed. There must always be an end to match the
switch. If the first case statement is true, the other case statements do not execute.

(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

Write a MATLAB program in M-File to do the following steps:


a) Input the value of x, y, z.
b) Calculate s.

(6 - 7)

You might also like