chapter 1 starting out with c++: early objects 5/e slide 1 © 2006 pearson education. all rights...

124
Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Upload: marilynn-dixon

Post on 06-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Chapter 1 Starting Out with C++: Early Objects 5/e Slide 3 © 2006 Pearson Education. All Rights Reserved Input and Output cout cin

TRANSCRIPT

Page 1: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 1

© 2006 Pearson Education. All Rights Reserved

Midterm

Wednesday (Oct. 16, 2013)Class room

Page 2: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 2

© 2006 Pearson Education. All Rights Reserved

Basic Expression and Types• Arithmetic expression: +,-,*,/, %

• Condition expression ==, !=, <, <=, >=, >,||, &&,!

• Basic types: int, float, double, char

Page 3: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 3

© 2006 Pearson Education. All Rights Reserved

Input and Output

• cout

• cin

Page 4: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 4

© 2006 Pearson Education. All Rights Reserved

Basic Statements

• Assignments a=b; a++; ++a; a--; --a;• If/else• switch • while loop• for loop• break• continue

Page 5: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 5

© 2006 Pearson Education. All Rights Reserved

Main Hardware Component Categories

InputDevice

OutputDevice

SecondaryStorageDevices

CentralProcessing

Unit

MainMemory

Page 6: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 6

© 2006 Pearson Education. All Rights Reserved

From a High-level Program to an Executable File

Object Code

Linker

Executable Code

Source Code

Preprocessor

ModifiedSource Code

Compiler

Page 7: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 7

© 2006 Pearson Education. All Rights Reserved

Special CharactersCharacter Name Description

// Double Slash Begins a comment

# Pound Sign Begins preprocessor directive

< > Open, Close Brackets Encloses filename with #include directive

( ) Open, Close Parentheses Used when naming function

{ } Open, Close Braces Encloses a group of statements

" " Open, Close Quote Marks Encloses string of characters

; Semicolon Ends a programming statement

Page 8: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 8

© 2006 Pearson Education. All Rights Reserved

2.2 The cout Object

• Displays information on computer screen• Use << to send information to cout

cout << "Hello, there!";• Can use << to send > 1 item to cout

cout << "Hello, " << "there!";Or cout << "Hello, "; cout << "there!";

Page 9: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 9

© 2006 Pearson Education. All Rights Reserved

The cin Object

• User input goes from keyboard to the input buffer, where it is stored as characters

• cin converts the data to the type that matches the variable int height;cout << "How tall is the room? ";cin >> height;

Page 10: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 10

© 2006 Pearson Education. All Rights Reserved

2.5 Variables and Constants

• Variable– Has a name and a type of data it can hold

char letter;

– Is used to reference a location in memory where a value can be stored

– This value can be changed (i.e. can “vary”)

variable namedata type

Page 11: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 11

© 2006 Pearson Education. All Rights Reserved

Variables

– If a new value is stored in the variable, it replaces the previous value

– The previous value is overwritten and can no longer be retrieved

int age; age = 17; // age is 17 cout << age; // Displays 17 age = 18; // Now age is 18 cout << age; // Displays 18

Page 12: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 12

© 2006 Pearson Education. All Rights Reserved

Constants• Constant

– Data item whose value does not change during program execution

– Constants are also called literals

'A' // character constant "Hello" // string constant 12 // integer constant 3.14 // floating-point constant

Page 13: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 13

© 2006 Pearson Education. All Rights Reserved

2.5 Assignment Statement

• Uses the = operator• Has a single variable on the left side

and a value on the right side• Copies the value on the right into the

variable on the left item = 12;

Page 14: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 14

© 2006 Pearson Education. All Rights Reserved

Defining Variables• Variables of the same type can be defined

- In separate statements int length; int width;- Or in the same statement int length, width;

• Variables of different types must defined in different statements

Page 15: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 15

© 2006 Pearson Education. All Rights Reserved

Integral Constants

• To store an integer constant in a long memory location, put ‘L’ at the end of the number: 1234L

• Constants that begin with ‘0’ (zero) are base 8: 075

• Constants that begin with ‘0x’ are base 16: 0x75A

Page 16: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 16

© 2006 Pearson Education. All Rights Reserved

