You are on page 1of 26

UNIT 2: C++ PROGRAMMING

To write a simple program in the C++ programming language, you


must first understand the fundamental concepts required to build
useful programs. Acquiring skills and mastering these concepts early
will make it easier to solve problems and write powerful and efficient
codes.
VARIABLES
Unit 2 - Lesson 2
Lesson Objectives:
At the end of the lesson, you should be able to:

create and name variables;

understand the data types used in programming;

assign values to variables; and

declare and initialize variables.


TOPICS

• Creating and Naming Variables


• Defining Variables
• Scope of Variables
• Assigning Values to Variables
Data

Data Variable

Variables allow programmers to write programs that instruct the


computer to behave in a specific way. Variables and directions are
defined in programs, and variables represent changeable data. The
instructions explain how the variables should be used by the computer.
Variables enable a program to execute different sets of data.
Data

Data Variable

A variable is any name used to hold information or serve as a


reference to information. They are called variables because the
represented information can change, but the operations on the variable
remain the same. It is the exact opposite of a constant, whose values do
not change.
Data

Data Variable

A variable gives us names that we can use to store values that our
programs can manipulate. In C++, each variable has a type that
determines the size and layout of the variable's memory, the range of
values that can be stored within that memory, and the set of operations
that can be applied to the variable.
Data

Data Variable

Create a variable name that tells the program's reader what the
variable represents and describes the data stored at that specific
memory location. For example, if you want to save a test score, a
variable name like score is easier to remember than a variable name like
val2.
Creating and Naming Variables

There are several rules to follow when naming variables.

• Variable names must begin with an alphabet letter, an underscore, or


(_). The convention is to always use an alphabet letter.
• Variable names can include numbers from 0 to 9 after the first letter.
There are no spaces or special characters allowed.
• The name can be as long as you want.
• Variable names are case-sensitive; uppercase characters are distinct
from lowercase characters.
Creating and Naming Variables

There are several rules to follow when naming variables.

• You cannot use a keyword or reserved word for a variable name.


• In your variable names, avoid using hyphens (-).
• Try to use meaningful names.
• Use prefixes to your advantage.
• Maintain consistency.
• Do not use any of the action category names.
• Do not assign a value to a built-in variable by mistake.
Creating and Naming Variables

There are several rules to follow when naming variables.

• If you don't intend to, don't use the same variable name twice.
• For variable names, avoid using accented characters.

We must first understand the different data types used in C++


programming before we can create or declare variables in a C++
program.
Creating and Naming Variables

Variables in C++ can only be created with data types like integers
or strings, and they can only contain data that can be presented to
them. This restriction on the data types of variables results in faster
development and more efficient program execution.

int age = 20;


Creating and Naming Variables
Data Types Meaning
byte - All numbers in the interval [-128, 127] can be stored in a byte variable.
- Ex:
byte b1 = -128; // OK
byte b2 = 127; // OK
byte b3 = 100; // OK

byte b4 = -129; // ERROR


byte b5 = 128; // ERROR
short - All numbers in the interval [-32768, 32767] can be stored in a short variable.
- Ex:
short s1 = 5000; // OK
short s2 = 32767; // OK

short s4 = 32768; // ERROR

int - is used to store and process integer numbers or whole numbers (i.e., numbers without a decimal point).
- Ex: 24, -19, 2021, etc.
Creating and Naming Variables
Data Types Meaning

long - All numbers in the interval [-9223372036854775808, 9223372036854775807] can be stored in a long
variable.

float - is used to store and process single-precision real numbers (i.e., real numbers containing 6 to 7 digits after
the decimal point).
- Ex: 3.142, 9.8, 2.123456, 3.4E+38

double - is used to store and process double-precision real numbers (i.e., real numbers containing 15 to 16 digits
after the decimal point).
- Ex: 2.1234567898765

boolean - is used to store and process Boolean values.


- Ex: true or false

char - is used to store and process any symbol from the ASCII character set enclosed between a pair of single
quotations.
- Ex: ‘A’, ‘9’, ‘$’, ‘\n’, ‘\t’
Defining Variables

Variables are declared in A good variable name tells us right away


C++ by stating their type, what the variable is for and helps us understand
followed by their name or the flow of our program.
identifier, followed by a
semicolon. Example: int number;

A variable declaration In the given example, int is the type, and


informs the computer number is the name of our variable.
that there is only one
variable of the specified
type and name.
Defining Variables

Variables are declared in Take a look at other examples of types and the
C++ by stating their type, variable names assigned to them. Consider the
followed by their name or comments that come after the double slash; these
identifier, followed by a describe how the variable should function.
semicolon. Example:

A variable declaration char letter; //letter will hold a single non-numeric character
informs the computer int price; //price will hold a whole number value
that there is only one int quantity; //quantity will hold a whole number value
variable of the specified double money; //money will hold numbers with decimal value
type and name. float area; //area will hold numbers with decimal value
Defining Variables
Simulate the given program. Notice Program Output
how variables are declared.

1. Type in your codes here:


https://www.onlinegdb.com/
2. On the upper right portion of the
website, click "language."
Choose "C++.“

3. Once you are done typing the


codes, click the "run" button in
the upper portion of the
website.
Scope of Variables

A scope is a region of the program where variables can be declared.


There are three places where this is possible:
Scope of Variables

• A variable is called a local variable if it is written within a function or a


block. It can only be used by statements within that function or block of
code. Local variables are not known to function outside of their own.

• A variable can be declared in the definition of function parameters,


which are also called formal parameters.

• A variable is called a global variable if it is declared outside of all


functions. Any function can access a global variable, and it is available for
use throughout the program after it is declared.
Scope of Variables

In the following lessons, we will learn about functions and their


parameters. Let us now look at local and global variables.
Program Output

num1, num2, and sum are considered


local variables because they are declared inside a
function or block.
Scope of Variables

In the following lessons, we will learn about functions and their


parameters. Let us now look at local and global variables.
Program Output

num3 is considered a global variable


because it is declared outside of a function
and can be used in any part of the program.
Assigning Values to Variables

We use the equal (=) sign, Syntax for declaring variables:


which is an assignment <datatype> variable;
operator, to assign values
to variables.
Examples:
Explore its syntax and the char letter;
examples that follow. int num1;
double money;
Assigning Values to Variables

We use the equal (=) sign, Syntax for assigning values to variables:
which is an assignment <variable name> = value;
operator, to assign values
to variables.
Examples:
Explore its syntax and the letter = 'A';
examples that follow. num1 = 80;
money = 100.50
Assigning Values to Variables
Program

When we assign a value


to a variable, we are
initializing it. In our
example, the
total_amount variable
was set to zero. The value
of total_amount is then
200 after the arithmetic
operation is completed.
Output
Variables are important in
the aspect of storing values
used in the program that are
dependent on the condition
or information passed to the
program and help minimize
the computer's memory
space consumption.
ACTIVITY TIME!

- Go to the student portal


- Do the activity: WSComputer9.U2Lesson2_VARIABLES

You might also like