Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.

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.
Numeric Types, Expressions, and Output ROBERT REAVES.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Data types and variables
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
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.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
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++
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
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.
Expressions and Interactivity. 3.1 The cin Object.
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.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
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 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Completing the Problem-Solving Process
Basic Elements of C++.
Data Types, Identifiers, and Expressions
The Selection Structure
1.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Numbers.
CIS16 Application Development Programming with Visual Basic
Chapter 2: Introduction to C++.
An Introduction to Programming with C++ Fifth Edition
Variables and Constants
Presentation transcript:

Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators

 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 Edition2

 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 Edition3

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

 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 Edition5

6

 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 Edition7

8 Requires four memory locations:  Two input items radius  variable pi  named constant  One processing item radius squared  variable  One output item area  variable

 Identifiers should be descriptive and follow some rules: An Introduction to Programming with C++, Fifth Edition9

 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 Edition10

An Introduction to Programming with C++, Fifth Edition11

An Introduction to Programming with C++, Fifth Edition12 These data types, except string, are fundamental data types

 string is a class ◦ Program must include:  #include  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 Edition13

 The data type to use for a memory location depends on the values it will store An Introduction to Programming with C++, Fifth Edition14

An Introduction to Programming with C++, Fifth Edition15

An Introduction to Programming with C++, Fifth Edition16

 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.0  ‘ ‘  “”  true, false An Introduction to Programming with C++, Fifth Edition17

An Introduction to Programming with C++, Fifth Edition18

 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 Edition19

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

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

An Introduction to Programming with C++, Fifth Edition22

An Introduction to Programming with C++, Fifth Edition23

An Introduction to Programming with C++, Fifth Edition24

An Introduction to Programming with C++, Fifth Edition25

An Introduction to Programming with C++, Fifth Edition26

An Introduction to Programming with C++, Fifth Edition27

 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 Edition28

An Introduction to Programming with C++, Fifth Edition29

An Introduction to Programming with C++, Fifth Edition30

An Introduction to Programming with C++, Fifth Edition31

 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 Edition32

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

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

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

 Use fixed stream manipulator to display a floating-point number in fixed-point notation #include using std::fixed;  Use scientific stream manipulator for e notation #include using std::scientific;  Setprecision stream manipulator controls number of decimal places displayed #include using std::setprecision; An Introduction to Programming with C++, Fifth Edition36

An Introduction to Programming with C++, Fifth Edition37

An Introduction to Programming with C++, Fifth Edition38

 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 Edition39

 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 Edition40