2.8 The char Data Type

• Used to hold single characters or very small integer values

• Usually 1 byte of memory• A numeric value representing the character

is stored in memory

CODE MEMORY char letter = 'C'; letter 67

Page 17: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 17

© 2006 Pearson Education. All Rights Reserved

Assigning Floating-point Values to Integer Variables

• If a floating-point value is assigned to an integer variable– The fractional part will be truncated (i.e.,

“chopped off” and discarded)– The value is not roundedint rainfall = 3.88; cout << rainfall; // Displays 3

Page 18: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 18

© 2006 Pearson Education. All Rights Reserved

• Represents values that are true or false• bool variables are stored as short integers

• false is represented by 0, true by 1 bool allDone = true; bool finished = false;

2.11 The bool Data Type

allDone finished

1 0

Page 19: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 19

© 2006 Pearson Education. All Rights Reserved

Binary Arithmetic Operators

SYMBOL OPERATION EXAMPLE ans+ addition ans = 7 + 3; 10

- subtraction ans = 7 - 3; 4

* multiplication ans = 7 * 3; 21

/ division ans = 7 / 3; 2

% modulus ans = 7 % 3; 1

Page 20: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 20

© 2006 Pearson Education. All Rights Reserved

Using Mathematical Expressions

• Can be used in assignment statements, with cout, and in other types of statements

• Examples:

border = 2 * PI * radius;cout << "border is: " << (2*(l+w));

This is an expression

These are expressions

Page 21: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 21

© 2006 Pearson Education. All Rights Reserved

Order of Operations

In an expression with > 1 operator, evaluate in this order - (unary negation) in order, left to right

* / % in order, left to right

+ - in order, left to right

In the expression 2 + 2 * 2 – 2 ,

Do first

Do last

Do next

Evaluate 1st

Evaluate 2nd

Evaluate 3rd

Page 22: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 22

© 2006 Pearson Education. All Rights Reserved

Associativity of Operators

• - (unary negation) associates right to left• * / % + - all associate left to right• parentheses ( ) can be used to override the

order of operations 2 + 2 * 2 – 2 = 4 (2 + 2) * 2 – 2 = 6 2 + 2 * (2 – 2) = 2 (2 + 2) * (2 – 2) = 0

Page 23: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 23

© 2006 Pearson Education. All Rights Reserved

Stream Manipulators

• Used to control features of an output field

• Some affect just the next value displayed–setw(x): Print in a field at least x spaces

wide. Use more spaces if specified field width is not big enough.

Page 24: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 24

© 2006 Pearson Education. All Rights Reserved

Stream Manipulators• Some affect values until changed again

– fixed: Use decimal notation (not E-notation) for floating-point values.

– setprecision(x): • When used with fixed, print floating-point value using x

digits after the decimal.• Without fixed, print floating-point value using x significant

digits.– showpoint: Always print decimal for floating-point

values. – left, right: left-, right justification of value

Page 25: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 25

© 2006 Pearson Education. All Rights Reserved

Open the File3. Open the file• Use the open member function

inFile.open("inventory.dat");outFile.open("report.txt");

• Filename may include drive, path info.• Output file will be created if necessary;

existing output file will be erased first• Input file must exist for open to work

Page 26: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 26

© 2006 Pearson Education. All Rights Reserved

Use the File

4. Use the file• Can use output file object and << to send

data to a file outFile << "Inventory report";

• Can use input file object and >> to copy data from file to variables inFile >> partNum; inFile >> qtyInStock >> qtyOnOrder;

Page 27: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 27

© 2006 Pearson Education. All Rights Reserved

Close the File

5. Close the file• Use the close member function

inFile.close();outFile.close();

• Don’t wait for operating system to close files at program end

– May be limit on number of open files– May be buffered output data waiting to be

sent to a file

Page 28: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 28

© 2006 Pearson Education. All Rights Reserved

4.1 Relational Operators• Used to compare numbers to determine

relative order• Operators:

> Greater than< Less than>= Greater than or equal to<= Less than or equal to== Equal to!= Not equal to

Page 29: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 29

© 2006 Pearson Education. All Rights Reserved

Relational Expressions• Relational expressions are Boolean

(i.e., evaluate to true or false)• Examples:

12 > 5 is true7 <= 5 is false

if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

Page 30: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 30

© 2006 Pearson Education. All Rights Reserved

Format of the if Statement if (expression){ statement1; statement2; … statementn;}

The block inside the braces is called the body of the if statement. If there is only 1 statement in the body, the { } may be omitted.

No ; goes here

; goes here

Page 31: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 31

© 2006 Pearson Education. All Rights Reserved

Example if Statements

if (score >= 60) cout << "You passed.\n";

if (score >= 90){ grade = 'A'; cout << "Wonderful job!\n";}

Page 32: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 32

© 2006 Pearson Education. All Rights Reserved

if Statement Flow of Control

expression

1 or morestatements

true false

Page 33: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 33

© 2006 Pearson Education. All Rights Reserved

Example if Statements

if (score >= 60) cout << "You passed.\n";

if (score >= 90){ grade = 'A'; cout << "Wonderful job!\n";}

Page 34: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 34

© 2006 Pearson Education. All Rights Reserved

4.3 The if/else Statement• Allows a choice between statements

depending on whether (expression) is true or false

• Format: if (expression) { statement set 1; } else { statement set 2; }

Page 35: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 35

© 2006 Pearson Education. All Rights Reserved

How the if/else Works

• If (expression) is true, statement set 1 is executed and statement set 2 is skipped.

• If (expression) is false, statement set 1 is skipped and statement set 2 is executed.

Page 36: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 36

© 2006 Pearson Education. All Rights Reserved

if/else Flow of Control

expression

statement set 1

true false

statement set 2

Page 37: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 37

© 2006 Pearson Education. All Rights Reserved

Example if/else Statements if (score >= 60) cout << "You passed.\n"; else cout << "You did not pass.\n";

if (intRate > 0) { interest = loanAmt * intRate; cout << interest; } else cout << "You owe no interest.\n";

Page 38: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 38

© 2006 Pearson Education. All Rights Reserved

4.7 Logical Operators

• Used to create relational expressions from other relational expressions

Operators, Meaning, and Explanation && AND New relational expression is true if

both expressions are true || OR New relational expression is true if

either expression is true

! NOTReverses the value of an expression; true expression becomes false, and false becomes true

Page 39: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 39

© 2006 Pearson Education. All Rights Reserved

Logical Precedence

Highest ! && Lowest ||

Example: (2 < 3) || (5 > 6) && (7 > 8)

is true because AND is done before OR

Page 40: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 40

© 2006 Pearson Education. All Rights Reserved

More on Precedence

Example:

8 < 2 + 7 || 5 == 6 is true

Highest arithmetic operatorsrelational operators

Lowest logical operators

Page 41: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 41

© 2006 Pearson Education. All Rights Reserved

switch Statement Format

switch (expression){ case exp1: statement set 1;case exp2: statement set 2;...case expn: statement set n;default: statement set n+1;

}

Page 42: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 42

© 2006 Pearson Education. All Rights Reserved

The break Statement

• Used to stop execution in the current block

• Also used to exit a switch statement

• Useful to execute a single case statement without executing statements following it

Page 43: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 43

© 2006 Pearson Education. All Rights Reserved

Example switch Statement

switch (gender){ case 'f': cout << "female"; break; case 'm': cout << "male"; break; default : cout << "invalid gender";}

Page 44: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 44

© 2006 Pearson Education. All Rights Reserved

How the while Loop Works

while (expression){ statement(s);}

• expression is evaluated– if it is true, the statement(s) are

executed, and then expression is evaluated again

– if it is false, the loop is exited

Page 45: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 45

© 2006 Pearson Education. All Rights Reserved

while Loop Flow of Control

true

statement(s)

falsecondition

Page 46: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 46

© 2006 Pearson Education. All Rights Reserved

while Loop Example

int val = 5;while (val >= 0){ cout << val << " "; val--;}

• produces output:5 4 3 2 1 0

Page 47: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 47

© 2006 Pearson Education. All Rights Reserved

5.8 The do-while and for Loops

• do-while: a posttest loop (expression is evaluated after the loop executes)

• Format:do{ 1 or more statements; } while (expression);

