Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 2 © 2006 Pearson Education. All Rights Reserved Basic Expression and Types Arithmetic expression: +,-,*,/, % Condition expression ==, !=, =, >,||, &&,! Basic types: int, float, double, char

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

4 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

5 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 5 © 2006 Pearson Education. All Rights Reserved Main Hardware Component Categories Input Device Output Device Secondary Storage Devices Central Processing Unit Main Memory

6 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 6 © 2006 Pearson Education. All Rights Reserved From a High-level Program to an Executable File Object Code Linker Executable Code Source Code Preprocessor Modified Source Code Compiler

7 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 7 © 2006 Pearson Education. All Rights Reserved Special Characters CharacterNameDescription // Double SlashBegins a comment # Pound SignBegins preprocessor directive Open, Close BracketsEncloses filename with #include directive ( ) Open, Close ParenthesesUsed when naming function { } Open, Close BracesEncloses a group of statements " Open, Close Quote MarksEncloses string of characters ; SemicolonEnds a programming statement

8 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 1 item to cout cout << "Hello, " << "there!"; Or cout << "Hello, "; cout << "there!";

9 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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;

10 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 name data type

11 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

12 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

13 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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;

14 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

15 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

16 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

17 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 rounded int rainfall = 3.88; cout << rainfall; // Displays 3

18 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 10

19 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 19 © 2006 Pearson Education. All Rights Reserved Binary Arithmetic Operators SYMBOLOPERATIONEXAMPLE 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

20 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

21 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

22 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

23 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

24 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

25 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 25 © 2006 Pearson Education. All Rights Reserved Open the File 3.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

26 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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;

27 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

28 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

29 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 29 © 2006 Pearson Education. All Rights Reserved Relational Expressions Relational expressions are Boolean (i.e., evaluate to true or false) Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

30 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

31 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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"; }

32 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 32 © 2006 Pearson Education. All Rights Reserved if Statement Flow of Control expression 1 or more statements true false

33 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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"; }

34 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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; }

35 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

36 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 36 © 2006 Pearson Education. All Rights Reserved if/else Flow of Control expression statement set 1 true false statement set 2

37 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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";

38 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 ! NOT Reverses the value of an expression; true expression becomes false, and false becomes true

39 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 39 © 2006 Pearson Education. All Rights Reserved Logical Precedence Highest ! && Lowest || Example: (2 6) && (7 > 8) is true because AND is done before OR

40 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 40 © 2006 Pearson Education. All Rights Reserved More on Precedence Example: 8 < 2 + 7 || 5 == 6 is true Highestarithmetic operators relational operators Lowest logical operators

41 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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; }

42 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

43 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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"; }

44 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

45 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 45 © 2006 Pearson Education. All Rights Reserved while Loop Flow of Control true statement(s) false condition

46 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

47 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 ;

48 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 48 © 2006 Pearson Education. All Rights Reserved do-while Flow of Control statement(s) condition false true

49 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 ;

50 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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;

51 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 51 © 2006 Pearson Education. All Rights Reserved for Loop Flow of Control true statement(s) false test initialization code update code

52 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

53 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

54 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

55 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

56 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

57 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

58 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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

59 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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

60 Chapter 6 Starting Out with C++: Early Objects 5/e slide 60 © 2006 Pearson Education. All Rights Reserved #include using namespace std; void displayMessage(){ cout << “From the function displayMessage.\n"; } int main(){ cout << "Hello from main.\n"; displayMessage(); // Call displayMessage cout << "Back in function main again.\n"; return 0; }

61 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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

62 Chapter 6 Starting Out with C++: Early Objects 5/e slide 62 © 2006 Pearson Education. All Rights Reserved Function Return Type If a function returns a value, the type of the value must be indicated int main() If a function does not return a value, its return type is void void printHeading() { cout << "\tMonthly Sales\n"; }

63 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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

64 Chapter 6 Starting Out with C++: Early Objects 5/e slide 64 © 2006 Pearson Education. All Rights Reserved #include 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; }

65 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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() ;

66 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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)

67 Chapter 6 Starting Out with C++: Early Objects 5/e slide 67 © 2006 Pearson Education. All Rights Reserved 6.4 Sending Data into a Function Can pass values into a function at time of call c = 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

68 Chapter 6 Starting Out with C++: Early Objects 5/e slide 68 © 2006 Pearson Education. All Rights Reserved #include using namespace std; void displayValue(int); // Function prototype int main(){ cout << "I am passing 5 to displayValue.\n"; displayValue(5) // Call displayValue with argument 5 cout << "Now I am back in main.\n"; return 0; } void displayValue(int num){ cout << "The value is " << num << endl; }

