0% found this document useful (0 votes)
71 views19 pages

Slide02-Variables and Data Types

This document discusses variables and data types in C++. It defines a variable as a temporary storage location where data can be stored while a program runs. Variables are declared with a type and name, and then assigned values. The document outlines different data types including integers, characters, floating-point numbers, and strings. It also discusses creating variables, naming conventions, and examples of variable usage.

Uploaded by

shu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views19 pages

Slide02-Variables and Data Types

This document discusses variables and data types in C++. It defines a variable as a temporary storage location where data can be stored while a program runs. Variables are declared with a type and name, and then assigned values. The document outlines different data types including integers, characters, floating-point numbers, and strings. It also discusses creating variables, naming conventions, and examples of variable usage.

Uploaded by

shu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Regent University College of Science & Technology

VARIABLES AND DATA


LECTURE 2

David Botwe
TYPES
1
WHAT IS A VARIABLE?
Variables are temporary storage locations where
data can be stored while a program is running.

Technology
Regent University College of Science &
Their values can be changed at any point over the
course of a program

2
CREATING VARIABLES
To create a variable, declare its name
and the type of information that it will
store.

Technology
Regent University College of Science &
The type is listed first, followed by the
name.
Example: a variable that stores an integer
representing the score on an exam could
be declared as follows:
int score ;

type variable-name;
3
CREATING VARIABLES (CONTD)
Now you have the variable (score), you will
want to assign a value to it.

Technology
Regent University College of Science &
Example: a score in the class exam is 98.
score = 98;

Examples of other types of variables:


string studentName;
bool gameOver; 4
VARIABLE NAMES (IDENTIFIERS)
A valid variable name (or identifier) is a
sequence of

Technology
Regent University College of Science &
Letters (upper and lowercase)
Digits
A name cannot start with a digit
Underscores
A name should not normally start with an underscore

Variable Names are case sensitive


MyObject is a different from MYOBJECT
5
KEYWORDS
Keywords are words reserved as part of the
language

Technology
Regent University College of Science &
int, return, float, double

They cannot be used by the programmer to name


things
They consist of lowercase letters only
They have special meaning to the compiler

6
FUNDAMENTAL C++ DATA TYPES
C++ has a large number of fundamental or built-
in data types

Technology
Regent University College of Science &
The fundamental data types fall into three
categories
Integer data types
Floating-point data types
Character data types

7
INTEGER DATA TYPES
The basic integer data type is int
The size of an int depends on the machine
and the compiler

Technology
Regent University College of Science &
On PCs it is normally 16 or 32 bits
Other integer data types
short: typically uses less bits
long: typically uses more bits
Differenttypes allow programmers to use
resources more efficiently
Standard arithmetic and relational
operations are available for these types 8
INTEGER CONSTANTS
Integer constants are positive or negative whole
numbers

Technology
Regent University College of Science &
Examples
97
40000L L or l indicates long
integer
50000

23a (illegal)

9
CHARACTER DATA TYPES
Character data type char is related to the
integer types

Technology
Regent University College of Science &
Characters are encoded using a scheme
where an integer represents a particular
character
ASCII is the dominant encoding scheme
Examples
' ' encoded as 32 '+' encoded as 43
'A' encoded as 65 'Z' encoded as 90
'a' encoded as 97 'z' encoded as 122
10
CHARACTER CONSTANTS

Explicit (literal) characters within single


quotes
'a','D','*'

Technology
Regent University College of Science &
Special
characters - delineated by a
backslash \
Two character sequences (escape codes)
Some important special escape codes
\t denotes a tab \n denotes a new line
\\ denotes a backslash \' denotes a single quote

\" denotes a double quote

'\t' is the explicit tab character, '\n' is the


explicit new line character, and so on 11
FLOATING-POINT DATA TYPES
Floating-point data types represent real
numbers
Integer part

Technology
Regent University College of Science &
Fractional part

The number 108.1517 breaks down into the


following parts
108- integer part
1517 - fractional part

C++ provides three floating-point object types


float
double
long double 12
FLOATING-POINT CONSTANTS
Standard decimal notation
134.123 F or f indicates single
0.15F

Technology
Regent University College of Science &
precision floating point
value
Standard scientific notation
1.45E6 L or l indicates long double
0.979e-3L floating point value

When not specified, floating-point constants are


of type double

13
EXAMPLE
/* Program to find the area of a circle*/

#include <iostream>

Technology
Regent University College of Science &
using namespace std;

int main()
{
float pi=3.142F;
float area, radius;
cout<<"Enter radius: ";
cin>>radius;
area= pi * radius * radius;
cout<<"Area = "<< area<<endl;
system("PAUSE");
return 0; 14
}
CONSTANT DEFINITIONS
Modifier const indicates that a variable cannot
be changed
i.e. Variable is read-only

Technology
Regent University College of Science &
Useful when defining variables representing
physical and mathematical constants
const float Pi = 3.1415;

Value has a name that can be used throughout


the program
const int SampleSize = 100;

Makes changing the constant easy


Only need to change the definition and recompile
15
STRINGS CONSTANTS
Astring constant is a sequence of zero or
more characters enclosed in double quotes

Technology
Regent University College of Science &
"We are even loonier than you
think"
"Rust never sleeps\n"
"Nilla is a Labrador Retriever"

Note: A string is NOT a fundamental data


type 16
STRING VARIABLES
Class string
Used to represent a sequence of characters as

Technology
Regent University College of Science &
a single object
Some definitions
string Name = "Joanne";
string DecimalPoint = ".";
string empty = "";
string copy = Name;
string Question = '?'; // illegal
17
EXERCISE

What is a variable?
How do you declare a variable in a

Technology
Regent University College of Science &
program?
Which of the following are valid variable

names?
i) Amount
ii) 6tally
iii) my*Name
iv) salary
v) first Name 18
EXERCISE (CONT'D)

What data types would you use to store the


following?

Technology
Regent University College of Science &
i) Population of Ghana
ii) Approximation of
iii) Average mark of a student
iv) First letter of your name

19

You might also like