Download presentation

Presentation is loading. Please wait.

Published byHelen Rakestraw Modified over 2 years ago

1
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to: additional operators branches to control operations loops to repeat operations

2
Relational Operators in M ATLAB A operator B A and B can be: – Variables or constants or expressions to compute – Scalars or arrays (match the sizes on arrays!) – Numeric or string Operators: >> == = << =~ = Result is true (1) or false (0) – perhaps an array

3
Examples: expressionresult 5 < 71 [ 3 5 2 ] > = [ 1 0 12 ]1 1 0 max( 1:6 ) < = 7 1 [3 pi -12 ] > 11 1 0 'Tom' = = 'Bob'0 1 0 'Tom' = = 'm'0 0 1 Note – arrays and strings need to be the same size

4
Matlab has Logical Operators as Well A operator B A and B can be: – Variables or constants or expressions to compute – Scalars or arrays, numeric or string A and B are interpreted as logical (binary): – Numeric 0 is interpreted as false – All else is interpreted as true (equal to 1) Result is true (1) or false (0) – perhaps an array

5
Basic operators: and & or | xor not ~ 0 0 1 1 ~A~A 0 1 1 0 xor(A,B) 1001 1010 1111 0000 A|BA|BA&BBA “truth table”“unary” operator

6
Operator Precedence (left to right) 1. Parentheses ( ) 2. Transpose(') and power(.^) 3. Negation (-) and logical negation (~) 4. Multiplication (.*) and division (./), 5. Addition (+) and subtraction (-) 6. Colon operator (:) 7. Relational operators (, >=, = =, ~=) 8. Logical AND (&) 9. Logical OR (|)

7
Branches, Conditional Statements Commands to select and execute certain blocks of code, skipping other blocks. Three types in Matlab: – if/else – switch – try/catch this week

8
“If/Else” Use relational and logical operators to determine what commands to execute: if expression {commands if true } else {commands if false } end evaluate this use of blue in editor; also, auto indentation on commands

9
Example – output whether a variable x is positive or not: x = … { computed somehow }; if x > 0 disp('the value is positive') else disp('the value is negative or zero') end

10
Example – output a warning if the variable x is negative (note that there is no “else” portion in this example): x = … { computed somehow }; if x < 0 disp( ' Warning: negative value ' ) end the else component is not required

11
Example – ask if a plot should be drawn: x = input( ' Plot now? ', ' s ' ); if x = = ' yes ' | x = = ' YES ' plot( ….. ) end more complicated expression to evaluate

12
Example – Write a script to put 2 numbers in numerical order:

13
Loops Commands to repeatedly execute certain blocks of code Two types in Matlab: – for – while this week

14
The “for” Loop Used for a specific number of repetitions of a group of commands: for index = array { commands to be repeated go here } end Rules: One repetition per column of array index takes on the corresponding column’s values

15
Example – implement a count down timer (in seconds):

Similar presentations

OK

Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)

Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on basics of ms office Ppt on standing order act packets Ppt on online shopping cart Ppt on improvement in food resources science Ppt on online banking in java Ppt online compressor map Ppt on kinetic theory of matter/gas laws Ppt on central administrative tribunal orders Ppt on places in our neighbourhood lesson Ppt on network theory and analysis