An Introduction to Programming with C++ Fifth Edition

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 3: Using Variables and Constants
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 2: Input, Processing, and Output
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 2 Data Types, Declarations, and Displays
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Bill Tucker Austin Community College COSC 1315
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Completing the Problem-Solving Process
Programming Fundamentals
Chapter 2: Input, Processing, and Output
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Revision Lecture
The Selection Structure
1.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Chapter 3: Understanding C# Language Fundamentals
An Introduction to Programming with C++ Fifth Edition
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Introduction to C++ Programming
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Numbers.
CIS16 Application Development Programming with Visual Basic
Chapter 2: Introduction to C++.
Data Types and Expressions
Chapter 2: Input, Processing, and Output
Chapter 2 Primitive Data Types and Operations
Lecture 3 Expressions, Type Conversion, and string
Variables and Constants
Review of Java Fundamentals
Presentation transcript:

An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators

Objectives Distinguish among a variable, a named constant, and a literal constant Explain how data is stored in memory Declare and initialize a memory location Use an assignment statement to assign data to a variable An Introduction to Programming with C++, Fifth Edition

Objectives (continued) Include arithmetic operators and arithmetic assignment operators in a statement Get string input using the getline() function Ignore characters using the ignore() function Format floating-point output Write simple .NET C++ commands An Introduction to Programming with C++, Fifth Edition

Concept Lesson More on the Problem-Solving Process Variables and Named Constants Declaring a Named Constant Declaring a Variable An Introduction to Programming with C++, Fifth Edition

Concept Lesson (continued) Using an Assignment Statement to Store Data in a Variable Arithmetic Operators Getting Data from the Keyboard Formatting Floating-Point Numbers An Introduction to Programming with C++, Fifth Edition

More on the Problem-Solving Process An Introduction to Programming with C++, Fifth Edition

Variables and Named Constants Declare a memory location for each input, processing, and output item in IPO chart A variable is a type of memory location whose contents can change while program is running Values of named constant items remain the same each time the program is executed An Introduction to Programming with C++, Fifth Edition

Variables and Named Constants (continued) Requires four memory locations: Two input items radius variable pi named constant One processing item radius squared One output item area An Introduction to Programming with C++, Fifth Edition

Selecting a Name for a Memory Location Identifiers should be descriptive and follow some rules: An Introduction to Programming with C++, Fifth Edition

Selecting a Name for a Memory Location (continued) Most programmers: Use uppercase letters for named constants Use lowercase letters for variables Use camel case if a variable’s name contains two or more words An Introduction to Programming with C++, Fifth Edition

Selecting a Name for a Memory Location (continued) An Introduction to Programming with C++, Fifth Edition

Selecting a Data Type for a Memory Location These data types, except string, are fundamental data types An Introduction to Programming with C++, Fifth Edition

Selecting a Data Type for a Memory Location (continued) string is a class Program must include: #include <string> using std::string; C++ contains one or more data types for storing Integers (whole numbers) Floating-point numbers (with a decimal place) Characters (letters, symbols, and numbers that will not be used in calculations) Boolean values (true and false) An Introduction to Programming with C++, Fifth Edition

Selecting a Data Type for a Memory Location (continued) The data type to use for a memory location depends on the values it will store An Introduction to Programming with C++, Fifth Edition

How Data is Stored in Internal Memory An Introduction to Programming with C++, Fifth Edition

How Data is Stored in Internal Memory (continued) An Introduction to Programming with C++, Fifth Edition

Selecting an Initial Value for a Memory Location To initialize is to assign an initial value to a memory location Typically a literal constant Type can be: numeric, character, or string A location with bool data type can be initialized with keywords true or false Typical initialization values 0.0 ‘ ‘ “” true, false An Introduction to Programming with C++, Fifth Edition

Selecting an Initial Value for a Memory Location (continued) An Introduction to Programming with C++, Fifth Edition

Type Conversions Implicit type conversions can occur when assigning a value to a memory location Or, when processing calculation statements Value can be promoted or demoted Implicit demotion can adversely affect output Use explicit type conversion (type casting) to convert an item from one data type to another static_cast operator An Introduction to Programming with C++, Fifth Edition

Type Conversions (continued) An Introduction to Programming with C++, Fifth Edition

Type Conversions (continued) An Introduction to Programming with C++, Fifth Edition

Variables and Named Constants (continued) An Introduction to Programming with C++, Fifth Edition

Declaring a Named Constant An Introduction to Programming with C++, Fifth Edition

Declaring a Variable An Introduction to Programming with C++, Fifth Edition

Declaring a Variable (continued) An Introduction to Programming with C++, Fifth Edition

Using an Assignment Statement to Store Data in a Variable An Introduction to Programming with C++, Fifth Edition

Using an Assignment Statement to Store Data in a Variable (continued) An Introduction to Programming with C++, Fifth Edition

Arithmetic Operators Precedence numbers indicate order in which computer performs the operation in an expression Use parentheses to override order of precedence An Introduction to Programming with C++, Fifth Edition

Arithmetic Operators (continued) An Introduction to Programming with C++, Fifth Edition

Arithmetic Operators (continued) An Introduction to Programming with C++, Fifth Edition

Arithmetic Assignment Operators An Introduction to Programming with C++, Fifth Edition

Getting Data from the Keyboard Use >> to get numeric, character, or string values from the keyboard and store them in a variable Stops reading characters when it encounters a white-space character in the input Blank, tab, or newline An alternative is to use getline() An Introduction to Programming with C++, Fifth Edition

The getline() Function When getline() encounters the delimiter character in the input, it consumes the character newline character Items between parentheses in a function’s syntax are the arguments An Introduction to Programming with C++, Fifth Edition

The ignore() Function ignore() instructs computer to read and consume characters entered at keyboard An Introduction to Programming with C++, Fifth Edition

The ignore() Function (continued) An Introduction to Programming with C++, Fifth Edition

Formatting Floating-Point Numbers Use fixed stream manipulator to display a floating-point number in fixed-point notation #include <iostream> using std::fixed; Use scientific stream manipulator for e notation using std::scientific; Setprecision stream manipulator controls number of decimal places displayed #include <iomanip> using std::setprecision; An Introduction to Programming with C++, Fifth Edition

Formatting Floating-Point Numbers (continued) An Introduction to Programming with C++, Fifth Edition

Formatting Floating-Point Numbers (continued) An Introduction to Programming with C++, Fifth Edition

Summary Programs have variables, constants (named, literal), and arithmetic operators (to perform calculations) const dataType constantName = value; dataType variableName [= initialValue]; Use assignment statement to store data in a variable variableName = expression; When assigning a value to a memory location, the value should fit the memory location’s data type Use static_cast operator to convert an item of data from one data type to another An Introduction to Programming with C++, Fifth Edition

Summary (continued) Arithmetic operators have a precedence number Use parentheses to override order of precedence Arithmetic assignment operators abbreviate an assignment statement varName arithmeticAssignmentOp expr; getline() gets a string of characters ignore() reads and consumes characters entered at the keyboard fixed and scientific stream manipulators format the display of floating-point numbers An Introduction to Programming with C++, Fifth Edition

Application Lesson: Using Variables, Constants, and Arithmetic Operators in a C++ Program Lab 4.1: Stop and Analyze Study the program shown in Figure 4-26, then answer the questions Lab 4.2: Test program in Figure 4-27 Lab 4.3: Modify program so that it allows user to enter the semester hour fee Lab 4.4: Desk-Check Lab Lab 4.5: Debugging Lab An Introduction to Programming with C++, Fifth Edition