0% found this document useful (0 votes)
11 views9 pages

01 Creating Variables in C++

The document explains the concept of variables in C++, describing them as containers for data values that can be referenced by name. It details the rules for naming variables, the syntax for declaring them, and the five basic data types available in C++. Additionally, it provides programming examples and suggests exercises for practicing variable creation and usage.

Uploaded by

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

01 Creating Variables in C++

The document explains the concept of variables in C++, describing them as containers for data values that can be referenced by name. It details the rules for naming variables, the syntax for declaring them, and the five basic data types available in C++. Additionally, it provides programming examples and suggests exercises for practicing variable creation and usage.

Uploaded by

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

CREATING VARIABLES IN C++

Computer Science

Programming is an art form that fights back


WHAT IS A VARIABLE?
 A “variable” is like a container in a C++ program in which
a data value can be stored inside the computer’s
memory. The stored value can be referenced using the
variable’s name.
VARIABLE NAMES
 A variable name may contain only letters, digits, and the
underscore character, but cannot begin with a digit.
 It’s good practice to choose meaningful names to make

the code more comprehensible.


 Names are case-sensitive in C++ so variables named

VAR, Var, and var are treated as three individual


variables.
 Traditionally C++ variable names are lowercase and

seldom begin with an underscore as some C++ libraries


use that convention.
HOW DO I CREATE A NEW VARIABLE?
 To create a new variable in a program it must be “declared,”
specifying the type of data it may contain and its chosen
name.
 A variable declaration has this syntax:

data-type variable-name;

 Multiple variables of the same data type can be created in a


single declaration as a comma-separated list with this syntax:

data-type variable-name1, variable-name2, variable name3;


THERE ARE FIVE BASIC DATA TYPES IN C++
Data Type: Description Example:
char A single byte, capable of holding one ‘A’
character
int An integer whole number 100
float A floating-point number, correct to six 0.123456
decimal places
double A floating-point number, correct to ten 0.0123456789
decimal places
bool A boolean value of true or false, or false or 0
numerically zero is false and any non-zero true or 1
is true
NOTES ON VARIABLES
 Always begin boolean variable names with “is” so they
are instantly recognizable as booleans.
 Also, use “lowerCamelCase” for all variable names that

comprise multiple words – where all except the first


word begin with uppercase, like “isTrue.”
VARIABLE DECLARATIONS
 Variable declarations must appear before executable
statements so they will be available for reference within
statements.
 When a value is assigned to a variable it is said to have

been “initialized.”
 Optionally a variable may be initialized in its declaration.

The value stored in any initialized variable can be


displayed on standard output by the cout function.
A PROGRAM WITH VARIABLES
#include <iostream>
using namespace std;
int main()
{
char letter;
letter=‘A’; //declared then initialized
int number;
number=100;
float decimal=7.5; //declared AND initialized
double pi= 3.14159;
bool isTrue=false;

cout<<“char letter: “<<letter<<endl;


cout<<“int number: “<<number<<endl;
cout<<“float decimal: “<<decimal<<endl;
cout<<“double pi: “<<pi<<endl;
cout<<“bool isTrue: “<<isTrue<<endl;
}
PROGRAMS TO WRITE
 Write a separate program for each type of variable listed
in the table in slide five.
 Write a program that combines at least two types of

variables.
 Write a program that combines every type of variable

listed in slide five.


 Write one more program of your own design.

You might also like