Chapter 4 (Conditionals)

advertisement
CMPS 1371
Introduction to Computing
for Engineers
CONDITIONAL STATEMENTS
ARRAYS
 The important use of arrays as indices has
to do with picking out the elements of a
vector (or array) that have a certain
property or characteristic. To do this
requires to things:


definition of the characteristic
"Finding" the indices of the vector with that
property
Boolean test on Vectors
 Consider the vector

A = [ 5 3 1 7 -3 -1 9 27 6 1]
 Suppose want non-negative elements.
Use a logic comparison to locate:



Need notion of true/false – called
Boolean
Only two possible values (true or false)
In Matlab false is 0, true is 1
Relational Operators
<
<=
>
>=
==
~=
Less than
Less than or equal to
Greater than
Greater than or equal to
Equal to
Not equal to
Comparisons
 Comparisons are either true or false
 Most computer programs use the number 1
for true and 0 for false
The results of a comparison are used in
selection structures and repetition
structures to make choices
MATLAB compares corresponding elements and
determines if the result is true or false for each
Comparisons
 In order for MATLAB to decide a comparison
is true for an entire matrix, it must be true for
every element in the matrix
Logical Operators
&
~
|
xor
and
not
or
exclusive or
Simple
“if”
statement
if comparison
statements
end
For example….
if G<50
count = count +1;
disp(G);
end
IF statements
if
condition
false
if
condition
true
statements
if A>10
% computations;
end
false
true
statements (1)
statements (2)
if A>10
% computations;
else
% computations;
end
 Each branch can include multiple statements
 Statements can also include other if statements (can
nest if statements inside if statements)
The if/else structure
 The simple if triggers the execution of a block of code
if a condition is true
 If it is false that block of code is skipped, and the
program continues without doing anything
 What if instead you want to execute an alternate set of
code if the condition is false?
Flow chart of an if/else structure
True
Block of code to
execute if the
comparison is true
Comparison
False
Block of code to
execute if the
comparison is false
Calculation Example
 Use an if structure to calculate a natural log
 Check to see if the input is positive


If it is, calculate the natural log
If it isn’t, send an error message to the screen
M-file Program
Interactions in the Command Window
The if/else/elseif structure
 Use the elseif for multiple selection criteria
 For example
if (grade >= 90)
Letter = ‘A’
Disp(‘You did great’) % note disp function
Elseif (grade >= 80) % didn’t need <90
Letter = ‘B’
Elseif (grade >= 70)
Letter = ‘C’
Elseif (grade >= 60)
Letter = ‘D’
Else
Letter = ‘F’
End
Examples
• Write a program to
determine if an
applicant is eligible
to drive
Start
if age<16
True
Sorry –
You’ll have
to wait
elseif
age<18
True
You may
have a youth
license
elseif
age<70
else
Drivers over 70
require a
special license
True
You may have a
standard license
M-file Program
Always test your
programs – making
sure that you’ve
covered all the
possible calculational
paths
switch/case
 This structure is an alternative to the if/else/elseif




structure
The code is generally easier to read
This structure allows you to choose between multiple
outcomes, based on some criterion, which must be
exactly true
The criterion can be either a scalar (a number) or a
string.
In practice, it is used more with strings than with
numbers.
The structure of switch/case
switch variable
case option1
code to be executed if variable is exactly
equal to option 1
case option2
code to be executed if variable is exactly
equal to option 2
…
case option_n
code to be executed if variable is exactly
equal to option n
otherwise
code to be executed if variable is not
equal to any of the options
end
Airfare Example
Suppose you want to determine what
the airfare is to one of three cities
Menu
 The menu function is often used in
conjunction with a switch/case structure.
 This function causes a menu box to appear on
the screen with a series of buttons defined by
the programmer.
 Because the input is controlled by a menu
box, the user can’t accidentally enter a bad
choice
 This means you don’t need the otherwise
portion of the switch/case structure
Menu
Note that the otherwise portion of the
switch/case structure wasn’t used
When you run this code a menu
box appears
Instead of entering
your choice from
the command
window, you select
one of the buttons
from the menu
Download