Week 8: Decisions Bryan Burlingame 21 October 2015.

Slides:



Advertisements
Similar presentations
Lecture 3: Control Structures - Selection BJ Furman 10SEP2012.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
Week 4: Control Structures - Repetition
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
Lecture 1 The Basics (Review of Familiar Topics).
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
Logical Operators and Conditional statements
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Selection. Structured Components There are three basic constructs that can be used to build programs Sequence One statement follows another. Selection.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
CONTROLLING PROGRAM FLOW
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
Week 4 Program Control Structure
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Decision Statements, Short- Circuit Evaluation, Errors.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
COMP Loop Statements Yi Hong May 21, 2015.
Lecture 2: Logic Bryan Burlingame 10 Feb Ref: xkcd:
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Statements: Part1  if, if…else, switch 1.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 2: Logic Bryan Burlingame 31 Aug 2016.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
Week 4: Microcontrollers & Flow Control
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Computer Science Core Concepts
Week 3 – Program Control Structure
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Presentation transcript:

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

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

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

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

Boolean Logic And && FalseTrue False TrueFalseTrue Or || FalseTrue False True Exclusive Or (Xor) ^^ FalseTrue False True False

Numeric Comparison Operators  ! (not) Changes true to false and false to true , >= Less than, and less than or equal to are different operations Note: !( =  ==, != (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

Examples float 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 e); //false (b e); // true (b e) || (c < e); //true (b e) || (b + c); //true, why? printf(“%f”, b) && (b + c); //true, why?

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

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

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.

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

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

Double-Selection IF - Flowchart TRUE Speed > 65 FALSE Print “Over speed limit” Print “Within speed limit”

IF-ELSE statement example Pseudocode (notice indentation!) If speed is greater than 65 mph print “Over speed limit!” else print “Within speed limit”

if - example int 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 them else if( speed < 65 ) { // note the indentation. printf( “Speed is within legal limits\n” ); } else { printf( “Speed is precisely 65\n” ); }

Switch char 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 }

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

for - example int 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:

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 statement TRUE FALSE condition

while - example int 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:

do/while Structure – Flow Chart statement is executed at least once statement TRUE FALSE condition

do - example int 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 end Outputs:

Ternary operator Returns 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 = 2 Since bob is greater than 2, return the “true value” ann = (bob < 2) ? 2 : jim; // ann = 23 Since bob is not less than 2, return the “false value” Can be used inline, but be careful bob + ann ? 23 : 45; // evaluates to 23 bob + (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

References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html