Unit-2 C++ Basics-Lesson 2
Unit-2 C++ Basics-Lesson 2
Unit-2 C++ Basics-Lesson 2
UNIT
2 C++
BASICS
LESSON 2:
Basic Elements of a Program
OBJECTIVES:
At the end of the lesson, students will be able to:
Symbols
Comment symbol
The program that you write should be direct not only to you but also to the reader itself.
Part of good programming is the including of comments in the program. Generally,
comments can be used to explain the program’s purpose or explain the meaning of
key statements in the program. The compiler ignores these comments when it
translates the program into executable code.
For example:
For example:
Reserved Word
The second category of tokens is a reserved word. Reserved words are also called
keywords that cannot be redefined within any program; that is, they can not be applied
for anything other than their expected use. Some of the essential reserved words are
given below:
The reserved words of C++ may be strategically located in some groups. In the first
group, were also present in the C programming language and have been continued
over into C++. There are 32 such reserved words:
There are other 30 reserved words that were not in C, are therefore new to C++:
The following 11 C++ reserved words are not vital when the standard ASCII character
set is being used. Still, they have been added to give more readable alternatives for
some of the C++ operators, and also to encourage programming with character sets
that lack characters needed by C++.
A specific compiler may not be completely modern, which implies that some (and possibly many) of the
reserved words in the lead up to two groups may not yet be actualized.
There is a differentiation between reserved words and predefined identifiers, which are once in a while
collectively referred to as keywords. Nonetheless, be aware that the terminology is nonstandard. As an
illustration, some authors use keywords in the same sense that others use the reserved word.
Identifier
The third category of tokens is the identifier. Identifier refers to the names of variables,
constants, functions, and other objects defined in a C++ program. It allows us to name
data and other objects in the program. Each identified object in the computer is stored
at a unique address. If we did not have identifiers that we could use to represent data
locations symbolically, we would have to know and use the object's addresses.
Instead, we simply give data identifiers and let the compiler keep track of where they
are physically located. Different programming languages use different syntactical rules
to form identifiers.
▪ The only valid name symbols are the capital letters A through Z, the lowercase
letters a through z, digits 0 – 9, and underscore.
▪ The first character cannot be a digit or an underscore.
▪ An identifier cannot contain punctuation marks, math symbols, or other special
symbols.
▪ ANSI guarantees only the first 32 characters to be significant.
▪ C++ identifier is case-sensitive,
▪ The last rule is that the name we create cannot be keywords. Keywords are
also known as reserved words.
Example:
Data Types
A data type of a variable in the operating system assigns memory and decides what
can be stored in the reserved memory.
Some of the basic types can be modified using one or more of these type modifiers:
▪ signed
▪ unsigned
▪ short
▪ long
The following table shows the type of variable, how much memory it takes to store
the value in memory, and the maximum and minimum value stored in such variables.
The data type string is a computer programmer-defined type of data. Unlike primitive
data type, a string cannot be directly available for use in a program. To use this type,
you need to access program components from the C++ library.
Variable
A variable is used to hold data. The data stored in a variable is called its value.
datatype identifier;
int x;
float rate;
char answer;
IT 103: COMPUTER PROGRAMMING 1 64
UNIT 2: C++ Basics
Note:
The sizes of variables might be different from those shown in the above table
depending on the compiler and the computer you are using.
Operators
▪ Assignment Operator
▪ Arithmetic Operators
▪ Relational Operators
▪ Logical Operators
Assignment Operator
The assignment operator (=) is used in the assignment statement, which takes the
following form:
variable = expression;
To evaluate assignment statement, the expression on the right-hand side of the equal
sign is assessed first, and then the variable on the left-hand side is set to this value.
A variable needs to be initialized the first time a value is placed in the variable,
int x =10;
double pi = 3.14;
char ans = ‘y’;
Arithmetic Operator
Symbol Name Example Definition
It is taking two or more numbers
and adding them together. The
+ Addition x+y
result of an addition is called
sum.
represents the operation of
removing objects from a
- Subtraction x–y
collection. The result of
subtraction is called a difference.
one of the four basic operations
of arithmetic gives the result of
* Multiplication x* y combining groups of equal sizes.
The result of a multiplication is
called a product.
It is a method of distributing a
/ Division x/y
group of things into equal parts.
It is placed between
two expressions that have the
= Equal x=y same value, or for which one
studies the conditions under
which they have the same value
Modulus or It is the remainder after dividing
% x%y
Modulo one number by another.
++ Increment x ++ or ++ x Increases the value of x by 1
-- Decrement y ++ or -- y Decreases the value of x by 1
+ (unary) Positive +x The value of x
- (unary Negative -x The arithmetic negation of x
The modulo operator, represented by a percentage sign (%), gives the remainder of a
division process.
For example:
x = 10 % 3;
Order of precedence
It is likely to build mathematical expressions with several operators. When more than
one arithmetic operator is used in an expression, C++ uses the operator precedence
rules to assess the expression. In line with the order of precedence rules for arithmetic
operators,
However, when operators have the same level, the operations are performed from
left to right.
What value will be stored in x? The answer is 14 because the order of operations
dictates that the division operator works before the addition operator does.
Expression Value
5 + 2 *4 13
10 / 2 – 3 2
8 + 12 * 2 – 4 28
6–3*2+7–1 6
(5 + 2) * 4 28
10 / (5 – 3) 5
8 + 12 * (6 – 2) 56
(6 – 3) * (2 + 7) / 3 9
You probably remember from algebra class that the expression 2xy is understood to
mean 2 times x times y. in math, you do not always use an operator for multiplication.
Programming languages, however, require an operator for any mathematical
operation.
Programming
Algebraic Expression
Expression
6B 6*B
(3)(12) 3 * 12
4xy 4*x*y
𝑥
y = 32 y=x/2*3
z = 3bc + 4 z = 3 * b *c + 4
𝑥+2
a = 𝑎−1 a = (x + 2) / (a – 1)
Increment (++) and Decrement (--) operators are placed either in front or after the
operand. The statements
++x; or ++x;
- -y; or y- -;
When used in assignment statements, however, placing the operators after the
variable takes a different meaning.
In the assignment statement for the variables y and z, the increment and decrement
operators are placed after the variable operands. As such, the operators are called
post-increment and post-decrement operators, respectively. This means that the
variable operands will be incremented/decremented after the expressions have been
thoroughly evaluated. In the statement
y=x++;
The value of the variable x is assigned first to the variable y before it is incremented.
Thus, after the statement is executed, x=6, and y=5. In the statement
z=y- -*2;
Example 1 Example 2
x=4;
y=++x; // x contains x=5;
5, y contains y=x++;// x contains
5 6, y contains 5
x += 2; x = x + 2;
y - = 5; y = y - 5;
a /= 2; a = a / 2;
b * = c; b = b * c;
z %=10 z = z % 10;
Relational Operators
Two expressions can be collate using relational operators. For example, to know if
two values are equal or if one is less than the other.
The result of such an operation is either true or false (i.e., a Boolean Value)
Relational Operator
Symbol Name Example Definition
Compares if the value of the
< Less than x<y left operand is less than the
value of the right operand.
Compares if the value of the
left operand is greater than
> Greater than x>y
the value of the right
operand.
Compares if the value of the
left operand is less than or
<= Less than or equal to x <= y
equal to the value of the
right operand.
Compares if the value of the
Greater than or equal left operand is greater than
>= x>=y
to or equal to the value of the
right operand.
Compares if the values of
== Equal to x == y two operands are equal or
not.
Compares if the values of
!= Not Equal x != y two operands are equal or
not.
Boolean Expressions
(7 == 5) // false
(5 > 4) // true
(3 != 2) // true
(6 >= 6) // true
(5 < 5) // false
Of course, it is not only numeric constants that can be linked but only any value,
including variables.
// false,
(a = = 4)
since a is not equal to 4
// true,
(a * b >= c)
since (2*3 >= 6) is true
// false,
( b+4 > a)
since (3+4 > 2*6) is false
Logical Operator
Symbol Name Example Definition
((a == 5) && (b> 6)) Logical AND operator. If
// evaluates to false both the operands are non-
&& And
zero, then the condition
( true && false )
becomes true.
((a == 5) || (c>8))
Logical OR Operator. If
any of the two operands is
|| Or // evaluates to true
( true || false ) non-zero, then condition
becomes true.
!(a == 5)
// evaluates to false Logical NOT Operator.
because the Use to reverses the logical
state of its operand. If a
! Not expression at its
condition is true, then
right (a == 5) is
Logical NOT operator will
true make false.