Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X1 Chapter 3 Control Statements.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Advertisements

L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4: Selections.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Programming Fundamentals I (COSC- 1336), Lecture 3 (prepared after Chapter 3 of Liang’s 2011 textbook) Stefan Andrei 10/9/20151 COSC-1336, Lecture 3.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 05 Title: Decision/Selection Control Structures Reference: MalikFarrell, chap 1, Liang Ch 3.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Information  HW1 now available  Group Project (Find your group members)  Start thinking about group projects.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Introduction to Control Statements JavaScript – Part 3 George Mason University June 3, 2010.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
1 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chapter 3 Selection Statements §3.1 The Boolean Type and Operators §3.2 The if-else Statements §3.3 Case Studies §3.4 Logical Operators §3.5 Switch Statements.
1 Chapter 5 Control Statements. 2 Objectives F To understand the flow of control in selection and loop statements. F To use Boolean expressions to control.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Introduction to Control Statements IT108 George Mason University.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Group Project (Find your group members)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Control Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Selections Java.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Chapter 3 Selections ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH Introduction to Java Programming, Liang (Pearson 2014)
EGR 2261 Unit 4 Control Structures I: Selection
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5 Control Statements
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 3 Control Statements
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Presentation transcript:

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 2 Comparison Operators

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 3 Type bool expression: a combination of values, variables, operators and functions which, when evaluated, produce a value. examples: 2 + 2, pow(2, 2), ++i, k % 2 = = 0 Boolean expression (a.k.a. condition): an expression that results in a Boolean value, that is, true (1) or false (0). examples: 5 > 3, k % 2 = = 0, isupper(‘a’)  All conditions are expressions but not all expressions are conditions

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 4 Simple if Statements if (booleanExpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; cout << "The area for the circle of " << " radius " << radius << " is " << area; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 5 Examples Listing 3.1 gives a program that checks whether a number is even or odd. The program prompts the user to enter an integer (line 9) and displays “number is even” if it is even (lines 11-12) and “number is odd” if it is odd (lines 14-15). TestBoolean Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 6 Note

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 7 Caution Adding a semicolon at the end of an if clause is a common mistake. This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. This error often occurs when you use the next-line block style.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 8 Boolean Operators Operator Name ! not && and || or

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 9 Truth Table for Operator &&

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 10 Truth Table for Operator ||

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 11 Examples Listing 3.3 gives a program that checks whether a number is divisible by 2 and 3, whether a number is divisible by 2 or 3, and whether a number is divisible by 2 or 3 but not both: TestBooleanOperators Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 12 Short-Circuit Operator When evaluating p1 && p2, C++ first evaluates p1 and then evaluates p2 if p1 is true; if p1 is false, it does not evaluate p2. When evaluating p1 || p2, C++ first evaluates p1 and then evaluates p2 if p1 is false; if p1 is true, it does not evaluate p2. Therefore, && is referred to as the conditional or short-circuit AND operator, and || is referred to as the conditional or short-circuit OR operator.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 13 The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 14 Examples Listing 3.4 presents a program that lets the user enter a year and checks whether it is a leap year. A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible by 400. So you can use the following Boolean expression to check whether a year is a leap year: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) LeapYear Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 15 Nested if Statements if (i > k) { if (j > k) cout << "i and j are greater than k"; } else cout << "i is less than or equal to k";

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 16 Multiple Alternative if Statements

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 17 Note The else clause matches the most recent if clause in the same block.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 18 Note, cont. Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) cout << "A"; } else cout << "B"; This statement prints B.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 19 TIP

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 20 CAUTION

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 21 Conditional Operator if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2 Ternary operator Binary operator Unary operator

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 22 Conditional Operator cout << ((num % 2 == 0) ? "num is even" : "num is odd");

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 23 Conditional Operator, cont. (booleanExp) ? exp1 : exp2

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 24 Operator Precedence How to evaluate * 4 > 5 * (4 + 3) – 1?

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 25 Operator Precedence F var++, var--  +, - (Unary plus and minus), ++var, --var F (type) Casting F ! (Not)  *, /, % (Multiplication, division, and remainder)  +, - (Binary addition and subtraction) , >= (Comparison)  ==, !=; (Equality) F && (Conditional AND) Short-circuit AND F || (Conditional OR) Short-circuit OR  =, +=, -=, *=, /=, %= (Assignment operator)

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 26 Operator Precedence Simplified When in doubt, use parentheses version 1: if (ch >= ‘A’ && ch <= ‘Z’) cout << “UC”; version 2: if ((ch >= ‘A’) && (ch <= ‘Z’)) cout << “UC”;