Notice the required ;

Page 48: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 48

© 2006 Pearson Education. All Rights Reserved

do-while Flow of Control

statement(s)

condition

false

true

Page 49: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 49

© 2006 Pearson Education. All Rights Reserved

The for Loop• Pretest loop that executes zero or more times• Useful for counter-controlled loop

• Format:for( initialization; test; update )

{ 1 or more statements; } No ;

goes here

Required ;

Page 50: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 50

© 2006 Pearson Education. All Rights Reserved

for Loop Example

int sum = 0, num;for (num = 1; num <= 10; num++)

sum += num;cout << "Sum of numbers 1 – 10 is "

<< sum << endl;

Page 51: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 51

© 2006 Pearson Education. All Rights Reserved

for Loop Flow of Control

true

statement(s)

falsetest

initialization code

updatecode

Page 52: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 52

© 2006 Pearson Education. All Rights Reserved

5.11 Breaking Out of a Loop

• Can use break to terminate execution of a loop

• Use sparingly if at all – makes code harder to understand

• When used in an inner loop, terminates that loop only and goes back to outer loop

Page 53: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 53

© 2006 Pearson Education. All Rights Reserved

5.12 The continue Statement

• Can use continue to go to end of loop and prepare for next repetition– while and do-while loops go to test

and repeat the loop if test condition is true– for loop goes to update step, then tests,

and repeats loop if test condition is true

• Use sparingly – like break, can make program logic hard to follow

Page 54: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 54

© 2006 Pearson Education. All Rights Reserved

Prefix Mode

• ++val and --val increment or decrement the variable, then return the new value of the variable.

• It is this returned new value of the variable that is used in any other operations within the same statement

Page 55: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 55

© 2006 Pearson Education. All Rights Reserved

Postfix Mode

• val++ and val-- return the old value of the variable, then increment or decrement the variable

• It is this returned old value of the variable that is used in any other operations within the same statement

Page 56: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 56

© 2006 Pearson Education. All Rights Reserved

4.10 More About Variable Definitions and Scope

• Scope of a variable is the block in which it is defined, from the point of definition to the end of the block

• Usually defined at beginning of function• May be defined close to first use

Page 57: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 57

© 2006 Pearson Education. All Rights Reserved

More About Variable Definitions and Scope

• Variables defined inside { } have local or block scope

• When in a block nested inside another block, you can define variables with the same name as in the outer block. – When in the inner block, the outer definition

is not available– Not a good idea

Page 58: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 58

© 2006 Pearson Education. All Rights Reserved

6.1 Modular Programming

• Modular programming: breaking a program up into smaller, manageable functions or modules

• Function: a collection of statements to perform a task

• Motivation for modular programming – Improves maintainability of programs– Simplifies the process of writing programs

Page 59: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 59

© 2006 Pearson Education. All Rights Reserved

6.2 Defining and Calling Functions

• Function call: statement that causes a function to execute

• Function definition: statements that make up a function

Page 60: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 60

© 2006 Pearson Education. All Rights Reserved

#include <iostream>using namespace std;

void displayMessage(){cout << “From the function displayMessage.\n";

}int main(){

cout << "Hello from main.\n";displayMessage(); // Call displayMessagecout << "Back in function main again.\n";return 0;

}

Page 61: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 61

© 2006 Pearson Education. All Rights Reserved

Function Definition• Definition includes

return type: data type of the value the function returns to the part of the program that called it

name: name of the function. Function names follow same rules as variable names

parameter list: variables that hold the values passed to the function

body: statements that perform the function’s task

Page 62: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 62

© 2006 Pearson Education. All Rights Reserved

Function Return Type• If a function returns a value, the type of the

value must be indicatedint main()

• If a function does not return a value, its return type is voidvoid printHeading(){ cout << "\tMonthly Sales\n";}

Page 63: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 63

© 2006 Pearson Education. All Rights Reserved

Calling a Function

• To call a function, use the function name followed by () and ;printHeading();

• When a function is called, the program executes the body of the function

• After the function terminates, execution resumes in the calling function at the point of call

Page 64: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 64

© 2006 Pearson Education. All Rights Reserved

#include <iostream>using namespace std;int evenOrOdd(int n){

