Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.

Slides:



Advertisements
Similar presentations
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Conditional Statement
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
TK 1914 : C++ Programming Control Structures I (Selection)
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Selection Chapter 6. C++ An Introduction to Computing, 3rd ed. 2 Objectives Expand on introduction to structures Examine if statement in more detail Study.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Chapter Making Decisions 4. Relational Operators 4.1.
Functions Chapter 4. C++ An Introduction to Programming, 3rd ed. 2 Objectives Study software development using OCD Take a first look at building functions.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Calvin College Controlling Behavior The if, switch and for Statements.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures 1 The if, for, and while Statements §5.1, §5.4, & §5.5 (Several examples here; also Lab #4)
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
The Ohio State University
Branching statements.
Controlling Behavior The if and for Statements.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Branching Statements
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Control Structures – Selection
Chapter 7 Conditional Statements
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chap 7. Advanced Control Statements in Java
Controlling Behavior The if and for Statements.
Presentation transcript:

Selection

Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections Observe use of boolean expressions for modeling logical circuits First look at class mutator methods

Computer Programming 3 Selection: the if Statement Single branch if (boolean_exp) statement Dual branch if (boolean_exp) statement1 else statement2 Multibranch if (boolean_exp) statement else if (boolean_exp) statement else if … else …

Computer Programming 4 Selection: the if Statement Note multibranch if – Appears to be a different version – Actually is an if statement with another if statement as the statement of the else if (boolean_exp) statement else if (boolean_exp) statement else if …

Computer Programming 5 The Multi-branch if The if ’s final form has a nested if as Statement 2 : if (Cond 1 ) Stmt 1 else if (Cond 2 ) Stmt 2... else if (Cond N ) Stmt N else Stmt N+1 Cond 1 Stmt 1 T F Stmt 2 Cond 2 TF Stmt N Cond N TF Stmt N+1...

Computer Programming 6 Multibranch if Note which else goes with which if

Computer Programming 7 The Dangling else Problem Consider: if (x > 0) if (y > 0) z = sqrt (x) + sqrt(y); else cerr << " * * Unable to Compute * *"; Which if does the else go with? In a nested if statement, an else is matched with the nearest preceding unmatched if.

Computer Programming 8 Conditional Expression Form: condition ? expression 1 : expression 2 Behavior – condition evaluated – If it is true, expression 1 returned as result – If it false, expression 2 returned as result Try it out … what gets printed? double a = 5, b = 10, c = -3; cout 0 ? "real root" : "imaginary root") ;

Computer Programming 9 Warning: Confusing = and == True and false in C++ – An integer value of 0 interprets as false – A non zero value interprets as true Assignments are expressions x = 7; – The value is assigned to the variable … and – The expression has a value (the value assigned) What happens when you write if (x = 7) …

Computer Programming 10 Warning: Confusing = and == When you write if (x = 7) … – The value is assigned to the variable – The expression has that value (in this case non zero) – The value of the expression is used to select the true or false branch of the if statement The program will – Compile and run without crashing – But will probably not execute as expected

Computer Programming 11 Selection: The switch Statement Multiple choices. – E.g. choose between different conversions You can use – The multi-branch if has non-uniform execution time: Menu option 'A' requires 1 comparison Menu option 'B' requires 2 comparisons... Menu option 'F' requires 6 comparisons Computations that are “later” in the if take longer. There are situations where the time to select one of many statements must be uniform. Solution: – Use switch statement instead

Computer Programming 12 The switch Statement Form: switch (expression) { case_list 1 : statement_list 1 ; case_list 2 : statement_list 2 ; … default: statement_list n+1 ; }

Computer Programming 13 Warning C++ switch statements exhibit drop-through behavior. 1. Expression is evaluated. 2. The control jumps to the right case statement. 3. Control continues within the switch statement until: a. The end of the switch is reached; b. A break is executed, terminating the switch; c. A return is executed, terminating the function; or d. An exit() is executed, terminating the program.

Computer Programming 14 Cases with No Action In some situations the expression of a switch statement may be a legal value but: – No action takes place for that value Solution: – Include that value in the case list – But include no statement, only the break ; switch (option) { case 1 : doWhatever(); break; case 2 : doSomethingElse(); break; … case 9: case 10: break; }

Computer Programming 15 When to Use switch Choose switch over if-else-if when: 1. The equality == comparison is being performed 2. The same expression is being compared in each condition … and … 3. The value to which the expression is being compared is int compatible

Computer Programming 16 Boolean Logic and Digital Design Based on work by George Boole – Note basic rules, pg 316 Use of these rules became important with invention of digital computer – Circuits

Computer Programming 17 Boolean Logic and Digital Design Combine to create half adder circuit – digit1, digit2, sum, and carry are all binary digits (either 0 or 1)

Computer Programming 18 Problem 1 Sort three real numbers x, y, and z in a non- decreasing order. (The solution will be provided in class)

Computer Programming 19 Problem 2 The user is presented with a menu of characters, namely A, B, C, and Q. When the user chooses A, we print “The choice is A”. When B is chosen, the output is “The choice is B”. If the user chooses C, then “The choice is C” is printed out. When Q is chosen, the program exits. (The solution will be provided in class)