18
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if 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'); end else if x == 13 fprintf('Unlucky number!\n'); else fprintf('Big number\n'); end end

Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

Embed Size (px)

Citation preview

Page 1: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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

Page 2: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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

Page 3: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

Selection or branching statements

• Most common: if constructIF expression

statementsELSEIF expression statementsELSEIF expression statements

.:

ELSE statements

END

Page 4: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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.

Page 5: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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’

Page 6: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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.

Page 7: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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

Page 8: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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.

Page 9: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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 ( ~ )

Page 10: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

Relational Operators

• == Equal to

• > Larger than

• < Less than

• >= Larger or equal to

• <= Less or equal to

• ~= Not equal to

Page 11: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

Precedence of operators

• Arithmetic operators

• Relational operators

• ~

• &

• |

Page 12: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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

Page 13: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

switch statement or switch construct:

switch switch_expressioncase case_expression, statements case case_expression, statementscase {case_expression1, case_expression2,

case_expression3,. . .} statements

. . .otherwise statementsend

Page 14: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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.

Page 15: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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

Page 16: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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’

Page 17: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

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

Page 18: Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and

Example

Write a program that will print a series of choices for the users and gets an input from them.