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

Lecture 4-1

Uploaded by

4cwsj2ftyh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 4-1

Uploaded by

4cwsj2ftyh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Matlab Programming for Engineers

Flow Control and Loops


Branching Statements
Lecture 4-1

FACULTY OF INFORMATION TECHNOLOGY


PETRA UNIVERSITY
THE LOGICAL DATA TYPE (true, false)
2
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=) 3

Some relational operations


and their results
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)
4
Compare array by Be careful because of
Compare array roundoff errors
another one (of the
by a value
same size)
LOGIC OPERATORS
5
Operator Operation
& Logical AND (scalar/array, both sides evaluation)
&& Logical AND (scalar, left then right side evaluation)
| Logical inclusive OR (scalar/array both sides evaluation)
|| Logical inclusive OR (scalar, left then right side evaluation)
xor Logical exclusive OR
~ Logical NOT
LOGIC OPERATORS
EXAMPLE
6
LOGICAL FUNCTIONS
7
8

Branches:
a) if Statement
Branches: “if” Statement 9

false if ( condition )
condition
statement 1
statement 2
true
...
statement end
group
Branches: “if - else” Statement 10
Example: Create a script file and type the following code:
11
Example: 12
Check each element in x in this example all elements
give true 13

Check each element in x in this example -9


give false
Create two vectors x, and y with values (4,9,25) for x and (4,6,8) for y .
If all elements in x and y greater than zero create a new vector named z
Where z = x +y, other wise print this statement “some of the elements of
14
x and/or
Y are negative”

In false case
BRANCHES (if-STATEMENT) 15

true condition false


1

statement
Optional group 1 true condition false
2

statement statement
group 2 group 3
Example:
16
Branching Examples
17
 Example: Assigning letter grades

Range Grade
100  grade > 95 A
95  grade > 86 B
86  grade > 76 C
76  grade > 66 D
66  grade > 0 F

How can we compute the letter corresponding to a given


numeric grade?
Branching Examples 18

 Solution for Letter grade example:


19
Branching Examples
20
evaluating a Function of Two Variables
Solution:
1- create script named calc
2- create function named calc test:
21
3- test the value of x and y command window(call
statement)
The Nested if Statements
22
It is always legal in MATLAB to nest if-else statements which means you
can use one if or elseif statement inside another if or elseif statement(s).
Example:
23
a = 100;
b = 200;
% check the boolean condition
if( a == 100 )
% if condition is true then check the following
if( b == 200 )
% if condition is true then print the following
fprintf('Value of a is 100 and b is 200\n' );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );
24

Branches:
b) switch Statement
BRANCHES (switch-STATEMENT)
The switch statement syntax is a means of conditionally
25
executing code. In particular, switch executes one set of
statements selected from an arbitrary number of alternatives.

The switch_expr & case_expr


may be a scalar or a string

Optional
BRANCHES (switch-STATEMENT)
26
Remarks:
 The case expression could be more than one enclosed by {}

 At most one code block can be executed. If the switch_exp matches more
than case_exp the first one is executed.
BRANCHES (switch-STATEMENT) 27
Example:
Switch statement is an
alternative for if statement:

If (value == 1 | value ==3 |


value == 5 | value == 7 |
value == 9)
disp (‘odd’)
elseif (value == 2 | value
==4 | value == 6 | value
== 8 | value == 10)
disp (‘even’)
else
disp(‘out of range’)
end
Example:
28
BRANCHES (switch-STATEMENT)
29
BRANCHES (switch-STATEMENT)
30
H.W : Use MATLAB to create a game called “Guess the number”. The game 31
will ask the player to guess the number between 0-10. The dummy(answer)
number is generated randomly(using rand round command). If player failed 3
times, the game is over. After game over, the game must ask the player
whether he/she want to play again. See the example for details.

Example
>>mygame
Guess a number(0-10):5
Guess a number(0-10):7
Guess a number(0-10):2
You failed, Try again(Y/N)? % player enter ’Y’ if ‘N’ quit a
game
Guess a number(0-10):9
Yes, you got it!!!

You might also like