Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.

Slides:



Advertisements
Similar presentations
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
Chapter 4: Control Structures I (Selection)
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.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 Making Decisions
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
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
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
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)
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/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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,
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Chapter 3 Selection Statements
Chapter 4: Control Structures I (Selection)
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements
Selections Java.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Control Structures – Selection
Control Structures - Repetition
Chapter 4: Control Structures I (Selection)
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Flow of Control.
Control Structure.
Presentation transcript:

Control Structures – Selection Chapter 4

2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions if ( ) if ( ) … else  Selection if ( ) and if ( ) … else  switch  switch Structures assert  The assert Function

3 Control Structures  Statements can be executed in sequence  One right after the other  No deviation from the specified sequence

4 Control Structures  A selection structure can be used  Which statement is executed is selected by whether the expression is true or false

5 Control Structures  Statements can be repeated  The number of repetitions depends on when the expression turns false

6 Relational Operators  The expressions which determine Selection and Repetition are usually comparisons  Comparisons are done with relational operators Beware of mistaking the assignment = for the equality ==

7 Relational Operators Examples: ExpressionMeaningValue 8 < 158 is less than 15 true 6 != 66 is not equal to 6 false 2.5 > is greater than 5.8 false 5.9 <= is less than or equal to 7.5 true

8 Relational Operators Given string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; string str5 = "Big"; Determine the values of these comparisons using variables

9 Logical (Boolean) Operators  Logical or Boolean operators enable you to combine logical expressions  Operands must be logical values  The results are logical values (true or false) A unary operator Binary operators

10 Logical (Boolean) Operators &&  The && operator (logical and) If both operands are true, the result is true If either or both operands is false, the comparison is false ||  The || operator (logical or) If either or both of the operands are true, the comparison is true The comparison is false only if both operands are false !  The ! operator (logical not) The not operator reverses the logical value of the one operand

11 Logical Expressions 12 > 7 || 9 * 5 >= 6 && 5 7 || 9 * 5 >= 6 && 5 < 9 Highest Lowest Order of Precedence View Sample Program

12 Short Circuit Evaluation (x != 0) && (1.0 / x < 0.25)  Consider (x != 0) && (1.0 / x < 0.25)  If the first condition is false, the program could crash when it tried to divide by zero but if the first condition is false, the whole expression is false no need to go on  When C++ evaluates an expression, realizes that fact and does not even make the second comparison  Called "short circuit" evaluation

13 Selection if (...)  C++ has two versions of if statements  In this version, the condition is checked If the expression is true, the statement is executed If it is false, nothing happens

14 Selection if (...) if ( logicalExpression ) statement;  Syntax if ( logicalExpression ) statement; if (x < 5 ) cout << "low value for x";  Example if (x < 5 ) cout << "low value for x"; Note parentheses around the condition Note there is no "then" as part of the syntax

15 Selection if ( ) … else …  Also possible to make two way selection  If the expression is true, statement1 is executed  Otherwise statement2 is executed

16 Selection if ( ) … else … if (condition) statement1; else statement2;  Syntax if (condition) statement1; else statement2; if (x < 5) cout << "low x"; else cout << "high x";  Example if (x < 5) cout << "low x"; else cout << "high x"; View sample program

17 Compound Statements if  Consider the need for multiple statements to be controlled by the if  This is called a compound statement  Group the statements in curly brackets Statement1; Statement2; Statement3;

18 Compound Statements if (x < 5) { x = x + 10; cout << x; }  Example if (x < 5) { x = x + 10; cout << x; }  Note the use of indenting and white space in the source code for readability. The compound statement

19 The Nested if IF

20 Nested if  Syntax calls for a “statement” after the if ( … )  That statement can be any kind of statement (List statements we know about)  It can be an if statement cout cin assignment if if (x 5) cout << “hi mom”;

21 The Dangling else if else  How to determine which if the else goes with  Example: if (abs (x - 7)) if (x < 7) cout << “x approaches 7 from left”; else cout << “x approaches 7 from the right”; else cout << “x not close to 7”; Rule : An else goes with the closest unmatched if ? ?

22 The Dangling Else else if  Rule : an else goes with the closest unmatched if else if  Consider … how do you force an else to go with a previous if ? if (x < y) if (y > 3) cout 3”; else cout << “message about x and y”; if (x < y) if (y > 3) cout 3”; else cout << “message about x and y”; if (x < y) { if (y > 3) cout 3”; } else cout << “message about x and y”; if (x < y) { if (y > 3) cout 3”; } else cout << “message about x and y”; Use { curly brackets } to nest the statements

23 Multiple Selections  Consider determining a letter grade based on a score Cut off points for A, B, C, and D are 90, 80, 70, and 60 respectively  We check the score against each of these values  See source codesource code

24 Multiple Selections  Contrast if … else if …A sequence of if … else if … statements ifA sequence of separate if statements  What happens in each case when it is the first if condition that is true? if … else ifif … else if sequence will jump out of the structure whenever match is found ififsequence of separate if 's – each if is checked, no mater where the match is

25 Multiple Selections  Recall the current branching capability provided by the if ( … ) statement  Only branches two ways  We desire a more eloquent way to do multiway branching

26 switch Structures  C++ provides the switch statement switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); }

27 switch Structures  Value of the switch expression matched with one of the labels attached to a branch  The statement(s) with the match get executed switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); }

28 switch Structures  Switch expression => the expression in parentheses whose value determines which switch label is selected cannot be floating point usually is int or char case  Identifiers following case must be constants switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); }

29 switch Structures break  The break causes control to be shifted to first statement after the switch statement default  the default statement is executed if the value of the switch expression is NOT found among switch labels switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); } // next statement switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); } // next statement

30 Testing the State of an I/O Stream  The name of the input stream (used by itself) returns a value returns a 0 if it is NOT successful it returns a NON zero value if it IS successful

31 Testing the State of an I/O Stream  When reading a file (a named input stream) we wish to know when it reaches the end  Since the name returns a 0 or non-0, this can be used as a Boolean value  Used to control program sequencing, control a file reading loop

32 The assert Function  Some statements will compile and run fine in normal situations  Certain values may cause a statement to crash the program root = -b + sqrt(b * b – 4 * a * c);What might happen in this statement? root = -b + sqrt(b * b – 4 * a * c); The program will crash if it tries to take the square root of a negative number

33 The assert Function  C++ provides a function which can check specified conditions If the condition is true the program continues If the condition is false, it will cleanly terminate the program It displays a message as to what condition caused the termination assert ( logicalValue);  Syntax assert ( logicalValue); #include  Note, you must #include

34 The assert Function assert (b * b - 4 * a * c >= 0); root = -b + sqrt(b * b – 4 * a * c);  Example: assert (b * b - 4 * a * c >= 0); root = -b + sqrt(b * b – 4 * a * c);  At run time the assertion condition is checked If true program continues assertIf false, the assert halts the program  Good for debugging stage of your program Shows you places where you have not written the code to keep things from happening assertOnce fully tested, assert s might be commented out