return n%2;}int main(){ int n;

cin>>n;if (evenOrOdd(n)==1) cout<<“Odd”;

else cout << “Even";return 0;

}

Page 65: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 65

© 2006 Pearson Education. All Rights Reserved

6.3 Function Prototypes

• Ways to notify the compiler about a function before a call to the function – Place function definition before calling

function’s definition– Use a function prototype (similar to the

heading of the function• Heading: void printHeading()• Prototype: void printHeading();

Page 66: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 66

© 2006 Pearson Education. All Rights Reserved

Prototype Notes

• Place prototypes near top of program • Program must include either prototype or

full function definition before any call to the function, otherwise a compiler error occurs

• When using prototypes, function definitions can be placed in any order in the source file (Traditionally, main is placed first)

Page 67: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 67

© 2006 Pearson Education. All Rights Reserved

6.4 Sending Data into a Function

• Can pass values into a function at time of callc = sqrt(a*a + b*b);

• Values passed to function are arguments• Variables in function that hold values passed

as arguments are parameters• Alternate names:

– argument: actual argument, actual parameter– parameter: formal argument, formal parameter

Page 68: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 68

© 2006 Pearson Education. All Rights Reserved

#include <iostream>using namespace std;void displayValue(int); // Function prototype

int main(){cout << "I am passing 5 to displayValue.\n";displayValue(5) // Call displayValue with argument 5cout << "Now I am back in main.\n";

return 0;}

void displayValue(int num){cout << "The value is " << num << endl;

}

Page 69: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 69

© 2006 Pearson Education. All Rights Reserved

Parameters, Prototypes, and Function Headings

• For each function argument,– the prototype must include the data type of each

parameter in its () void evenOrOdd(int); //prototype– the heading must include a declaration, with variable

type and name, for each parameter in its () void evenOrOdd(int num) //heading

• The function call for the above function would look like this: evenOrOdd(val); //call

Page 70: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 70

© 2006 Pearson Education. All Rights Reserved

Function Call Notes• Value of argument is copied into parameter

when the function is called• Function can have > 1 parameter• There must be a data type listed in the

prototype () and an argument declaration in the function heading () for each parameter

• Arguments will be promoted/demoted as necessary to match parameters

Page 71: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 71

© 2006 Pearson Education. All Rights Reserved

Calling Functions with Multiple Arguments Illustration

displayData(height, weight); // call

void displayData(int h, int w)// heading{ cout << "Height = " << h << endl; cout << "Weight = " << w << endl;}

Page 72: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 6 Starting Out with C++: Early Objects 5/eslide 72

© 2006 Pearson Education. All Rights Reserved

Calling Functions with Multiple Arguments

When calling a function with multiple arguments– the number of arguments in the call must

match the function prototype and definition– the first argument will be copied into the

first parameter, the second argument into the second parameter, etc.

Page 73: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 73

© 2006 Pearson Education. All Rights Reserved

Sample Problems in Midterm

True/FalseIndicate whether the sentence or

statement is true or false.____ 1. Suppose P and Q are logical

expressions. The logical expression P && Q is true if both P and Q are true.

Page 74: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 74

© 2006 Pearson Education. All Rights Reserved

Sample Problems in Midterm____ 56. The value of the expression 26 + 14 /

3 + 1 is _____.A)10 B)14 C)29 D)31

____ 55. The value of the expression 26 – 14 % 3 + 1 is _____.

A)0 B)1 C)24 D)25

Page 75: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 75

© 2006 Pearson Education. All Rights Reserved

Problem (True/False)

n=1while (n<5){ cout<<‘n’<<“ “; n++;}____ 22. Assume all variables are properly

declared. The output of the C++ code above is 1 2 3 4.

Page 76: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 76

© 2006 Pearson Education. All Rights Reserved

Answer

• ‘n’ mean a character• It only prints out n n n n• The choice is false

Page 77: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 77

© 2006 Pearson Education. All Rights Reserved

Problem (Multiple Choice)

num=100;while (num<=200) num=num+3;cout<num;____ 62. Assume all variables are properly

declared. What is the output of the C++ code above?

A)200 B)202 C)203 D)204

Page 78: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 78

