Chapter 2 Assignment and Interactive Input

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
Computer Science 1620 Programming & Problem Solving.
Chapter 3 Assignment and Interactive Input
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Operaciones y Variables
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Completing the Basic (L06)
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Bill Tucker Austin Community College COSC 1315
Introduction to Computer Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Computing Fundamentals
Basic Elements of C++.
Chapter 5: Loops and Files.
Chapter 2: Basic Elements of C++
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Chapter 2 Elementary Programming
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
Starting Out with C++: From Control Structures through Objects
CS150 Introduction to Computer Science 1
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
elementary programming
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Objectives You should be able to describe: The while Statement
Chapter 2 part #3 C++ Input / Output
Chapter 2: Overview of C++
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Presentation transcript:

Chapter 2 Assignment and Interactive Input Program Development and Design Using C++, Third Edition

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

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

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

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

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

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

Accumulating Program 3.3: Accumulating Example Program Development and Design Using C++, Third Edition

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

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

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

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

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

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

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

Interactive Keyboard Input (continued) Figure 3.11: Inputting Data into Variables num1, num2, and num3 Program Development and Design Using C++, Third Edition

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

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

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

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

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

Program Development and Design Using C++, Third Edition Reading and Homework Reading: Chapter 2: pg.83-end of chapter. Chapter 3: pg. 114-126 and pg. 143-151. Homework: Pg. 110: ex.1, 2, 6, and 9. Pg. 148-149: ex. 2, 4a, 5. Program Development and Design Using C++, Third Edition