Computer Programming TCP1224 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 Programming with Microsoft Visual Basic 2005, Third Edition.
Types and Arithmetic Operators
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
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.
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++.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
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.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
Input, Output, and Processing
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
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.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
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 Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
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.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Completing the Problem-Solving Process
Programming Fundamentals
Basic Elements of C++.
Data Types, Identifiers, and Expressions
The Selection Structure
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Numbers.
Chapter 2: Introduction to C++.
An Introduction to Programming with C++ Fifth Edition
Variables and Constants
Presentation transcript:

Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators

2 double int * cin / variable data types

We have done some programming last week. We are left with some un-answered questions. We will try to re-visit these questions this week. ▫So programming is not mysterious, and is EASY! 3

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 4

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 5

More Problem-Solving Process 6

Variables and Named Constants First question – What are variables? 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 7

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

Selecting a Name for a Memory Location Names – No Spaces! Identifiers should be descriptive and follow some rules: 9

Selecting a Name for a Memory Location 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 10

Selecting a Name for a Memory Location 11

Selecting a Data Type for a Memory Location 12 These data types, except string, are fundamental data types What are Data Types!

Selecting a Data Type for a Memory Location string is a class ▫Program must include: #include using std::string; 13

Selecting a Data Type for a Memory Location 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) 14

Selecting a Data Type for a Memory Location The data type to use for a memory location depends on the values it will store 15

How Data is Stored? Many questions about data types can be answered if there’s understanding of how it is stored in the computer’s memory. Questions such as “what’s the difference between double and float ?” 16

How Data is Stored in Internal Memory 17

How Data is Stored in Internal Memory 18

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.0  ‘ ‘  “”  true, false 19

Selecting an Initial Value for a Memory Location 20

Type Conversions Need to understand this for next laboratory session 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 21

22

23

Declaring a Named Constant 24

Declaring a Variable 25

Using an Assignment Statement to Store Data in a Variable 26

Using an Assignment Statement to Store Data in a Variable 27

Arithmetic Operators Basics of arithmetic operations are similar to how you would write in your mathematics class! Precedence numbers indicate order in which computer performs the operation in an expression ▫Use parentheses to override order of precedence 28

Arithmetic Operators 29 These are different!

Arithmetic Operators (continued) 30

Arithmetic Assignment Operators 31

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()  For example, if you want to read a sentence. 32

getline() and ignore() Will explain only here, you are expected to try this and figure out in this week’s lab. 33

The getline() Function When getline() encounters the delimiter character in the input, it consumes the character 34 Items between parentheses in a function’s syntax are the arguments newline character

The ignore() Function ignore() instructs computer to read and consume characters entered at keyboard 35

The ignore() Function 36

Formatting Floating-Point Numbers 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; 37

Formatting Floating-Point Numbers 38

Formatting Floating-Point Numbers 39

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 40

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 41