© 2006 Pearson Education. All Rights Reserved

Answer

• Num changes from 100, 103, …, 130, 160, 190, 193, 196, 199, 202

• The right choice is B.

Page 79: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 79

© 2006 Pearson Education. All Rights Reserved

Sample Problems

• True/False

• Multiple Choice

Page 80: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 80

© 2006 Pearson Education. All Rights Reserved

Problem

• ____2. The expression !(x > 10) is equivalent to the expression x < 10.

Page 81: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 81

© 2006 Pearson Education. All Rights Reserved

4.1 Relational Operators• Used to compare numbers to determine

relative order• Operators:

> Greater than< Less than>= Greater than or equal to<= Less than or equal to== Equal to!= Not equal to

Page 82: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 82

© 2006 Pearson Education. All Rights Reserved

Relational Expressions• Relational expressions are Boolean

(i.e., evaluate to true or false)• Examples:

12 > 5 is true7 <= 5 is false

if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

Page 83: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 83

© 2006 Pearson Education. All Rights Reserved

Answer

• False

Page 84: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 84

© 2006 Pearson Education. All Rights Reserved

Problem

• ____4. Suppose found = true and num = 6. The value of the expression (!found) || (num > 6) is false.

Page 85: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 85

© 2006 Pearson Education. All Rights Reserved

4.7 Logical Operators

• Used to create relational expressions from other relational expressions

Operators, Meaning, and Explanation && AND New relational expression is true if

both expressions are true || OR New relational expression is true if

either expression is true

! NOTReverses the value of an expression; true expression becomes false, and false becomes true

Page 86: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 86

© 2006 Pearson Education. All Rights Reserved

Logical Precedence

Highest ! && Lowest ||

Example: (2 < 3) || (5 > 6) && (7 > 8)

is true because AND is done before OR

Page 87: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 87

© 2006 Pearson Education. All Rights Reserved

Answer

• True

• The value is false.

Page 88: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 88

© 2006 Pearson Education. All Rights Reserved

Problem

• ____15. When a while loop terminates, the control first goes back to the statement just before the while statement and then the control goes to the statement immediately following the while loop.

Page 89: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 89

© 2006 Pearson Education. All Rights Reserved

How the while Loop Works

while (expression){ statement(s);}

• expression is evaluated– if it is true, the statement(s) are

executed, and then expression is evaluated again

– if it is false, the loop is exited

Page 90: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 90

© 2006 Pearson Education. All Rights Reserved

while Loop Flow of Control

true

statement(s)

falsecondition

Page 91: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 91

© 2006 Pearson Education. All Rights Reserved

while Loop Example

int val = 5;while (val >= 0){ cout << val << " "; val--;}

• produces output:5 4 3 2 1 0

Page 92: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 92

© 2006 Pearson Education. All Rights Reserved

Answer

• False

Page 93: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 93

© 2006 Pearson Education. All Rights Reserved

Problem

• ____17. It is possible that the body of a while loop may not execute at all, but the body of a for loop executes at least once.

Page 94: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 94

© 2006 Pearson Education. All Rights Reserved

The for Loop• Pretest loop that executes zero or more times• Useful for counter-controlled loop

• Format:for( initialization; test; update )

{ 1 or more statements; } No ;

goes here

Required ;

Page 95: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 95

© 2006 Pearson Education. All Rights Reserved

for Loop Example

int sum = 0, num;for (num = 1; num <= 10; num++)

sum += num;cout << "Sum of numbers 1 – 10 is "

<< sum << endl;

Page 96: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 96

© 2006 Pearson Education. All Rights Reserved

for Loop Flow of Control

true

statement(s)

falsetest

initialization code

updatecode

Page 97: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 97

© 2006 Pearson Education. All Rights Reserved

Answer

• False

Page 98: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 98

© 2006 Pearson Education. All Rights Reserved

Problem

• ____18. Both while and for loops are pre-test loops.

Page 99: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 99

© 2006 Pearson Education. All Rights Reserved

while Loop Flow of Control

true

statement(s)

falsecondition

Page 100: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 100

© 2006 Pearson Education. All Rights Reserved

for Loop Flow of Control

true

statement(s)

falsetest

