Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
1 C++ Syntax and Semantics The Development Process.
Data Types and Expressions
Types and Variables. Computer Programming 2 C++ in one page!
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Primitive Variables.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
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 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CPS120: Introduction to Computer Science Variables and Constants.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
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.
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
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Chapter # 2 Part 2 Programs And data
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Basic Elements of C++ Chapter 2.
Introduction to the C Language
Microsoft Visual Basic 2005 BASICS
Chapter 2 Variables.
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
CHAPTER FOUR VARIABLES AND CONSTANTS
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Chapter 2 Variables.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Data in a C++ Program Numeric data and character data
Presentation transcript:

Chapter 2 Variables and Constants

Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character variables. Explain the different floating-point types and use variables of those types. Describe Boolean variables. Use constants.

Understanding Variables A variable is a data structure whose contents can change while a program is running. In C++ you must select the data type that best fits the nature of the data to be stored.

Integer Data Types An integer is a whole number. There are many ways to store integers; each has its own range and memory requirements. The integer data types in C++ are: char, unsigned char, short, unsigned short, int, unsigned int, long and unsigned long.

Declaring and Naming Variables Indicating to the compiler the name and type of variable you want to use is called “declaring” the variable. You must declare a variable for you can use it. A typical variable declaration might look like the following: int i; // declare i as an integer

Initializing Variables Initializing a variable means assigning a value to it. Variable values are indeterminate when they are first declared. You use the = sign to assign a value to a variable. A typical variable initialization statement might look like the following: i = 2; // initialize i to 2

Naming Variables Names of variables are called identifiers. Identifiers must start with a letter or an underscore (_). You can use letters, numerals, or underscores in the rest of the identifier. Use names that make the purpose of the variable clear. There can be no spaces in identifiers. Keywords cannot be used as identifiers.

Characters and the Char Data Type Characters are stored as numbers according to the ASCII codes. C++ includes a char data type for storing characters. Each variable of the char data type can hold only one character. A group of characters put together to form a word or phrase is called a string.

Floating-point data types Tasks such as working with money require floating-point data types. Floating-point data types can store fractional numbers. The floating-point data types in C++ are float, double and long double. With these types, you can also use exponential notation: e6

Boolean Variables A Boolean variable can have two possible values. One value usually represents true and one usually represents false. Some C++ compilers do not support the Boolean data type.

Constants Constants hold data that does not change as the program runs. Constants must be given a data type and a name. An example of a statement that declares a constant: const double PI = ;

Summary Computers store data in data structures. Data is stored in variables or constants. Within one data type, there may be types with different ranges and memory requirements. Variables must be declared before they are used. Boolean variables can have one of two values.