69 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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

70 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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

71 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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; }

72 Chapter 6 Starting Out with C++: Early Objects 5/e slide 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.

73 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 73 © 2006 Pearson Education. All Rights Reserved Sample Problems in Midterm True/False Indicate 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.

74 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

75 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 75 © 2006 Pearson Education. All Rights Reserved Problem (True/False) n=1 while (n<5){ cout<<‘n’<<“ “; n++; } ____22.Assume all variables are properly declared. The output of the C++ code above is 1 2 3 4.

76 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 76 © 2006 Pearson Education. All Rights Reserved Answer ‘n’ mean a character It only prints out n n n n The choice is false

77 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

78 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

79 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 79 © 2006 Pearson Education. All Rights Reserved Sample Problems True/False Multiple Choice

80 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 80 © 2006 Pearson Education. All Rights Reserved Problem ____2.The expression !(x > 10) is equivalent to the expression x < 10.

81 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

82 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 82 © 2006 Pearson Education. All Rights Reserved Relational Expressions Relational expressions are Boolean (i.e., evaluate to true or false) Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

83 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 83 © 2006 Pearson Education. All Rights Reserved Answer False

84 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

85 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 ! NOT Reverses the value of an expression; true expression becomes false, and false becomes true

86 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 86 © 2006 Pearson Education. All Rights Reserved Logical Precedence Highest ! && Lowest || Example: (2 6) && (7 > 8) is true because AND is done before OR

87 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 87 © 2006 Pearson Education. All Rights Reserved Answer True The value is false.

88 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

89 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

90 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 90 © 2006 Pearson Education. All Rights Reserved while Loop Flow of Control true statement(s) false condition

91 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

92 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 92 © 2006 Pearson Education. All Rights Reserved Answer False

93 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

94 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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 ;

95 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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;

96 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 96 © 2006 Pearson Education. All Rights Reserved for Loop Flow of Control true statement(s) false test initialization code update code

97 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 97 © 2006 Pearson Education. All Rights Reserved Answer False

98 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 98 © 2006 Pearson Education. All Rights Reserved Problem ____18.Both while and for loops are pre-test loops.

99 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 99 © 2006 Pearson Education. All Rights Reserved while Loop Flow of Control true statement(s) false condition

100 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 100 © 2006 Pearson Education. All Rights Reserved for Loop Flow of Control true statement(s) false test initialization code update code

101 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 101 © 2006 Pearson Education. All Rights Reserved Answer True

102 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 102 © 2006 Pearson Education. All Rights Reserved ____20.Consider the while loop above. If the continue statement executes, counter is set to 0.

103 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

104 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 104 © 2006 Pearson Education. All Rights Reserved Answer False

105 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

106 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 106 © 2006 Pearson Education. All Rights Reserved Answer False

107 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 107 © 2006 Pearson Education. All Rights Reserved ____24.Assume all variables are properly declared. The output of the C++ code above is Stop.

108 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 108 © 2006 Pearson Education. All Rights Reserved Answer True

109 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 109 © 2006 Pearson Education. All Rights Reserved ____33.The execution of a C++ program always begins with the function main.

110 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 110 © 2006 Pearson Education. All Rights Reserved Answer True

111 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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)

112 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 112 © 2006 Pearson Education. All Rights Reserved Answer A

113 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

114 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 114 © 2006 Pearson Education. All Rights Reserved Answer B

115 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 115 © 2006 Pearson Education. All Rights Reserved ____49.Which of the following operators has the lowest precedence? A)! C)&& B)|| D)=

116 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 116 © 2006 Pearson Education. All Rights Reserved Logical Precedence Highest ! && Lowest || Example: (2 6) && (7 > 8) is true because AND is done before OR

117 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 117 © 2006 Pearson Education. All Rights Reserved More on Precedence Example: 8 < 2 + 7 || 5 == 6 is true Highestarithmetic operators relational operators Lowest logical operators

118 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 118 © 2006 Pearson Education. All Rights Reserved Answer D

119 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

120 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 120 © 2006 Pearson Education. All Rights Reserved Answer D

121 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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.

122 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 122 © 2006 Pearson Education. All Rights Reserved Answer A

123 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 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

124 Chapter 1 Starting Out with C++: Early Objects 5/e Slide 124 © 2006 Pearson Education. All Rights Reserved Answer B It reaches 103, …, 190, 193, 196, 199, 202


Download ppt "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."

Similar presentations


Ads by Google