Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Similar documents
Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

The following expression causes a divide by zero error:

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

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

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

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

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

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

All about flow control

Chapter 4 - Notes Control Structures I (Selection)

Chapter 2: Overview of C++

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

More Complex Versions of the if Statement. Class 13

Absolute C++ Walter Savitch

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

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

Quiz Determine the output of the following program:

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

Chapter 2: Introduction to C++

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

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

Chapter 2 Basic Elements of C++

Checking Multiple Conditions

Computer Programming : C++

If Control Construct

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

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.

Chapter 4: Making Decisions

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

To become familiar with array manipulation, searching, and sorting.

Chapter 4: Making Decisions

LECTURE 02 INTRODUCTION TO C++

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

LECTURE 04 MAKING DECISIONS

1) You want to determine whether time has run out. The following code correctly implements this:

Lecture # 6. Repetition. Review. If Else Statements Comparison Operators Boolean Expressions Nested Ifs. Switch Statements.

Boolean Algebra Boolean Algebra

CIS 130 Exam #2 Review Suggestions

5. Selection: If and Switch Controls

Expressions, Input, Output and Data Type Conversions

Programming. C++ Basics

Conditional Statement

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

BRANCHING if-else statements

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

These are notes for the third lecture; if statements and loops.

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

REVIEW. The C++ Programming Language. CS 151 Review #2

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Introduction to the C++ Programming Language

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

6.096 Introduction to C++ January (IAP) 2009

VARIABLES & ASSIGNMENTS

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CSCI 123 Introduction to Programming Concepts in C++

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Programming Language. Functions. Eng. Anis Nazer First Semester

The C++ Language. Arizona State University 1

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Introduction to Programming using C++

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

conditional statements

Chapter 3. More Flow of Control

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

5. Control Statements

Chapter 3. Selections

CS 31 Discussion 1A, Week 1. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Increment and the While. Class 15

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Unit 7. 'while' Loops

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

Computing and Statistical Data Analysis Lecture 3

Review. Relational Operators. The if Statement. CS 151 Review #4

UEE1302(1102) F10: Introduction to Computers and Programming

2 nd Week Lecture Notes

CT 229 Java Syntax Continued

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

C/C++ Programming Lecture 7 Name:

Problem Solving with C++

Add Subtract Multiply Divide

Full file at C How to Program, 6/e Multiple Choice Test Bank

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;

Why Is Repetition Needed?

Chapter Two MULTIPLE CHOICE

Week 1: Hello World! Muhao Chen

Chapter 2, Part III Arithmetic Operators and Decision Making

Relational Operators and if. Class 10

Transcription:

Lecture 5 Selection Statements Review from last week cin and cout directives escape sequences member functions formatting flags manipulators cout.width(20); cout.setf(ios::fixed); setwidth(20); 1

What we will learn about this week Simple Flow of Control Sequential Branching mechanisms If / Else Switch Program Style If you come to a fork in the road, take it. Yogi Berra Sequence 2

Branching (Selection) Chose between alternatives Calculate an employee s pay: pay = rate * 40 + 1.5 * rate * (hours 40) if the employee has worked overtime OR pay = rate * hours if the employee did not work overtime If Statement 3

If - Else Statement Syntax of the if else statement (See display 2.7 page 68 of book) if (Boolean_Expression) Yes_Statement; else No_Statement; if (Boolean_Expression) { yes_statement1; yes_statement2; } else { no_statement1; no_statement2; } 4

Comparison Operators == is equal to if(x == 6)!= is not equal to if(count!= 0) < is less than if(max < 100) <= is less than or equal to if(data <= 17) > is greater than if(left > 2) >= is greater than or equal to if(a>=b) (be careful not to use a single =) Should not have a space between the two symbols!!!! Comparison Operators (Display 2.8 page 69) 5

precedence (page 337) 1. unary operator +, -, ++, --,! 2. binary arithmetic operators *, /, % 3. binary arithmetic operators +, - 4. Boolean operators <, >, <=, >= 5. Boolean operators ==,!= 6. Boolean operators && 7. Boolean operators PITFALL: using = instead of == if (x = 12) else cout << x is equal to 12 ; cout << x is not equal to 12 ; The second expression is NEVER executed, regardless of the value of x before this statement is encountered. WORSE, after this if statement executes, the expression x = 12 HAS ASSIGNED the value 12 to x. Why? The expression x = 12 returns the value 12, which is converted to the bool value true, which is used by the if. 6

Boolean Expressions An expression that can be thought of as true or false Boolean expressions use!<>= &&, and evaluate to true and false Integers in Boolean Expressions: 0 is converted to false Any non-zero integer is converted to true if (integer_value) cout << Not zero ; else cout << Is zero ; Programming example (Display 2.6 page 67) #include <iostream> int main () { int hours; double gross_pay, rate; cout << Enter the hourly rate of pay: $ ; cin >> rate; cout << Enter the number of hours worked, \n << rounded to a whole number of hours: ; cin >> hours; if (hours > 40) gross_pay = rate*40 + 1.5*rate*(hours-40); else gross_pay = rate*hours; 7

if else continued cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << Hours = << hours << endl; cout << Hourly pay rate = $ << rate << endl; cout << Gross pay = $ << gross_pay << endl; } return 0; AND && 8

Boolean AND operator AND && if (a>b && count == 0) B1 B2 B1 && B2 False False False False True False True False False True True True OR 9

Boolean OR operator OR if (a>b count == 0) B1 B2 B1 B2 False False False False True True True False True True True True PITFALL: strings of inequalities In mathematics x < y < z is short hand for x < y && y < z. In C++, this is not true. It is still valid C++, but isn t what you expect from the mathematics. In C++ the precedence rules require x < y < z be evaluated like this: (x < y) < z The parenthesized expression returns a bool value. The < requires the same type on both sides. The bool value gets converted to the int value 0 (for false) or 1 (for true). Then 0 <z or 1 < z compiles. And gives (most of the time) a wrong answer! 10

Nested Ifs More readable if indentation rules are broken: 11

This format works because the conditions are mutually exclusive: Dangling Else if (fuel < 0.75) { if (fuel < 0.25) cout << Fuel low!\n ; } else cout Fuel over ¾ full.\n ; 12

Multi-way statements An If statement is a two-way branch. When you want three or four way branches, you can nest the If statements. A switch statement is another way to implement a multi-way branch. 13

Take a Break! 14

Programming Style (or how not to write confusing code) Clarity and Style Your goal is not just to get a program to work. You are graded on both program clarity and correctness: Comments are important! A correct header is important! Output identification is important! Spacing and Indentation are important! Clear and concise code is important! (KISS) 15

Programming Style Group like things together indent leave a blank line Comments // comment follows until end of line /* multi-line comment ends with */ Use Headers (I will no longer grade programs without names in source and output) Writing good code If the problem is complex, make sure you have a game plan (algorithm). Write one section of code at a time, test it, and when it works move on to the next section. Comment the main sections of code, then after the program is done, go back and add additional comments to clarify what you are doing. Remember to use blank lines to group like statements together. Proper indentation also makes the code more legible. 16

17

18

19

20

21

Re-submit Programs Remember that you can re-submit your program and I will average the two grades as the final grade for the project. Turn in both the original and updated programs when you re-submit. Remember to change the header on the updated program. Turn the programs in to the re-submit folder! 22

Reading Assignment Sections 3.1, 3.2, 3.3 (pg 99-117) Labs In Class: Room Capacity Programming Project #6 page 95 Simple Version Runs Once Take Home: Days of the Week 23