Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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

2 Control Constructs  Sequential Control Normal execution, by order of statements.  Selection or branching statements Statement chosen from two or more. Ex: if construct  Loops Statements are repeated. Ex: for loop

3 Selection or branching statements Most common: if construct IF expression statements ELSEIF expression statements ELSEIF expression statements. : ELSE statements END

4 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.

5 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’

6 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.

7 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

8 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.

9 Logical Operators and Truth Tables XYX&YX|Y 0000 0True0 00 Operators AND ( & ) OR ( | ) NOT ( ~ )

10 Relational Operators ==Equal to >Larger than <Less than >=Larger or equal to <=Less or equal to ~=Not equal to

11 Precedence of operators Arithmetic operators Relational operators ~ & |

12 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

13 switch statement or switch construct: switch switch_expression case case_expression, statements case case_expression, statements case {case_expression1, case_expression2, case_expression3,...} statements... otherwise statements end

14 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.

15 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

16 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’

17 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

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


Download ppt "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."

Similar presentations


Ads by Google