A Lesson2-Basic-Elements-of-a-Program
A Lesson2-Basic-Elements-of-a-Program
C++ Basics
• Symbols
+ - * / mathematical symbols
<= != == >= relational operators
• Symbols
Comment symbols
// single-line comment symbol
For example,
// This is my first program
// C++ Language
• Reserved Words
Reserved words are also called keywords that cannot be redefined
within any program; that is, they cannot be used for anything
other than their intended use.
Some of the important reserved words are :
char float int void
break if else switch
case return for do
while main
• 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.
• Identifier
Rules for identifiers are:
•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.
• Identifier
Examples of Valid and Invalid identifiers
Valid Identifiers Invalid Identifiers
grade grade+3
area1 1area
area_of_triangle area of triangle
• Data Type
A data type defines a set of values that a variable can hold.
Based on the data type of a variable; the operating system
allocates memory and decides what can be stored in the
reserved memory.
Type Keyword Data type Examples
Boolean boolean
Character char boolean true, false
Integer int
Floating point float char ‘A’ , ‘b’ ,’+’
Double floating double
point int 10,200,550
Valueless void
float /double 10.54, 2.56
• Variable
• Variable
Variable Declaration
datatype identifier;
For example:
int x;
• Operator
Types of operators:
•Assignment Operator
•Arithmetic Operators
•Relational Operators
•Logical Operators
• Operator
•Assignment Operator
variable = expression;
Example:
y = 5;
• Operator
•Arithmetic Operators
Operator Meaning Example
* Multiplication x*y
/ Division x/y
% Modulus x%y
+ Addition x+y
- Subtraction x–y
++ Increment x++
-- Decrement x--
+ (unary) Positive sign +x
- (unary) Negative sign -x
• Operator
Order of Precedence
However, when operators have the same level, the operations are performed
from left to right.
• Operator
•Relational Operators
• Expression
Arithmetic Expression