initialization code

updatecode

Page 101: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 101

© 2006 Pearson Education. All Rights Reserved

Answer

• True

Page 102: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 102

© 2006 Pearson Education. All Rights Reserved

• ____20. Consider the while loop above. If the continue statement executes, counter is set to 0.

Page 103: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 103

© 2006 Pearson Education. All Rights Reserved

5.12 The continue Statement

• Can use continue to go to end of loop and prepare for next repetition– while and do-while loops go to test

and repeat the loop if test condition is true– for loop goes to update step, then tests,

and repeats loop if test condition is true

• Use sparingly – like break, can make program logic hard to follow

Page 104: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 104

© 2006 Pearson Education. All Rights Reserved

Answer

• False

Page 105: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 105

© 2006 Pearson Education. All Rights Reserved

• ____22. Assume all variables are properly declared. The output of the C++ code above is 1 2 3 4.

Page 106: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 106

© 2006 Pearson Education. All Rights Reserved

Answer

• False

Page 107: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 107

© 2006 Pearson Education. All Rights Reserved

• ____ 24. Assume all variables are properly declared. The output of the C++ code above is Stop.

Page 108: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 108

© 2006 Pearson Education. All Rights Reserved

Answer

• True

Page 109: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 109

© 2006 Pearson Education. All Rights Reserved

• ____33. The execution of a C++ program always begins with the function main.

Page 110: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 110

© 2006 Pearson Education. All Rights Reserved

Answer

• True

Page 111: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 111

© 2006 Pearson Education. All Rights Reserved

• ____38. Suppose that x is an int variable. Which of the following expressions always evaluates to true?

• A)(x > 0) || ( x <= 0)• B)(x >= 0) || (x == 0)• C)(x > 0) && ( x <= 0)• D)(x > 0) && (x == 0)

Page 112: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 112

© 2006 Pearson Education. All Rights Reserved

Answer

• A

Page 113: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 113

© 2006 Pearson Education. All Rights Reserved

• ____ 40. What is the output of the C++ code above?

• A)One• B)Two• C)One Two• D)Two One

Page 114: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 114

© 2006 Pearson Education. All Rights Reserved

Answer

• B

Page 115: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 115

© 2006 Pearson Education. All Rights Reserved

• ____49. Which of the following operators has the lowest precedence?

• A)!• C)&&• B)||• D)=

Page 116: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 116

© 2006 Pearson Education. All Rights Reserved

Logical Precedence

Highest ! && Lowest ||

Example: (2 < 3) || (5 > 6) && (7 > 8)

is true because AND is done before OR

Page 117: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 117

© 2006 Pearson Education. All Rights Reserved

More on Precedence

Example:

8 < 2 + 7 || 5 == 6 is true

Highest arithmetic operatorsrelational operators

Lowest logical operators

Page 118: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 118

© 2006 Pearson Education. All Rights Reserved

Answer

• D

Page 119: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 119

© 2006 Pearson Education. All Rights Reserved

• ____ 50. What is the output of the C++ code above?

• A)24 0• C)25 0• B)24 1• D)25 1

Page 120: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 120

© 2006 Pearson Education. All Rights Reserved

Answer

• D

Page 121: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 121

© 2006 Pearson Education. All Rights Reserved

____ 55. Which of the following is true about a do...while loop?

• A)The body of the loop is executed at least once.

• B)The logical expression controlling the loop is evaluated before the loop is entered.

• C)The body of the loop may not execute at all.• D)It cannot contain break.

Page 122: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 122

© 2006 Pearson Education. All Rights Reserved

Answer

• A

Page 123: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 123

© 2006 Pearson Education. All Rights Reserved

____ 62. Assume all variables are properly declared. What is the output of the C++ code above?

• A)200• C)203• B)202• D)204

Page 124: Chapter 1 Starting Out with C++: Early Objects 5/e Slide 1 © 2006 Pearson Education. All Rights Reserved Midterm Wednesday (Oct. 16, 2013) Class room

Chapter 1 Starting Out with C++: Early Objects 5/eSlide 124

© 2006 Pearson Education. All Rights Reserved

Answer

• B

It reaches 103, …, 190, 193, 196, 199, 202