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.

Slides:



Advertisements
Similar presentations
Data types and variables
Advertisements

Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
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.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 2 Variables.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ for Engineers and Scientists Second Edition
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Documentation Need to have documentation in all programs
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
DATA HANDLING.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Basics
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
C++ for Engineers and Scientists Second Edition
Variables and Constants
Programming Fundamental-1
Presentation transcript:

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 compiler restriction u Give numeric values to variables with assignment statement Lesson 3.1 variable_name = value; assignment operator

Naming Identifiers u First character must be letter –a-z, A-Z or _ u Other characters –letters (a-z, A-Z, _ ) or digits 0-9 u Cannot use C++ keywords (reserved words) u Cannot have blank within identifies Lesson 3.1

Keywords u Words with special meaning to C++ u Also includes alternative representations of certain operators and punctuators u Full listing in Table 3.1 u Examples: –auto, bool, float, inline, union, delete –namespace, private, void Lesson 3.1

Declaring Variables u Variables MUST be declared u List name and data type u Variables of same type may be declared in same statement (separate by comma) u Causes C++ compiler to know size of space to be reserved for storing variable’s value Lesson 3.1 double radius, diameter;data type variable names separator

Assignment Statements u Causes value to be stored in variable’s memory cell variable_name = value; u Variable name MUST appear on left u Equal sign is assignment operator Note: be careful = does NOT mean equal u Example: temperature = 78; Lesson 3.1

Constant Qualified Variables u Use const qualifier const double PI = ; u Cannot modify later in program u Style tip: use all uppercase characters to name constant –Makes constants easy to identify Lesson 3.2

Formatting Output u Insert I/O manipulators (parameterized) into cout statements for printing –declared in header iomanip #include u Basic form cout << manipulator(parameter); what manipulator uses to modify output Lesson 3.2 Listed in Table 3.2

setw( ) u Sets field width u Right justifies contents u C++ automatically expands if set width too small Lesson 3.2 cout<<“number =“<<setw(7)<<num<<endl; number = 5 ******* Field size

setprecision( ) u Sets number of digits after decimal point u All digits retained in memory u Once set may or may not be used until another statement changes it (compiler) Lesson 3.2 num = ; cout<<“num = “<<setprecision(2)<<num; num = 5.34

setfill( ) u Specifies character for blank space in field u Single quotes required around character enclosed in parentheses Lesson 3.2 num = 5.34; cout<<setw(10)<<setfill(‘*’)<<num; ******5.34

setiosflags(ios:: ) u Perform number of different actions based on flag that is set u Table 3.3 u Example: Lesson 3.2 num = 5.34; cout<<setiosflags(ios::left) << setfill(‘*’)<<setw(10)<<num; 5.34****** left justifies in field

Printing “dollar” Format u Necessary to use I/O manipulators Lesson 3.2 cout<<setprecision(2) <<setiosflags(ios::fixed|ios::showpoint) <<“Income = $” <<income; Income = $

Character Data u Lowercase and uppercase characters u Also graphic characters (!, #, ^) and “space” u Escape characters (\n) regarded as single characters u Numbers 0-9 can also be characters u Declare character variable:char c1,c2; u Assign value:c1 = ‘g’; Lesson 3.3 Can hold ONE character Enclose in single quotes

Assigning Characters to int u C++ assigns ASCII code value for the char u Does not use numeric value if assign 0-9 character u Table 3.5 gives characters and ASCII code/values Lesson 3.3

Arithmetic Operations u Look like algebraic expressions u Expression consists of sequence of operand(s) and operator(s) –Operand (variable, constant, any value) –Operators (+, -, *, /, % ) t Represent operation to be done t Increment (++) and decrement (--) Lesson 3.4

Problems u Uninitialized variables –C++ assigns own value –Does not trigger as an error u Exceeding integer range –int type range –32768 to –Due to limit of two bytes of memory –Overflow error u Division by zero Lesson 3.4

Pre- and Post- Operators u ++ or -- u Place in front, incrementing or decrementing occurs BEFORE value assigned Lesson 3.5 k = i++; i = 2 and k = 1 k = ++i; u Place in back, occurs AFTER value assigned i = 2 and k = 1 k =--i; k = i--; i = i + 1; k = i; 3333 i = i - 1; k = i; 1111 i = i + 1; 1313 k = i; i = i - 1; 2121

Mixed Type Arithmetic u Assign real to int –Cut off fractional part of real u Assign int value to real –Put decimal point at end, converts method of storage to exponential binary form u Modify with cast operators –Change type of expression –Keywordstatic_cast –Old form: (type) expression Lesson 3.5

static_cast Operator u General form Lesson 3.5 static_cast (expression) u Keyword requires underscore u Expression for which temporary copy is made of type type u Type can be any valid C++ data type

Operator Precedence Lesson 3.5 ( )parenthesesunaryprefix L to R 1 ++, --post-(in/de)crementunarypostfix L to R 2 ++, --pre-(in/de)crementunaryprefix R to L 3 +positive signunaryprefix R to L 3 -negative signunaryprefix R to L 3 static_castcastunaryprefix R to L 4 %, *, /remainder/multi/divbinaryinfix L to R 5 +, -add/subtractbinaryinfix L to R 6 +=, -=, *=math & assignmentbinaryinfix R to L 7 /=, %=math & assignmentbinaryinfix R to L 7 =assignmentbinaryinfix R to L 7

Real data types u Decimal numbers u float –4 bytes of memory, 6 digit precision u double –8 bytes of memory, 15 digits precision u long double –10 bytes of memory, 19 digits precision Lesson 3.6

Integer data types u Whole numbers u int, signed int, short int, signed short int –2 bytes, range: to u unsigned int, unsigned short int –2 bytes, range: 0 to u long int, signed long int –4 bytes, range: to u unsigned long int –4 bytes, range: 0 to Lesson 3.6

Math Functions u Need cmath or cstlib headers #include or #include u General form: name(parameters) u Note what type and form parameters take –Trig functions use radian measure not angle u Table 3.11 lists math library functions Lesson 3.6

Summary u Declare variables and constants u Format output u Work with character data u Create mathematical expressions u Work with mixed data types and casting u Use different data types for precision u Utilize available math functions Chapter 3 Learned how to: