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

Slides:



Advertisements
Similar presentations
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Advertisements

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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
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.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 part #4 Operator
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Input, Output, and Processing
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.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Programming with Microsoft Visual Basic th Edition
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
BASIC ELEMENTS OF A COMPUTER PROGRAM
Completing the Problem-Solving Process
Basic Elements of C++.
Multiple variables can be created in one declaration
The Selection Structure
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
CIS16 Application Development Programming with Visual Basic
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
An Introduction to Programming with C++ Fifth Edition
Chapter 2 Primitive Data Types and Operations
Operator King Saud University
Variables and Constants
Review of Java Fundamentals
Presentation transcript:

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

Introduction to Programming with C++, Fourth Edition2 Objectives Distinguish among a variable, a named constant, and a literal constant Select an appropriate name, data type, and initial value for a memory location Explain how data is stored in memory Declare and initialize a memory location Type cast data

Introduction to Programming with C++, Fourth Edition3 Objectives (continued) Use an assignment statement to assign data to a variable Include arithmetic operators in an expression Get string input using the getline() function Ignore characters using the ignore() function

Introduction to Programming with C++, Fourth Edition4 Variables and Named Constants Variable – the only type of memory location whose contents can change while a program is running Named Constant – used for any item whose value will remain the same each time the program is executed

Introduction to Programming with C++, Fourth Edition5 Circle Area Problem

Introduction to Programming with C++, Fourth Edition6 Selecting a Name for a Memory Location Identifier – a descriptive name assigned to each variable and named constant used in a program Keyword (reserved word) - a word that has a special meaning in a programming language

Introduction to Programming with C++, Fourth Edition7 Naming Rules

Introduction to Programming with C++, Fourth Edition8 Valid Names and Keywords

Introduction to Programming with C++, Fourth Edition9 Selecting a Data Type for a Memory Location Data types - control the type of data the memory location can store Fundamental data types - basic data types built into the C++ language which must be typed using lowercase letters Integers - whole numbers

Introduction to Programming with C++, Fourth Edition10 Selecting a Data Type for a Memory Location (continued) Floating-point numbers - numbers with a decimal place Characters - letters, symbols, and numbers that will not be used in calculations Boolean values - true or false

Introduction to Programming with C++, Fourth Edition11 Names of Memory Locations for the Circle Area Problem

Introduction to Programming with C++, Fourth Edition12 Some of the Data Types Available in C++

Introduction to Programming with C++, Fourth Edition13 Data Type Assigned to the Memory Locations for the Circle Area Problem

Introduction to Programming with C++, Fourth Edition14 How Data is Stored in Internal Memory Numeric data – represented in internal memory using the binary (or base 2) number system Character data - represented in internal memory using ASCII codes

Introduction to Programming with C++, Fourth Edition15 Comparison of the Decimal and Binary Number Systems

Introduction to Programming with C++, Fourth Edition16 Partial ASCII Chart

Introduction to Programming with C++, Fourth Edition17 Selecting an Initial Value for a Memory Location Initializing - assigning an initial, or beginning, value to a memory location Literal constant - an item of data that can appear in a program instruction, and can be stored in a memory location Numeric literal constant - a number

Introduction to Programming with C++, Fourth Edition18 Selecting an Initial Value for a Memory Location (continued) Character literal constant - a character enclosed in single quotation marks String literal constant - zero or more characters enclosed in double quotation marks

Introduction to Programming with C++, Fourth Edition19 Examples of Numeric, Character, and String Literal Constants

Introduction to Programming with C++, Fourth Edition20 Type Casting Implicit type conversion – in an assignment, if the data type of the expression does not match that of the memory location, the data is converted appropriately –Converts values to fit memory locations Conversion can either promote values to a larger type or demote values to a smaller type (loss of data)

Introduction to Programming with C++, Fourth Edition21 Type Casting (continued)

Introduction to Programming with C++, Fourth Edition22 Type Casting (continued) Type casting (explicit type conversion) – the forced conversion of data from one data type to another Enclose an item of data in parentheses, preceded by the desired type Example - type cast the double number 3.7 to the float data type by writing float(3.7)

Introduction to Programming with C++, Fourth Edition23 Initial Values Assigned to Memory Locations for the Circle Area Problem

Introduction to Programming with C++, Fourth Edition24 Declaring a Named Constant To declare a named constant specify: –Name –Data type –Initial value Can use the named constant anywhere the initial value can be used –Even in other named constants

Introduction to Programming with C++, Fourth Edition25 Declaring a Named Constant (continued)

Introduction to Programming with C++, Fourth Edition26 Declaring a Variable To declare a variable specify: –Name –Data type –Initial value (optional) If you omit the initial value, the variable may contain garbage (left over bits)

Introduction to Programming with C++, Fourth Edition27 Syntax and Examples of Instructions that Reserve and Initialize Variables in C++

Introduction to Programming with C++, Fourth Edition28 C++ Statements Reserving the radius, radiusSquared, and area Variables

Introduction to Programming with C++, Fourth Edition29 Using an Assignment Statement to Store Data in a Variable Use an assignment statement to change the contents of a variable while a program is running Assigns the value of the expression appearing on the right side of the assignment operator (=) to the variable whose variablename appears on the left side Remember that a variable can store only one value at a time: new assignments replace old

Introduction to Programming with C++, Fourth Edition30 Using an Assignment Statement to Store Data in a Variable (continued)

Introduction to Programming with C++, Fourth Edition31 Processing of Assignment Statements int temp = 3; temp = temp + 1; temp = temp * 2; Declaration statement int temp = 3: creates a temp variable in the computer’s internal memory and initializes the variable to the integer 3 The assignment statement temp = temp + 1: adds the number 1 to the contents of the temp variable, giving 4. It then replaces the 3 currently stored in the temp variable with the number 4

Introduction to Programming with C++, Fourth Edition32 Processing of Assignment Statements (continued) The assignment statement temp = temp * 2: first multiplies the contents of the temp variable (4) by 2, giving 8. It then removes the number 4 from the temp variable and stores the number 8 there instead

Introduction to Programming with C++, Fourth Edition33 Arithmetic Operators

Introduction to Programming with C++, Fourth Edition34 Arithmetic Operators (continued)

Introduction to Programming with C++, Fourth Edition35 Getting Data from the Keyboard Use cin and the extraction operator (>>) to input data from the keyboard Can input a single character or a string, as long as the string has no spaces The getline() function gets string input from the keyboard (including embedded spaces)

Introduction to Programming with C++, Fourth Edition36 Getting Data from the Keyboard (continued)

Introduction to Programming with C++, Fourth Edition37 The ignore() Function Instructs the computer to disregard, or skip, characters entered at the keyboard The function reads and discards (consumes) the characters Necessary when inputting numbers and characters

Introduction to Programming with C++, Fourth Edition38 The ignore() Function (continued)

Introduction to Programming with C++, Fourth Edition39 Summary Variables and constants need: –Name, data type, initial value Type casting converts values to fit memory locations Assignments change the contents of a variable while a program is running Use arithmetic operators in expressions Use getline() to input strings with spaces Use ignore() to consume unwanted characters from input stream