Download presentation
Presentation is loading. Please wait.
1
Chapter 2 Assignment and Interactive Input
Program Development and Design Using C++, Third Edition
2
Program Development and Design Using C++, Third Edition
Objectives You should be able to describe: Assignment Operations Interactive Keyboard Input Program Development and Design Using C++, Third Edition
3
Assignment Operations
Most basic statements for assigning values to variables and performing computations variable = expression; Equal sign tells computer first to: Determine value of operand to right Store (assign) value in location(s) associated with the variable to left Expression: Combination of constants, variables, and function calls that can be evaluated to a result e.g. The assignment statement: total=30; is valid. total+10=30; is NOT valid. Program Development and Design Using C++, Third Edition
4
Assignment Operations (continued)
Assignment operator: The = symbol Lower precedence than any other arithmetic operator Assignment expression: Expression using assignment operator Has a value Multiple assignments possible in same expression Assignment operator has right-to-left associativity Program Development and Design Using C++, Third Edition
5
Program Development and Design Using C++, Third Edition
Coercion Data type conversions take place across assignment operators Coercion: Value assigned to variable on left of assignment operator forced into data type of variable it is assigned to Integer value assigned to real variable Real value assigned to integer variable Pay attention to mixed-mode expressions Program Development and Design Using C++, Third Edition
6
Assignment Variations
Variable to left of equal sign can also be used on the right of equal sign e.g., sum = sum + 10; Remember operator precedence Shortcut assignment operators: += -= *= /= %= Program Development and Design Using C++, Third Edition
7
Assignment Variations (continued)
Figure 3.4: sum = sum + 10; Causes a New Value to Be Stored in sum Program Development and Design Using C++, Third Edition
8
Accumulating Program 3.3: Accumulating Example
Program Development and Design Using C++, Third Edition
9
Program Development and Design Using C++, Third Edition
Counting Counting statements have form: variable = variable + fixedNumber; Increment operator: ++ variable++ equivalent to variable = variable + 1 Prefix increment operator: ++variable Increment, then use variable Postfix increment operator: variable++ Use variable, then increment Program Development and Design Using C++, Third Edition
10
Program Development and Design Using C++, Third Edition
Counting (continued) Decrement operator: -- Variable-- equivalent to variable = variable - 1 Prefix decrement operator: --variable Decrement, then use variable Postfix decrement operator: variable-- Use variable, then decrement Program Development and Design Using C++, Third Edition
11
Interactive Keyboard Input
Programs that do same calculation on same set of numbers every time not very useful cin object: Used to enter data into program while executing (“get from”) operator, >> Prompt: String telling person at terminal what should be typed cin object puts computer into temporary pause state until user types a value Program Development and Design Using C++, Third Edition
12
Interactive Keyboard Input (continued)
Figure 3.10: cin Is Used to Enter Data; cout Is Used to Display Data Program Development and Design Using C++, Third Edition
13
Interactive Keyboard Input (continued)
Any number of values can be input using single cin statement cin extraction operation able to make some data type conversions Program Development and Design Using C++, Third Edition
14
Program Development and Design Using C++, Third Edition
Example 1 #include <iostream> using namespace std; int main() { double num1, num2, product; cout << "Please type in a number: "; cin >> num1; cout << "Please type in another number: "; cin >> num2; product = num1 * num2; cout << num1 << " times " << num2 << " is " << product << endl; return 0; } Program Development and Design Using C++, Third Edition
15
Program Development and Design Using C++, Third Edition
Example 2 #include <iostream> using namespace std; int main() { int num1, num2, num3; double average; cout << "Enter three integer numbers: "; cin >> num1 >> num2 >> num3; average = (num1 + num2 + num3) / 3.0; cout << "The average of the numbers is " << average << endl; return 0; } Program Development and Design Using C++, Third Edition
16
Interactive Keyboard Input (continued)
Figure 3.11: Inputting Data into Variables num1, num2, and num3 Program Development and Design Using C++, Third Edition
17
A Closer Look: Programming Errors
An error can be detected: Before program is compiled While program is being compiled While program is being run After program has been executed and output is being examined Program Development and Design Using C++, Third Edition
18
A Closer Look: Programming Errors (continued)
Compile-time errors: Errors detected by compiler Syntax errors and parse errors Run-time errors: Errors that occur while program is running Logic errors Ways to protect against run-time errors: Anticipate what user might do to cause errors Rigorous testing Program Development and Design Using C++, Third Edition
19
A Closer Look: Programming Errors (continued)
Desk checking: Checking program code for syntax and logic errors Program testing: Detecting errors during or after program execution Logic errors never caught by compiler Plan program testing carefully Maximize possibility of locating errors Program Development and Design Using C++, Third Edition
20
A Closer Look: Programming Errors (continued)
Bug: Program error Debugging: Process of isolating errors, correcting them, and verifying corrections Program tracing: Imitate computer and execute statements by hand Echo printing: Add temporary code to display values of input data Debugger: Controls execution of program Can interrupt program at any point Can display values of all variables Program Development and Design Using C++, Third Edition
21
Exercise: Find the errors
#include <iostream> using namespace std; int main() { width = 15 area = length * width; cout << “The area is “ << area } Program Development and Design Using C++, Third Edition
22
Program Development and Design Using C++, Third Edition
Reading and Homework Reading: Chapter 2: pg.83-end of chapter. Chapter 3: pg and pg Homework: Pg. 110: ex.1, 2, 6, and 9. Pg : ex. 2, 4a, 5. Program Development and Design Using C++, Third Edition
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.