Tokens & Manipulators in CPP

 


 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.


Variables: Identifiers for variables serve as labels for data values. A variable identifier can represent a raw data type, such as an integer or character, as well as a class instance.

Functions: Function identifiers name a block of code designed to perform a specific task. When the function is called using its identifier, the block of code executes.

Arrays: An array identifier refers to a collection of elements that are of the same type. The identifier allows access to the array and its elements through indexing.


 

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   


// Working of arithmetic operators
#include <iostream>
using namespace std;

int main() {
    int a = 9,b = 4;

    cout<<"a+b = "<<a+b<<endl;

    cout<<"a-b = "<<a-b<<endl;

    cout<<"a*b ="<<a*b<<endl;

    cout<<"a/b = "<<a/b<<endl;

    cout<<"Remainder when a divided by b =  "<<a%b;

    return 0;
}

 


// Working of increment and decrement operators
#include <iostream>
using namespace std;

int main()
{
    int a = 10, b = 100;
    float c = 10.5, d = 100.5;

    cout<<"++a =  "<< ++a;
    cout<<"--b =  "<< --b;
    cout<<"++c =  "<< ++c;
    cout<<"--d =  "<< --d;

    return 0;
}


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



// Working of assignment operators
#include <iostream>
using namespace std;

int main()
{
    int a = 5, c=20;

    c += a;    
    cout<<"c = "<< c;
    c -= a;  
    cout<<"\nc = "<< c;
    c *= a;    
    cout<<"\nc = "<< c;
    c /= a;    
    cout<<"\nc = "<< c;
    c %= a;    
    cout<<"\nc = "<< c;

    return 0;
}


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)




#include <iostream>
using namespace std;

int main() {
    bool result;

    result = (3 != 5) && (3 < 5);     // true
    cout << "(3 != 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 < 5);    // false
    cout << "(3 == 5) && (3 < 5) is " << result << endl;

    result = (3 == 5) && (3 > 5);    // false
    cout << "(3 == 5) && (3 > 5) is " << result << endl;

    result = (3 != 5) || (3 < 5);    // true
    cout << "(3 != 5) || (3 < 5) is " << result << endl;

    result = (3 != 5) || (3 > 5);    // true
    cout << "(3 != 5) || (3 > 5) is " << result << endl;

    result = (3 == 5) || (3 > 5);    // false
    cout << "(3 == 5) || (3 > 5) is " << result << endl;

    result = !(5 == 2);    // true
    cout << "!(5 == 2) is " << result << endl;

    result = !(5 == 5);    // false
    cout << "!(5 == 5) is " << result << endl;

    return 0;
}



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



#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 3;
    b = 5;
    bool result;

    result = (a == b);   // false
    cout << "3 == 5 is " << result << endl;

    result = (a != b);  // true
    cout << "3 != 5 is " << result << endl;

    result = a > b;   // false
    cout << "3 > 5 is " << result << endl;

    result = a < b;   // true
    cout << "3 < 5 is " << result << endl;

    result = a >= b;  // false
    cout << "3 >= 5 is " << result << endl;

    result = a <= b;  // true
    cout << "3 <= 5 is " << result << endl;

    return 0;
}



(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








Post a Comment

0 Comments