Chapter 5 Selection Statements

Slides:



Advertisements
Similar presentations
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Advertisements

1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Unit 3 Lesson 8 Selection Structures Mr. Dave Clausen La Cañada High School.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Flow of Control Part 1: Selection
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
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.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School.
Chapter Making Decisions 4. Relational Operators 4.1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Branching statements.
Chapter 3 Selection Statements
Chapter 4: Control Structures I (Selection)
Java Programming Fifth Edition
More on the Selection Structure
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Chapter 4: Control Structures I (Selection)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 4 Selection.
Chapter 6: Repetition Statements
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Control Statements Paritosh Srivastava.
Presentation transcript:

Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School

Boolean Expressions Selection Statements Control Structure a control statement that help the computer make decisions Certain code is executed when the condition is true, other code is executed or ignored when the condition is false. Control Structure controls the flow of instructions that are executed. 9/4/2019 Dave Clausen

New Data Type Simple data types that we already know New simple type int, double, char New simple type bool (Boolean data type: true or false) newer C++ compilers already have this type Borland C++ 5, Visual C++ 5, Codewarrior 3 older compilers don’t have this type Turbo C++ 3.0, Turbo C++ 4.5 Win 9/4/2019 Dave Clausen

Boolean Data Type In older systems 0 represents false any non zero number represents true download the file bool.h from AP Board or type the definition in the textbook. #include “bool.h” in preprocessor directives no need to make a project, just add #include “bool.h” assign values of true or false in lowercase letters or assign nonzero for true and zero for false Booltest.cpp Booltest.txt 9/4/2019 Dave Clausen

Relational Operators, and Boolean Expressions operations used on same data types for comparison equality, inequality, less than, greater than Simple Boolean Expression two values being compared with a single relational operator has a value of true or false 9/4/2019 Dave Clausen

greater than or equal to Relational Operators Arithmetic Operation = < >    Meaning Is equal to Is less than Is greater than less than or equal to greater than or equal to Is not equal to Relational Operator = = < > <= >= != 9/4/2019 Dave Clausen

Simple Boolean Expressions 7 = = 7 -3.0 = = 0.0 4.2 > 3.7 -18 < -15 13 < 0.013 -17.32 != -17.32 a = = a true false 9/4/2019 Dave Clausen

Order of Operations 1. ( ) 2. *, /, % 3. +, - 1. ( ) 2. *, /, % 3. +, - 4. = =, <, >, <=, >=, != 9/4/2019 Dave Clausen

Comparing Strings #include “apstring.cpp” compared according to ASCII values upper case < lower case (i.e. ‘A’ < ‘a’ is true) When comparing 2 strings where one is a subset of the other (one string is shorter & contains all the same letters) shorter string < longer string i.e. “Alex” < “Alexander” 9/4/2019 Dave Clausen

Confusing = and = = = = means equality in a comparison = means assigning a value to an identifier side effects are caused if we confuse the two operators P209Ex1.cpp P209Ex1.txt 9/4/2019 Dave Clausen

Compound Boolean Expressions Logical Operators And && (two ampersands) Conjunction Or || (two pipe symbols) Disjunction Not ! (one exclamation point) Negation Use parentheses for each simple expression and the logical operator between the two parenthetical expressions. i.e. ((grade >= 80) && (grade <= 90)) 9/4/2019 Dave Clausen

Truth Tables for AND / OR Expression1 Expression2 E1&&E2 E1||E2 (E1) (E2) true true true true true false false true false true false true false false false false 9/4/2019 Dave Clausen

Truth Table for NOT Order of Priority for Booleans Expression Not E (E) !E true false false true Order Of Priority in Boolean Expressions 1. ! 2. && 3. || 9/4/2019 Dave Clausen

Order of Operations including Booleans 1. ( ) 2. ! 3. *, /, % 4. +, - 5. <, <=, >, >=, = =, != 6. && 7. || 9/4/2019 Dave Clausen

Complements Operation ! < ! <= ! > ! >= Complement (equivalent) >= > <= < 9/4/2019 Dave Clausen

DeMorgan’s Laws ! ( A && B) is the same as: !A || !B Not (true AND true) Not(true) OR Not(true) Not (true) false OR false false false !( A || B) is the same as: !A && !B Not( true OR true) Not(true) AND Not(true) Not (true) false AND false false false 9/4/2019 Dave Clausen

If Statements Format for if statements: sum = 0.0; if (<Boolean expression>) <statement> Parentheses are required around the Boolean Expression. sum = 0.0; cin>>number; if (number > 0.0) sum = sum + number; cout<<“the sum is: “<<sum<<endl; 9/4/2019 Dave Clausen

