Tokens are the smallest individual units of the program
Types:
1. Keywords.
2. Identifiers
3. String
4. Constants
5. Operators
Keywords
- Keywords have set meanings, which cannot be modified.
- There are 32 keywords in 'C++'.
- Keywords are typed in lowercase letters.
- These keywords provide developers with the essential building blocks of C++ syntax and semantics, allowing them to effectively specify a wide range of programming constructions and operations.
- To build correct and productive C++ programs, one must first understand their meanings and applications.
Example
int
double
float
class
for
Identifiers
In programming languages such as Java, identifiers are the names assigned by the programmer to entities such as variables, functions, arrays, classes, and objects. These names are used to uniquely identify these entities inside their respective scopes.
Following rules must be followed for identifiers:
The first character must always be an alphabet or an underscore.
It should be formed using only letters, numbers, or underscore.
A keyword cannot be used as an identifier.
It should not contain any whitespace character.
The name must be meaningful.
Example of valid identifiers
Sum_1, result
Example of invalid identifiers
1Sum -> starts with number
Sum*1 -> * (asterisk) not allowed
Constants
Constants are the fixed values that never change during the execution of a program.
Various types of constants:
Integer Constants
An integer constant is nothing but a value consisting of digits or numbers
Example : 111, 1234
Floating point Constants
constants that contain a decimal point or a fraction value.
Example: 223.14, 400.054
Character Constants
A character constant contains only a single character enclosed within a single quote (‘ ').
Example: ‘A’ , ‘m’, ‘9’
String Constants
A string constant contains a sequence of characters enclosed within
double quotes (“ ").
Example: “Welcome”, “Hello”
Strings
Strings are nothing but an array of characters ended with a null character (‘\0’)
Strings are always enclosed in double quotes.
Example: “Welcome”
Operators
Operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations
Example: +, -, /, *, ++, --
1. Arithmetic Operators
Arithmetic operators are used for performing basic mathematical operations on operands.
Operator | Name | Description | Example |
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x |
-- | Decrement | Decreases the value of a variable by 1 | --x |
2. Assignment Operators
• Assignment operators are used to assign value to a variable.
Operator | Example | Same As |
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
&= | x &= 3 | x = x & 3 |
|= | x |= 3 | x = x | 3 |
^= | x ^= 3 | x = x ^ 3 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
3. Logical Operators
• Logical operators are used for evaluating a combination of conditions/constraints to get a resultant value.
• The result of the evaluation of a Boolean expression is Boolean which is either true or false.
Operator | Name | Description | Example |
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
4. Comparison Operators
• Relational or comparison operators are used to compare two operands.
• The result of the evaluation is either true or false.
Operator | Name | Example |
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
(Other Operators)
scope resolution operator is ::
Use of scope resolution operator: To access a global variable when there is a local variable with same name
Example
int x=20; // Global x
void main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
}
Output
Value of global x is 20
Value of local x is 10
0 Comments