ELEMENTS OF C++
1. CHARACTER SET
• A C++ program is constructed as a sequence of
characters.
• Among the characters that can be used in the program are:
▫ Lowercase letters (a – z)
▫ Uppercase letters (A – Z)
▫ Digits (0 – 9)
▫ Special characters (+, /, -, *, =, (, ……)
2. IDENTIFIERS
• A valid identifier is a sequence of one or more letters, digits or underline
symbols ( _ ). The length of an identifier is not limited, although for some
compilers only the 32 first characters of an identifier are significant (the rest are
not considered).
• Neither spaces nor marked letters can be part of an identifier. Only
letters, digits and underline characters are valid. In addition,
variable identifiers should always begin with a letter. They can also
begin with an underline character ( _ ), but this is usually reserved
for external links. They can never begin with a digit.
•
Naming VARIABLES correctly:
• It should start with letter or an underscore ( _ ).
• No spaces.
• No special characters.
3. OPERATORS
A. Arithmetic operators ( +, -, *, /, % )
• Precedence of Operators:
SYMBOL Hierarchy
() 1
*,/,% 2
+,- 3
= 4
ARITHMETIC OPERATORS Examples
Math Expression C++ Expression
1. a=5+3
2
2. C = ( 6 – 2)2 + 10
3. OPERATORS
B. Logical and Relational operators
• Precedence of Operators:
SYMBOL Hierarchy
() 1
! (NOT) 2
>=, >, <=, < 3
= = , != 4
&&(AND) , 5
|| (OR)
LOGICAL OPERATORS Examples
LOGICAL OPERATORS Examples
1.
(5==5) && (1>5) 2.((3>2)||(6==6))&&(3<23)
4. DATA TYPES
Among the that can be used in the program are:
▫ int ( whole numbers)
▫ float (with decimal part )
▫ double (higher than float)
▫ char (character and integers)
▫ void (valueless)
DATA TYPES Examples:
We can it in, variable declaration
▫ int ( whole numbers)
int x; int a=12;
▫ float (with decimal part )
float y=1.5; float grade=88.89;
▫ double (higher than float)
double c=286521. 286521;
▫ char (charters or integers)
char quiz1; char num2;
▫ void (valueless)
int void; float void;