EGR 2261 Unit 4 Control Structures I: Selection

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I Instructor: Mohammad Mojaddam
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Logical Operators and Conditional statements
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Decision Structures and Boolean Logic
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
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)
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 4: Control Structures I (Selection)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
The Ohio State University
Chapter 4: Control Structures I (Selection)
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 4: Control Structures I (Selection)
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
EGR 2261 Unit 5 Control Structures II: Repetition
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
The Selection Structure
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
JavaScript: Control Statements.
Java Programming: Guided Learning with Early Objects
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
3 Control Statements:.
Chapter 7 Conditional Statements
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

EGR 2261 Unit 4 Control Structures I: Selection Read Malik, Chapter 4. Homework #4 and Lab #4 due next week. Quiz next week. Handouts: Quiz 3, Unit 4 practice sheets.

Control Structures The programs you’ve written up to now have proceeded sequentially, executing each statement in order from the first statement to the last statement. A program’s execution can also proceed: Selectively: making a choice (Chapter 4) Repetitively: looping (Chapter 5) By calling a function (Chapter 6) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Control Structures (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Logical Expressions Selection or repetition relies on a logical expression (also called Boolean expression) that evaluates to a value of true or false. Examples: “8 is greater than 3.” Boring, because it’s always true. “num is greater than 3.” Sometimes true, sometimes false. Most logical expressions contain one or more relational operators: Allow comparisons. Require two operands (binary). Next slide lists the relational operators. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Relational Operators C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Relational Operators and the Simple Data Types The relational operators can be used with all three of our familiar simple data types (int, double, and char). Comparing int values: Example: 8 < 15 evaluates to true Example: 6 != 6 evaluates to false Comparing double values: Example: 2.5 == 5.8 evaluates to false Example: 5.9 <= 7.5 evaluates to true Do practice question 1. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Relational Operators and the Simple Data Types (cont’d.) Comparing char values : Example: 'R' > 'T’ The result of this comparison depends on the order of the characters in the ASCII code. From the table in Appendix C (next slide) we see that the ASCII code for 'R' is 82 and the ASCII code for 'T’ is 84. Therefore 'R' > 'T’ evaluates to false. Do practice question 2. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Relational Operators and the string Type Relational operators can also be applied to strings. Strings are compared character by character, starting with the first character. Comparison continues until either a mismatch is found or all characters are found equal. If two strings of different lengths are compared and they're the same through the last character of the shorter string, the shorter string is less than the longer string. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Relational Operators and the string Type (cont’d.) Suppose we have the following declarations: Do practice question 3. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

One-Way Selection C++ Programming: From Problem Analysis to Program Design, Seventh Edition

One-Way Selection (cont’d.) One-way selection syntax: statement is executed if the value of the expression is true. But statement is skipped if the value is false; program goes to whatever comes next. statement is any C++ statement. It could be a cin statement, a cout statement, an assignment statement, …. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

One-Way Selection: Example Demo using week04OneWaySelection.cpp. Do practice question 4. -Is it a serious limitation that we can only have one statement that is either executed or skipped? NO!.... Note indenting, which is for readability.

Compound Statements A compound statement (or block) is a group of statements enclosed in braces: Such a compound statement can be used anywhere a single statement can be used. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

One-Way Selection with a Compound Statement: Example Demo using week04CompoundStatement.cpp. -Show that you need the braces. The indenting is for readability only, and does not tell C++ where a block begins or ends. Do practice question 5.

Two-Way Selection C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Two-Way Selection (cont’d.) Two-way selection syntax: If expression is true, statement1 is executed; otherwise, statement2 is executed. statement1 and statement2 can be any C++ statements. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Two-Way Selection: Example Demo using week04TwoWaySelection.cpp. Do practice question 6.

Two-Way Selection with Compound Statements: Example Demo using week04TwoWaySelectionCompound.cpp.

Multiple Selections: Nested if Recall that, in the pictures above, statement1 and statement2 can be any C++ statements. They can even be other if statements, in which case we say that we’re nesting if statements inside each other. See next two slides for examples. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Multiple Selections: Nested if (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Nested if…else Statements Versus a Series of if Statements Here’s another example: The next slide shows another (less efficient) way to accomplish the same task. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Nested if…else Statements Versus a Series of if Statements (cont’d.) This code accomplishes the same task as the code on the previous slide, but it’s not as efficient. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

bool Data Type Recall that the bool data type has only two possible values: true and false. Internally, true is represented as the value 1, and false is represented as the value 0. In fact, if you use cout to display a Boolean variable or expression, what you’ll see on the screen is 0 or 1 rather than true or false. Demo using week04Bool.cpp. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Using the int Data Type Instead of bool Early versions of C++ did not provide a built-in bool data type. Instead, logical values were stored in variables of the data type int, with a value of 0 representing false and any non-zero value representing true. You can still use the int data type to manipulate logical (Boolean) values, but it’s more convenient to use the bool data type, since it lets you use the words true and false. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Logical Operators C++’s logical operators (also called Boolean operators) let you combine logical expressions. For example, you might want to check whether a variable’s is greater than 100 and less than 150: if (myNum > 100 && myNum < 150) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

The ! (Not) Operator C++ Programming: From Problem Analysis to Program Design, Seventh Edition

The && (And) Operator C++ Programming: From Problem Analysis to Program Design, Seventh Edition

The || (Or) Operator Do practice question 7. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Precedence of Some Operators Do practice question 8. As always, parentheses can override the normal order of evaluation. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Associativity of Operators Of the operators shown on the previous slide, operators with the same precedence are evaluated from left to right. (They have left-to-right associativity.) Note: The previous slide shows only some of the operators available in C++. Recall that the textbook’s Appendix B (next slide) shows precedence and associativity of all C++ operators. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Short-Circuit Evaluation Short-circuit evaluation: evaluation of a logical expression stops as soon as the value of the expression is known. In certain (unusual) cases this may give surprising results. Example: Compare the following code snippets: int x = 5; if ((x >= 2) || (++x < 10)) cout << x; Do practice question 9. int x = 5; if ((++x < 10) || (x >= 2)) cout << x; C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Three Common Mistakes The next three slides discuss three common mistakes: Confusing equality (==) with assignment (=). Comparing floating-point numbers for equality. Ignoring the associativity of relational operators.

Common Mistake #1: Confusing the Equality (==) and Assignment (=) Operators C++ allows you to use any expression that can be evaluated to either true or false as an expression in an if statement: if (x = 5) cout << "The value is five." << endl; This statement is syntactically correct, but it’s almost certainly not what the programmer intended. Using = in place of == is a silent killer: It is not a syntax error. It is a logical error that usually gives surprising results. Do practice question 10. Demo it (week04AssignmentVersusEquality.cpp) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Common Mistake #2: Comparing Floating-Point Numbers for Equality Comparison of floating-point numbers for equality may not behave as you would expect, because of rounding errors. Example: 1.0 == 3.0/7.0 + 2.0/7.0 + 2.0/7.0 evaluates to false. Why? 3.0/7.0 + 2.0/7.0 + 2.0/7.0 = 0.99999999999999989 Solution: either avoid doing this, or use a tolerance value instead: Example: if fabs(x – y) < 0.000001 You won’t run into this problem with integers—it’s fine to check them for equality. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Common Mistake #3: Ignoring Associativity of Relational Operators What we really want here is if (0 <= num && num <= 10) The expression (0 <= num <= 10) will evaluate as true for any non-negative number. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Common Mistake #3: Ignoring Associativity of Relational Operators (cont’d.) Suppose num = 5: Next, suppose num = 20: C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Program Style and Form (Revisited): Indentation A properly indented program: Shows the natural grouping of statements. Helps you spot and fix errors quickly. Two commonly used styles for braces: Each brace on a line by itself. Or left brace is placed after the expression, and the right brace is on a line by itself. if (x > 10) { cout << x; cout << y; } if (x > 10) { cout << x; cout << y; } C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Alternatives to if and if…else With if statements and if…else statements, you can handle any decision-making situations that your programs may require. But C++ does offer two other ways to do the same sort of thing. You may wish to use these instead of if and if…else: The conditional operator ?: The switch structure

Conditional Operator ?: The conditional operator looks like this: ?: It’s the only ternary operator: takes 3 arguments. Syntax: expression1 ? expression2 : expression3 If expression1 is true, the result of the entire expression is expression2. Otherwise, the result is expression3. Concise and efficient, but hard to read. See next slide for example. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Conditional Operator ?: (cont.) Example: max = (a >= b) ? a : b; The preceding statement is equivalent to the following code: if (a >= b) max = a; else max = b; Do practice question 11. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

switch Structures Syntax is shown at right. (expression) is evaluated first, and it must evaluate to an integer (this includes char expressions). Value of expression determines which corresponding action is taken. Expression is sometimes called the selector. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

switch Structures (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition

switch Structures (cont’d.) Do practice question 12. C++ Programming: From Problem Analysis to Program Design, Seventh Edition

switch Structures (cont’d.) One or more statements may follow a case label. Braces are not needed to turn multiple statements into a single compound statement. When a case value is matched, all statements after it execute until a break is encountered. The break statement may or may not appear after each statement. switch, case, break, and default are reserved words. C++ Programming: From Problem Analysis to Program Design, Seventh Edition