Compound Statements Use the symbols { and } to group several statements as a single unit. Simple statements within the compound statements end with semicolons. Compound Statements are sometimes called a statement block. Use compound statements in an if statement if you want several actions executed when the Boolean expression is true. Minmax.cpp Minmax.txt 9/4/2019 Dave Clausen

if …else Statements Format for if…else statements if (<Boolean expression>) <true statement> //end of if option else <false statement> //end of else option 9/4/2019 Dave Clausen

if …else Example cout<<“Please enter a number and press <Enter>. “; cin>>number; if (number < 0) { neg_count = neg_count + 1; cout<<setw(15) << number<<endl; } //end if option else non_neg_count = non_neg_count + 1; cout << setw(30) << number << endl; //end of else option 9/4/2019 Dave Clausen

Behavior of Selection Statements ? statement true false if (<Boolean expression>) { <statement 1> . <statement n> } <statement> else 9/4/2019 Dave Clausen

Robust Programs Robust Programs are protected from: most possible crashes bad data unexpected values For student programs balance “error trapping” and code efficiency our text assumes valid data is entered when requested. 9/4/2019 Dave Clausen

Nested if statements Nested if statement an if statement used within another if statement where the “true” statement or action is. If (score >=50) if (score>=69.9) cout<<blah, blah, blah //true for score>=69.9 and score>=50 else cout<<blah, blah, blah //false score>=69.9 true score >=50 cout<<blah, blah, blah //false for score >=50 9/4/2019 Dave Clausen

Extended if statements Extended if statements are Nested if statements where another if statement is used with the else clause of the original if statement. If (condition 1) action1 else if (condition2) action2 else action3 9/4/2019 Dave Clausen

Avoid Sequential Selection This is not a good programming practice. Less efficient only one of the conditions can be true this is called mutually exclusive conditions if (condition1) //avoid this structure action1 //use nested selection if (condition2) action2 if (condition3) action3 Sales.cpp Sales.txt 9/4/2019 Dave Clausen

Program Testing Use test data that tests every branch or selection in the program. Test the true statement(s) Test the else statement(s) Test the border, edge, extreme cases. Test carefully nested and extended selection statements and their paths. 9/4/2019 Dave Clausen

Switch Statements Allows for multiple selection that is easier to follow than nested or extended if statements. switch (age) //age is of type int { case 18: <statement1> break; case 19: <statement2> case 20: <statement3> default: <default statement> } 9/4/2019 Dave Clausen

Switch: Flow of Execution The selector (argument) for switch must be of an ordinal type (not double) switch (age) The variable “age” is called the selector in our example. If the first instance of the variable is found among the labels, the statement(s) following this value is executed until reaching the next break statement. Program control is then transferred to the next statement following the entire switch statement. If no value is found, the default statement is executed. Switch statements are similar to “case” statements in Pascal 9/4/2019 Dave Clausen

Switch Statement Example 2 switch (grade) //grade is of type char { case ‘A’ : case ‘B’ : cout<<“Good work!”<<endl; break; case ‘C’ : cout<<“Average work”<<endl; case ‘D’ : case ‘F’ : cout<<“Poor work”<<endl; default : cout<<grade<<“ is not a valid letter grade.”; } 9/4/2019 Dave Clausen

Assertions Assertions A statement about what we expect to be true at a certain point in the program where the assertion is placed. Could be used with selection, functions, and repetition to state what you expect to happen when certain conditions are true. Could be comments, should be Boolean expressions that could be evaluated by the compiler. Are used as preconditions and post conditions Can be used as a proof of your program’s correctness Can help you “debug” your program. If there is an error, the program halts with an error message and the line number where the error is located. 9/4/2019 Dave Clausen

Implementing Assertions Use #include <assert.h> Example 1: assert (number_of_students != 0); class_average = sum_of_scores / number_of_students; The parameter of assert is a Boolean Expression. The compiler verifies if the assertion is true or false if false, the program halts with an error message: Assertion Failed: number_of_students !=0, file “filename”, line 48 this tells you that your assertion is false 9/4/2019 Dave Clausen

Assertion Example #2 Assertions used as pre & post conditions: assert((num1>=0) && (num2>=0)); //precondition if (num1 < num2) { temp = num1; num1 = num2; num2 = temp; } assert((num1>=num2) && (num2>=0)); //postcondition 9/4/2019 Dave Clausen

Assert as a Debugging Tool Executable assertions when false, terminate your program and tell you the line number where the error is located. This is valuable during the software debugging and testing phases. When your program is working correctly, you do not need to remove all the assertions manually. Add the preprocessor directive: #define NDEBUG BEFORE #include <assert.h> to ignore all assert functions. //NDEBUG means No debug. 9/4/2019 Dave Clausen