Upload
beryl-mcgee
View
215
Download
0
Embed Size (px)
Citation preview
Reviewif imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); returnendif x < 10 % leave this out, type the next line first, and then put it in if x == 7 fprintf('Lucky number!\n'); else fprintf('Small number\n'); endelse if x == 13 fprintf('Unlucky number!\n'); else fprintf('Big number\n'); endend
Control Constructs
Sequential Control
Normal execution, by order of statements.
Selection or branching statementsStatement chosen from two or more.
Ex: if construct
LoopsStatements are repeated.
Ex: for loop
Selection or branching statements
• Most common: if constructIF expression
statementsELSEIF expression statementsELSEIF expression statements
.:
ELSE statements
END
More on if construct
• Must have one if and an end.
• May have any number of elseif clauses.
• May have one else clause.
• if constructs can be nested.
Exercise
Write a program that prints out the days when the user enters a number.
• ‘1’ is ‘Sunday’
• ‘2’ is ‘Monday’
• ‘3’ is ‘Tuesday’
• Any other number is ‘Any other day’
Some Definitions
• Nesting – Including a branching statement as one of the branches of another branching statement. It is allowed in Matlab and all other languages.
• Making a program robust.– A program is robust only if it does something
reasonable with all inputs.– Example: handle numbers with imaginary
components, as in last week’s example.
More Definitions
• Noun: Branch. A point in a program where the control can switch to more than one command.
• Verb: Branch. To choose a statement based on the state of the variables
Boolean Logic
• The expression in an if construct is an example of Boolean logic. Boolean has only two states: True and False.
• Traditionally, this is thought of as 1 = True and 0 = False.
• In Matlab, 0 = False, and True is any non-zero value.
Logical Operators and Truth Tables
X Y X&Y X|Y
0 0 0 0
0 True 0 True
True 0 0 True
True True True True
Operators• AND ( & )• OR ( | )• NOT ( ~ )
Relational Operators
• == Equal to
• > Larger than
• < Less than
• >= Larger or equal to
• <= Less or equal to
• ~= Not equal to
Precedence of operators
• Arithmetic operators
• Relational operators
• ~
• &
• |
Examples of Relational and Logical operators in an if construct
if X > 0 & X<10
fprintf(‘Positive number less than 10’);
end
%
if grades == ‘A’ | grades == ‘B’
fprintf(‘My CGPA will not suffer’);
end
switch statement or switch construct:
switch switch_expressioncase case_expression, statements case case_expression, statementscase {case_expression1, case_expression2,
case_expression3,. . .} statements
. . .otherwise statementsend
When to use the switch construct
• Switch is appropriate when there are a "small" number of constant values that must be treated differently. Useful only when it is possible to list all possible values of an expression.
• Especially useful for menus or highly restricted data validation
• No logical or relational statements. Just listed values.– Example: x < 1.70: if-statement (because there are an
infinite number of values)– Example: x == 1 or x == 2: switch statement.
Example of switch construct
grades=input(‘Enter my CS 103 grade’,’s’);switch grades
case ‘A’ fprintf(‘Plan for med school’);
case ‘B’ fprintf(‘Can still plan for med school’); case {‘C’, ‘D’}
fprintf(‘Do not plan for med school’); otherwise
fprintf(‘Practice: Do you want fries with that?’);
end
Exercise with Switch construct
•Write a program that prints out the days when the user enters a number.
• ‘1’ is ‘Sunday’
• ‘2’ is ‘Monday’
• ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, is ‘The rest of the week’
• Any other number is ‘Not a real day’
Example
Write a program that tells whether a measurement is long enough or not.
•Less than ten feet is too short
•Ten to fifteen feet is good
•More than fifteen is too long
Example
Write a program that will print a series of choices for the users and gets an input from them.