Post ,Pre Increment and Decrement Operator


Increment and Decrement Operator

There are many places in the program where the value of a variable should be incremented by 1 or decremented by 1 for example i=i+1; it can be written also i++;

Increment Operator

++ is an increment operator. This is a unary operator. It increments the value of a variable by one . The increment operator is classified into two categories as

  • post increment i++ increments value of i by one
  • Pre Increment ++I increments value f I by one

Post- Increment

If the increment operator ++ is placed immediately after (post) the operand then the operator is called post increment. As the name indicates post increment means increment after (post) the operand value is used so operand value is used first and then the operand value is incremented by 1.

void main()
{
   int i=20,b;
   b=i++;
   printf(“%d”,i);
    printf(“%d”,b);
}
Output

Output will be
21 20

Pre-Increment

If the increment operator ++ is placed before (pre) the operand then the operator is called Pre increment. As the name indicates pre increment means increment before (pre) the operand value is used, So operand value is incremented by 1 and this incremented value is used .

void main()
{
   int i=20,b;
   b=++i;
   printf(“%d”,i);
   printf(“%d”,b);
}

Output will be
21 21

Decrement Operator


-- is a decrement operator. This is a unary operator. It decrement the value of a variable by one . The decrement operator is classified into two categories as :-

  • Post Decrement i-- decrement value of i by one
  • Pre Decrement --i decrement value of i by one

Post Decrement

If the decrement operator -- is placed immediately after (post) the operand then the operator is called post decrement. As the name indicates post decrement means decrement after (post) the operand value is used so operand value is used first and then the operand value is decrement by 1.


void main()
{
   int i=20,b;
   b=i--;
   printf(“%d”,i);
    printf(“%d”,b);
}

Output will be
19 20

Pre Decrement

If the decrement operator -- is placed before (pre) the operand then the operator is called Pre decrement. As the name indicates pre decrement means decrement before (pre) the operand value is used, So operand value is decrement by 1 and this decrement value is used .


void main()
{
   int i=20,b;
   b=--i;
   printf(“%d”,i);
   printf(“%d”,b);
}

Output will be
19 19

What is Looping Statement ?

A Loop executes the sequence of statements many times until the stated condition becomes false. The statement that help us to execute a set of statements repeatedly are called loop control statements.


Note: "A set of statements may have to be repeatedly executed for a specified number of times or till a condition is satisfied is done through looping concept."

Types of looping construct in C:-


  1. while loop
  2. for loop
  3. nested for loop
  4. do-while loop