week 8: decisions bryan burlingame 21 october 2015

25
Week 8: Decisions Bryan Burlingame 21 October 2015

Upload: ann-cain

Post on 13-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Week 8: Decisions Bryan Burlingame 21 October 2015

Week 8: Decisions

Bryan Burlingame

21 October 2015

Page 2: Week 8: Decisions Bryan Burlingame 21 October 2015

Announcements & The Plan for Today™ Homework #4 due with MidTerm Midterm in Lab next Week Basic decisions

Page 3: Week 8: Decisions Bryan Burlingame 21 October 2015

Midterm

In lab! Open books, notes, basically anything

printed Access to the Matlab website and any

ME30 related website, only No calculators!

First part by hand, second part on PC

Page 4: Week 8: Decisions Bryan Burlingame 21 October 2015

Midterm (con’t) Mathematics

Bitwise logic ( &, |, ^, <<, >> ) Modulo division ( % ) Standard algebraic operators ( *, /, +, - ) Base conversion (binary, decimal, hexadecimal)

Matlab Basic operations Graphing

What makes a complete graph? Linear approximations

Logic Flowcharts and Pseudo code

Page 5: Week 8: Decisions Bryan Burlingame 21 October 2015

Boolean Logic

Recall: bitwise logic Very similar in concept & shares the same

truth tables Recall 0 is false, everything else is true Mind the order of operations (and, xor, or) Boolean and bitwise logic can be mixed

Bitwise logic is simply a mathematical operation

Page 6: Week 8: Decisions Bryan Burlingame 21 October 2015

Boolean LogicAnd &&

False True

False False False

True False True

Or ||

False True

False False True

True True True

Exclusive Or (Xor) ^^

False True

False False True

True True False

Page 7: Week 8: Decisions Bryan Burlingame 21 October 2015

Numeric Comparison

Operators ! (not)

Changes true to false and false to true <, <=, >, >=

Less than, and less than or equal to are different operations Note: !(<) is the same as >=

==, != (not equal) Note: equivalence uses a double ‘=‘, assignment uses a

single ‘=‘, be wary = returns the value being assigned

Technically, a = b = c = d = 5; is legal. Why?

= is performed right to left, so the d is assigned 5, which returns 5. That 5 is assigned to c

Page 8: Week 8: Decisions Bryan Burlingame 21 October 2015

Examplesfloat b = 17.0; float d = 3.14;float c = 20.0; float e = 33.0;(b < c); //true(b + c); //true (not zero)((int)(b/c)); //false (is zero, why?)(b < c) && (d > e); //false(b < c) || (d > e); // true(b < c) && (d > e) || (c < e); //true(b < c) && (d > e) || (b + c); //true, why?printf(“%f”, b) && (b + c); //true, why?

Page 9: Week 8: Decisions Bryan Burlingame 21 October 2015

Shortcut evaluation When performing a logic operation, C only does

as much as necessary to know the truth state Ex:int a = 4;(a == 0) && (a++); //false on (a == 0)printf(“%d”, a); // prints 4(a == 4) || (a++); //true on (a == 4)printf(“%d”, a); // prints 4(a == 4) && (a++); // true on both a == 4 and a++printf(“%d”, a); // prints 5

Page 10: Week 8: Decisions Bryan Burlingame 21 October 2015

Flow control

These Boolean operations are used along with flow control (or branching) statements to control the flow of a program

DecisionsTrue False

Page 11: Week 8: Decisions Bryan Burlingame 21 October 2015

Flow control if/if else/else – do this, do that, or do the other switch – choose between a bunch of items for – Do something for some number of times

also commonly referred to as iteration i.e. iterating over a range or iterating over a data set

while – For as long as some decision is true, keep doing some stuff

do .. while – Do something. At the end, if some thing is true, do it again.

Page 12: Week 8: Decisions Bryan Burlingame 21 October 2015

Selection Structure Overview Three kinds of selections structures

if (also called, ‘single-selection’) if condition is true

Perform action if condition is false, action is skipped, program continues

if/else (also called, ‘double-selection’) if condition is true

Perform action else (if condition is false)

Perform a different action (this will be skipped if condition is true) switch (also called ‘multiple-selection’)

