Constant
Objectives
After this lesson the student should be able to:
• Define what is constant
• Enumerate the different types of constant
• Use constant in making program
• Appreciate the value of collaboration
9/15/2025 PRESENTATION TITLE 2
Constant
• Is a quantity that cannot be change
during program execution.
Types of Constant
• Literal Constant
• Symbolic Constant
9/15/2025 PRESENTATION TITLE 3
Literal Constant
• It is a value that is typed directly in a program
• It appears wherever it is needed
Ex.
cout<<“Hello World”; (string literal constant
int age = 19; (19 is a literal constant)
9/15/2025 PRESENTATION TITLE 4
Types of Literal Constant
• Integer Constant
• Floating point Constant
• Character Constant
• String Constant
9/15/2025 PRESENTATION TITLE 5
Integer Constant
• Are numeric values without fraction or
decimal point
• Both positive and negative integer
constant are
Ex. 87 45 -10 -5
65000L -5500L
Uses of L at the end of the integer indicates that it is a long integer
9/15/2025 PRESENTATION TITLE 6
Floating Point Constant
• Are numeric values with fraction or decimal
point
• Both positive and negative
• Use of F or f at the end of real values indicates
that the value is float
Ex. 50.75F 10.22F
15.32 42.79 (F or f not use considered as double)
9/15/2025
52000.3L(indicates long double) 7
Character Constant
• refers to any character written in single
quotes
• Alphabet characters, digits and special
symbols can be used as character constant
Ex. ‘A’ ‘n’ ‘g’ ‘=’ ‘$’
9/15/2025 PRESENTATION TITLE 8
String Constant
• A set of characters written in double
quotations
• It may consist of alphabetic characters, digits
and special symbols
Ex. “Pakistan” “123” “99-Mall Road, Isulan”
9/15/2025 PRESENTATION TITLE
Symbolic Constant
• Is a name given to values that cannot be
changed
• A constant must be initialized, after
initialization its value cannot be changed
• It is used to represent a value that I
frequently used in a program
• Symbolic constant PI indicate the value pf
PI which is 3.141593
9/15/2025 PRESENTATION TITLE 10
Symbolic Constant
• Symbolic constant can be declared in
two ways:
1. const Qualifier
2. define Directive
9/15/2025 PRESENTATION TITLE 11
const Qualifier
• It is used to define a constant
• It must declared specifying its name and data
type
• Constant must be initialized with some value
• Initialized value cannot be changed during
program execution
9/15/2025 PRESENTATION TITLE 12
const Qualifier
Syntax: const data_type identifier = value;
• const keyword used to define a
constant
• data_type indicates the data type of the
constant
• identifier represents the name of the
constant
• value value which constant is initialized
Ex. const int N = 100; 13
define directive
• It is used to define a constant
• Its difference to const is that it does not define
the data type of the constant
• It starts with the symbol #, and not terminated
with semicolon
Syntax:
#define identifier value
14
define directive
Syntax:
#define identifier value
# - indicates the start of preprocessor directive
define - use to define a constant
identifier - name of the constant
value - represents the value associated with the identifier
preprocessor directive replaces all occurrences of the identifier with the
value identifier is conventionally written in uppercase
15
define directive
• preprocessor directive replaces all occurrences
of the identifier with the value
• identifier is conventionally written in
uppercase
Example:
#define PI 3.141593
16
Expression
• Refers to a statement that evaluates to a value
• It gives a single value
• An expressions consists of operators and
operands
• Operators, is a symbol that performs some
operation
• Operands, is the value on which the operator
performs some operation
9/15/2025 PRESENTATION TITLE 17
Expression
• Operands, can be a constant, variable or
function
• Expressions may consists of any number of
operations and operands.
Examples:
A + B;
m/n;
9/15/2025
x+ 100; PRESENTATION TITLE 18
Operators
• Are symbols that are used to perform
certain operations on data
• Various operators in C++ includes;
• Arithmetic operators, relational
operators, logical operators, bitwise
operators etc.
9/15/2025 PRESENTATION TITLE 19
Categorize of Operators
1. Unary Operators
• a type of operator that works with
one operand
• Ex. -, ++, --
-a;
N++;
--x;
9/15/2025 PRESENTATION TITLE 20
Categorize of Operators
2. Binary Operators
• a type of operator that works with
two operand
• Ex. +, -, *, /, %
a + b;
x/y;
9/15/2025 PRESENTATION TITLE 21
Arithmetic Operators
• It is a symbol that performs mathematical
operation on data
Operation Symbol Description
Addition + Adds two values
Subtraction - Subtract one value from another value
Multiplication * Multiplies two values
Division / Divide one value by another value
Gives the remainder of division of two
Modulus %
integers
9/15/2025 PRESENTATION TITLE 22
Example
Suppose we have the two variables A and B,
where A = 10 and B = 5
Operation Result
A+B 15
A- B 5
A*B 50
A/B 2
A%B 0
9/15/2025 PRESENTATION TITLE 23
Modulus
• Modulus operator is called remainder operator
• The modulus operator works only with integer
value
• If modulus operator is used with the division of
0, the result will always be 0, Ex 0 % 5 = 0
• In expression like 3 % 5 is not divisible by 5, =
3
9/15/2025 PRESENTATION TITLE 24
Assignment Statement
• It refers to a statement that assigns a value
to a variable
• Assignment operator (=) is used in
assignment statement
• Name of variable written on the left side of
the assignment operator
• Value is written on the right side of the
assignment operator
9/15/2025 PRESENTATION TITLE 25
Syntax
variable = expression;
Variable - name of variable to which the value is assigned
= - assignment operator used to assign the
value to variable
Expression - is the expression whose returned values is
assigned to variable, can be constant
variable or combination of operands and
arithmetical operators
9/15/2025 PRESENTATION TITLE 26
Examples
a = 100;
c = a + b;
x = c - d + 10
9/15/2025 PRESENTATION TITLE 27
lvalue and rvalue
• lvalue is an operand that can be written on
the left side of assignment operator (=), must
be a single value
• rvalue is an operand that can be written on
the right side of assignment operator (=)
• All lvalues can be used as rvalues, but
rvalues cannot be used as lvalue
ex. X = 5; 5 = x;
9/15/2025 PRESENTATION TITLE 28
Assignment Operator
The assignment operator assigns a value to
a variable.
9/15/2025 PRESENTATION TITLE 29
Compound Assignment
• It is a combine assignment operator with
arithmetic operators
• It is used to perform mathematical
operations more easily
Syntax
variable op = expressions;
9/15/2025 PRESENTATION TITLE 30
Compound Assignment
(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
expression equivalent to...
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
9/15/2025 PRESENTATION TITLE 31
Compound Assignment
(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
9/15/2025 PRESENTATION TITLE 32
Increment Operator
• It is used to increase the value of a variable by 1
• Denoted by the symbol ++
• Unary operator and works with single variable
• It cannot increment the value of constant and
expressions
Ex. A++; X++; valid statement
10++; //invalid statement
(a+b)++ or ++(a+b) //invalid
9/15/2025 PRESENTATION TITLE 33
Increment Operator
• It can be used in two forms
• Prefix form
• Postfix form
• Prefix form
• the increment operator is written before the
variable
++y; //it increments the value of variable y by 1
9/15/2025 PRESENTATION TITLE 34
Increment Operator
• Postfix form
• the increment operator is written after the
variable
• y++; //it increments the value of variable y by 1
9/15/2025 PRESENTATION TITLE 35
Difference between Prefix and
Postfix
• when increment operator used independently
prefix and postfix form work similarly
• Ex. Result of A++ and ++A is the same
• But when increment operator used in a larger
expression with other operators, prefix and postfix
forms work differently
Ex. A = ++B
A = B++ are different
9/15/2025 PRESENTATION TITLE 36
Difference between Prefix and
Postfix
• A = ++B
How it works?
1. It increments the value of B by 1
2. It assigns the value of B to A
• It is equivalent to the following two statements
++B;
A = B;
9/15/2025 PRESENTATION TITLE 37
Difference between Prefix and
Postfix
• In postfix form, the statement A= B++
How it works?
1. It assigns the value of B to A
2. It increments the value of B by 1
• It is equivalent to the following two statements
A = B;
B++
9/15/2025 PRESENTATION TITLE 38
Example:
9/15/2025 PRESENTATION TITLE 39
Example
9/15/2025 PRESENTATION TITLE 40
Decrement Operator
• It is used to decrease the value of a variable by 1
• Denoted by the symbol --
• Unary operator and works with single variable
• It cannot decrement the value of constant and
expressions
Ex. A--; X--; valid statement
10--; //invalid statement
(a+b)-- or --(a+b) //invalid
9/15/2025 PRESENTATION TITLE 41
Decrement Operator
• It can be used in two forms
• Prefix form
• Postfix form
• Prefix form
• the deccrement operator is written before the
variable
--y; //it decrements the value of variable y by 1
9/15/2025 PRESENTATION TITLE 42
Increment Operator
• Postfix form
• the decrement operator is written after the
variable
• Y--; //it decrements the value of variable y by 1
9/15/2025 PRESENTATION TITLE 43
Difference between Prefix and
Postfix
• when decrement operator used independently
prefix and postfix form work similarly
• Ex. Result of A-- and --A is the same
• But when decrement operator used in a larger
expression with other operators, prefix and postfix
forms work differently
Ex. A = --B
A = B-- are different
9/15/2025 PRESENTATION TITLE 44
Difference between Prefix and
Postfix
• A = --B
How it works?
1. It decrements the value of B by 1
2. It assigns the value of B to A
• It is equivalent to the following two statements
--B;
A = B;
9/15/2025 PRESENTATION TITLE 45
Difference between Prefix and
Postfix
• In postfix form, the statement A= B--
How it works?
1. It assigns the value of B to A
2. It decrements the value of B by 1
• It is equivalent to the following two statements
A = B;
B--;
9/15/2025 PRESENTATION TITLE 46
Example:
9/15/2025 PRESENTATION TITLE 47
Example
9/15/2025 PRESENTATION TITLE 48
Relational and Comparison
Operator ( ==, !=, >, <, >=, <= )
• Two expressions can be compared using
relational and equality operators. For
example, to know if two values are equal or if
one is greater than the other.
• The result of such an operation is either true
or false (i.e., a Boolean value).
9/15/2025 PRESENTATION TITLE 49
Relational and Comparison
Operator ( ==, !=, >, <, >=, <= )
operator description
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
9/15/2025 PRESENTATION TITLE 50
Relational and Comparison
Operator ( ==, !=, >, <, >=, <= )
Example
(7 == 5) // evaluates to false
(5 > 4) // evaluates to true
(3 != 2) // evaluates to true
(6 >= 6) // evaluates to true
(5 < 5) // evaluates to false
9/15/2025 PRESENTATION TITLE 51
Relational and comparison
operators ( ==, !=, >, <, >=, <= )
• it's not just numeric constants that can be
compared, but just any value, including variables.
• Suppose that a=2, b=3 and c=6, then:
• Example
a = 2;
b = 3;
c = 6;
9/15/2025 PRESENTATION TITLE 52
Relational and comparison
operators ( ==, !=, >, <, >=, <= )
• The assignment operator (operator =, with one equal
sign) is not the same as the equality comparison
operator (operator ==, with two equal signs);
• the first one (=) assigns the value on the right-hand to
the variable on its left, while the other (==) compares
whether the values on both sides of the operator are
equal.
• Therefore, in the last expression ((b=2) == a), we first
assigned the value 2 to b and then we compared it to
a (that also stores the value 2), yielding true.
Logical operators ( !, &&, || )
• Logical Operators are used if we want to
compare more than one condition.
Name of the
Operators Type
Operator
&& AND Operator Binary
|| OR Operator Binary
! NOT Operator Unary
Logical operators ( !, &&, || )
• According to names of the Logical Operators,
the condition satisfied in following situation
and expected outputs are given
Operator Output
Output is 1 only when conditions on both
AND
sides of Operator become True
Output is 0 only when conditions on both
OR
sides of Operator become False
NOT It gives inverted Output
Logical operators ( !, &&, || )
• The operator ! is the C++ operator for the Boolean
operation NOT.
• It has only one operand, to its right, and inverts it,
producing false if its operand is true, and true if its
operand is false
• it returns the opposite Boolean value of evaluating its
operand.
Example:
!(5 == 5) // evaluates to false
(5 == 5) // is true
!(6 <= 4) // evaluates to true because (6 <= 4) would
be false
Logical operators ( !, &&, || )
• The operator &&
corresponds to the Boolean
logical operation AND,
which yields true if both its
operands are true, and false
otherwise.
• The following panel shows
the result of operator &&
evaluating the expression
a&&b:
Logical operators ( !, &&, || )
• The operator ||
corresponds to the
Boolean logical
operation OR, which
yields true if either
of its operands is
true, thus being
false only when both
operands are false.
9/15/2025 PRESENTATION TITLE 58
Operator Precedence
• The order in which different types of
operators in an expression are evaluated
• also known as hierarchy of operators
• Each operator has its own precedence level
• Known as hierarchy of operators
• The operators with higher precedence are
evaluated before the operators with lower
precedence
9/15/2025 PRESENTATION TITLE 59
Operator Precedence
• Any expression given in parentheses is
evaluated first
• Then multiplication(*) and division (/)
• Then plus (+) minus (-)
• In case of parentheses and parentheses, inner
parentheses will be evaluated first.
9/15/2025 PRESENTATION TITLE 60
Example:
10 * 24 / 5 -2 +13
10 * 24 / 5 -2 +13
240/ 5 -2 +13 46+13
48 -2 +13 59
46 +13
9/15/2025 PRESENTATION TITLE 61
Operator Associativity
• It refers to the order in which operation of
same precedence are evaluated
• If an expression contains some operation
that have same precedence level, the
expression is evaluated either from left – to
– right or right – to - left
9/15/2025 PRESENTATION TITLE 62
Operator Associativity
Operators Associativity
() ++(postfix) --postfix Left – to – right
+(unary) -(unary) ++(prefix) --(prefix) Left – to – right
* / % Left – to – right
+ - Left – to – right
= += -= *= /= Right – to - left
9/15/2025 PRESENTATION TITLE 63
Example
Assuming that a=10; b = 20; c = 15; d = 8
Solve the output of this expression:
a*b/(c*31%13) * d
9/15/2025 PRESENTATION TITLE 64
Solution
Step Operator Reduced Expression
1 Unary - a*b / (15 * 31 %13) *d
2 * a*b/ (465 %13) * d
3 % a * b / (10) *d
4 * 200 / (10) * d
5 / 20 * d
6 * 160
9/15/2025 PRESENTATION TITLE 65
Type Casting
• Refers to the process of converting the
data type of a value during execution
• It can be performed in two ways
• Implicit Type Casting
• Explicit Type Casting
9/15/2025 PRESENTATION TITLE 66
Implicit Type Casting
• It is performed automatically by C++
compiler
• Operands in arithmetic operation must
be of similar types
• Expression in which operands are of
different data types is called mixed -
type expressions
9/15/2025 PRESENTATION TITLE 67
Implicit Type Casting
• The result of an expression is evaluated
to larger data type in the expression
• Ex. Expressions contains an integer and
double as operation
• Result will be double data type
9/15/2025 PRESENTATION TITLE 68
Type Casting Order
Highest Data Type
Long
Char Int Long Float Double
double
Lowest Data Type
9/15/2025 PRESENTATION TITLE 69
Implicit Conversion
Expression Intermediate Type
char + float float
int – long long
int * double double
float / long double long double
9/15/2025 PRESENTATION TITLE 70
Implicit Conversion
Suppose x is an integer and y is a long variable :
x+y
What is the data type of the value of x after the
expression evaluated
9/15/2025 PRESENTATION TITLE 71
Explicit Casting
• It is performed by programmer
• It is performed using cast operator
• cast operator, tells the computer to convert
the data type of a value
Syntax
(type) expression;
9/15/2025 PRESENTATION TITLE 72
Explicit Casting
Syntax
(type) expression;
type - it indicates the data type to
which operand is to be converted
expression - it indicates the constant, variable
or expression whose data type is
to be converted
9/15/2025 PRESENTATION TITLE 73
Explicit Casting
Example: Suppose x and y are two float variables,
x contains10.3 and y contains 5.2 and the
following expression is evaluated.
x%y
• This expression will generate errors
• It will be written as:
(int) x % (int) y
9/15/2025 PRESENTATION TITLE 74
sizeof Operator
• It is used to find the size of any data
value
• It give the number of bytes occupied by
that value
Syntax: sizeof(operand);
Operand can be a variable or constant
9/15/2025 PRESENTATION TITLE 75
sizeof Operator
Example
sizeof(n);
sizeof(“English”);
9/15/2025 PRESENTATION TITLE 76
Comments
• It refers to the lines of program that are not
executed
• It ignores by the compiler
• It is used to increase the readability of the
program
• It notes about different lines of codes and its
purpose
9/15/2025 PRESENTATION TITLE 77
Comments
• Two ways of adding comments:
- single line comments, using double slash
// anything after the slash is considered as
comments and it is ignored during execution
- Multi – line comments, it is use /* at the
beginning of the comments, the character */
is used to end the multi – line comments
9/15/2025 PRESENTATION TITLE 78
Comments
Examples
//Practice to be a good programmer
/* This is also a comment
hello and goodbye */
9/15/2025 PRESENTATION TITLE 79
Write a program that reads the radius of a
circle as an integer and prints the circles
diameter, circumference, and area. Use the
constant value 3.14159 for pie. Do all
calculations in output statement.
9/15/2025 PRESENTATION TITLE 80