Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Similar documents
Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 3 Selection Statements

Selection Statements. Pseudocode

CONDITIONAL EXECUTION

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Selections. CSE 114, Computer Science 1 Stony Brook University

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Decision Making in C

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Boolean evaluation and if statements. Making decisions in programs

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 1 Java SE Programming

CS1150 Principles of Computer Science Boolean, Selection Statements

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

In this lab, you will learn more about selection statements. You will get familiar to

Lecture 5 Tao Wang 1

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 3 Tao Wang 1

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

VARIABLES & ASSIGNMENTS

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CHAPTER : 9 FLOW OF CONTROL

Chapter 3. More Flow of Control

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSC 1300 Exam 4 Comprehensive-ish and Structs

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

Relational & Logical Operators, if and switch Statements

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 4 - Notes Control Structures I (Selection)

bool bool - either true or false

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

EEL3834 Review Session September 13

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Add Subtract Multiply Divide

Introduction to Programming

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

CS349/SE382 A1 C Programming Tutorial

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Introduction. C provides two styles of flow control:

Chapter 2: Introduction to C++

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Checking Multiple Conditions

Exceptions, Case Study-Exception handling in C++.

CHAPTER 3 BASIC INSTRUCTION OF C++

Announcements. Lab 1 this week! Homework posted Thursday -Due next Thursday at 11:55pm

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

The following expression causes a divide by zero error:

BRANCHING if-else statements

Name Section: M/W T/TH Number Definition Matching (8 Points)

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

LECTURE 02 INTRODUCTION TO C++

Decisions. Arizona State University 1

C++ Program Flow Control: Selection

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 4: Control Structures I (Selection)

If Control Construct

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Control Structures in Java if-else and switch

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

The C++ Language. Arizona State University 1

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

Introduction to Object-Oriented Programming

CSCI 1061U Programming Workshop 2. C++ Basics

INTRODUCTION TO COMPUTER SCIENCE - LAB

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Control Structures in Java if-else and switch

Lecture 5: Making Decisions

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Chapter 2, Part III Arithmetic Operators and Decision Making

Transcription:

Chapter 3, Selection 1

The bool Type and Operators 2

One-way if Statements if (booleanexpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; cout << "The area for the circle of " << " radius " << radius << " is " << area; } 3

Examples Rewrite Listing 3.1 using an if-else statement TestBoolean Run 4

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. 5

The if...else Statement if (booleanexpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } 6

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"; 7

Multiple Alternative if Statements 8

Note The else clause matches the most recent if clause in the same block. 9

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. 10

TIP 11

Occam s Razor (see Google) 12

Common Errors in Selection Statements Common Error 1: Forgetting Necessary Braces 13

Common Errors in Selection Statements Mistakenly Using = for == if (count = 1) cout << "count is zero" << endl; else cout << "count is not zero" << endl; 14

Common Errors in Selection Statements Not an Error: Redundant Testing of Boolean Values 15

Logical Operators 16

Truth Table for Operator! 17

Truth Table for Operator && 18

Truth Table for Operator 19

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 20

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. 21

Examples Write 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 22

Problem: Lottery Randomly generates a lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rule: If the user input matches the lottery in exact order, the award is $10,000. If the user input matches the lottery, the award is $3,000. If one digit in the user input matches a digit in the lottery, the award is $1,000. Lottery 23

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 24

Conditional Operator cout << ((num % 2 == 0)? "num is even" : "num is odd"); 25

Conditional Operator, cont. (booleanexp)? exp1 : exp2 26

Formatting Output 27

Companion Website Enumerated Types enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; Once a type is defined, you can declare a variable of that type: Day day; The variable day can hold one of the values defined in the enumerated type. For example, the following statement assigns enumerated value MONDAY to variable day: day = MONDAY; 28

Companion Website Enumerated Types As with any other type, you can declare and initialize a variable in one statement: Day day = MONDAY; Furthermore, C++ allows you to declare an enumerated type and variable in one statement. For example, enum Day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY} day = MONDAY; TestEnumeratedType Run 29