Allows selection among many actions depending on the integral value of a variable or expression

Page 13: Week 8: Decisions Bryan Burlingame 21 October 2015

Single Selection IF - Flowchart

TRUE

FALSE

Speed > 65

connector

flow line

decision symbol

action symbol

Print “You’re speeding”

The symbol > is a Relational Operator. The Expression “speed > 65” evaluates to 1 if true, 0 if false

Page 14: Week 8: Decisions Bryan Burlingame 21 October 2015

Double-Selection IF - Flowchart

TRUESpeed > 65

FALSE

Print “Over speed limit”

Print “Within speed limit”

Page 15: Week 8: Decisions Bryan Burlingame 21 October 2015

IF-ELSE statement example

Pseudocode (notice indentation!)

If speed is greater than 65 mphprint “Over speed limit!”

elseprint “Within speed limit”

Page 16: Week 8: Decisions Bryan Burlingame 21 October 2015

if - exampleint speed = 5; int b = 4;if( speed > 65 ){ // do everything until the closing }

printf( “You are speeding!\n” );} // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit themelse if( speed < 65 ){ // note the indentation.

printf( “Speed is within legal limits\n” );}else{

printf( “Speed is precisely 65\n” );}

Page 17: Week 8: Decisions Bryan Burlingame 21 October 2015

Switchchar x = 'a';switch( x ){ case 'a': printf("Print a"); break; // a ends here case 'b': printf("Print b"); case 'c': printf("Print c"); break; // b or c end here default: printf("Print other"); // Always have a default}

Page 18: Week 8: Decisions Bryan Burlingame 21 October 2015

for Loop Structure – Flow Chart

initialization

T

F

Terminal decision

statement Iteration

operation

Initializes the loop control variable:

ex. i = 0;

Tests the loop control variable to see if it is time to quit looping:

ex. i < 10;

Increments the loop control

variable:ex. i++

Page 19: Week 8: Decisions Bryan Burlingame 21 October 2015

for - exampleint a = 5; int b = 4;for( a = 0; a < 5; ++a ){ //for( initialization, termination, iteration)

printf( “%d\n”, a );} // starts at 0 and keeps going as long as a is // less than 5

Outputs:0

1

2

3

4

Page 20: Week 8: Decisions Bryan Burlingame 21 October 2015

while Loop - Flowchart View

Statement is executed while condition is true Note that the

condition must first be true in order for the statement to be executed even once

statementTRUE

FALSE

condition

Page 21: Week 8: Decisions Bryan Burlingame 21 October 2015

while - exampleint a = 0; int b = 4;while( a < 5 ) // boundary condition{ //does the same thing as the for loop, previously

printf( “%d\n”, a );++a; // changes the boundary

condition} // starts at 0 and keeps going as long as a is // less than 5

Outputs:0

1

2

3

4

Page 22: Week 8: Decisions Bryan Burlingame 21 October 2015

do/while Structure – Flow Chart

statement is executed at least once

statement

TRUE

FALSE

condition

Page 23: Week 8: Decisions Bryan Burlingame 21 October 2015

do - exampleint a = 0; int b = 4;do // notice that there isn’t a decision here{

printf( “%d\n”, a );++a;

} while( a < 5 ); // the decision is at the endOutputs:

0

1

2

3

4

Page 24: Week 8: Decisions Bryan Burlingame 21 October 2015

Ternary operatorReturns a value based on a comparison (comparison) ? (true value) : (false value);

Ex:

int bob = 4; int jim = 23; int ann = 43;ann = (bob > 2) ? 2 : jim; // ann = 2Since bob is greater than 2, return the “true value”

ann = (bob < 2) ? 2 : jim; // ann = 23Since bob is not less than 2, return the “false value”

Can be used inline, but be careful

bob + ann ? 23 : 45; // evaluates to 23bob + (ann ? 23 : 45); // evaluates to 27, why? Good style would always put the ternary operator between parentheses

when used inline

This operator is commonly very fast

Page 25: Week 8: Decisions Bryan Burlingame 21 October 2015

References

Modular Programming in C http://www.icosaedro.it/c-modules.html

math.h http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html