Open In App

Pre Increment and Post Increment Operator in Programming

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Pre Increment Operator and Post Increment Operator are the two ways of using the Increment operator to increment the value of a variable by 1. They can be used with numeric data values such as int, float, double, etc. Pre-increment and Post-increment perform similar tasks with minor distinctions. In this article, we will discuss the pre-increment and post-increment operators.

What is a Pre-Increment Operator:

In pre-increment operation, the increment operator is used as the prefix of the variable. The increment in value happens as soon as the increment operator is encountered.

Syntax of Pre-Increment

++variable_name

What is a Post-Increment Operator:

In a post-increment operation, the value is incremented after all the other operations are performed i.e. all the other operators are evaluated. The increment operator is used as the suffix to the variable name.

Syntax of Post-Increment

variable_name++

Pre and Post Increment Operator in C:

Here are the implementation of Pre and Post Increment Operator in C language:

C
// C Program to illustrate the pre-increment and post
// increment
#include <stdio.h>

int main()
{

    int num1 = 15, num2 = 20;
    // Post increment
    int postNum = num1++;
    // Pre increment
    int preNum = ++num2;

    // Printing value of postNum
    // after post increment.
    printf("postNum = %d \n", postNum);
    printf("num1 = %d", num1);

    // Printing new line
    printf("\n");

    // Printing value of preNum
    // after pre increment.
    printf("preNum = %d \n", preNum);
    printf("num2 = %d", num2);

    return 0;
}

Output
postNum = 15 
num1 = 16
preNum = 21 
num2 = 21

Pre and Post Increment Operator in C++:

Here are the implementation of Pre and Post Increment Operator in C++ language:

C++
#include <iostream>
using namespace std;

int main()
{
    int num1 = 15, num2 = 20;
    // Post Increment
    int postNum = num1--;
    // Pre Increment
    int preNum = --num2;

    // Printing value of postNum after post Increment.
    cout << "postNum = " << postNum << endl;
    cout << "num1 = " << num1 << endl;

    // Printing value of preNum after pre Increment.
    cout << "preNum = " << preNum << endl;
    cout << "num2 = " << num2 << endl;

    return 0;
}

Output
postNum = 15
num1 = 14
preNum = 19
num2 = 19

Pre and Post Increment Operator in Java:

Here are the implementation of Pre and Post Increment Operator in java language:

Java
public class Main {
    public static void main(String[] args)
    {
        int num1 = 15, num2 = 20;
        // Post increment
        int postNum = num1++;
        // Pre increment
        int preNum = ++num2;

        // Printing value of postNum after post increment.
        System.out.println("postNum = " + postNum);
        System.out.println("num1 = " + num1);

        // Printing value of preNum after pre increment.
        System.out.println("preNum = " + preNum);
        System.out.println("num2 = " + num2);
    }
}

Output
postNum = 15
num1 = 16
preNum = 21
num2 = 21

Pre and Post Increment Operator in Python:

Here are the implementation of Pre and Post Increment Operator in python language:

Python
num1 = 15
num2 = 20
# Post increment
postNum = num1
num1 += 1
# Pre increment
preNum = num2 - 1
num2 += 1

# Printing value of postNum after post increment.
print("postNum =", postNum)
print("num1 =", num1)

# Printing value of preNum after pre increment.
print("preNum =", preNum)
print("num2 =", num2)

Output
('postNum =', 15)
('num1 =', 16)
('preNum =', 19)
('num2 =', 21)

Pre and Post Increment Operator in C#:

Here are the implementation of Pre and Post Increment Operator in C# language:

C#
using System;

class Program {
    static void Main()
    {
        int num1 = 15, num2 = 20;
        // Post Increment

        int postNum = num1++;
        // Pre INcrement

        int preNum = ++num2;
        // Printing value of postNum after post Increment.
        Console.WriteLine("postNum = " + postNum);
        Console.WriteLine("num1 = " + num1);

        // Printing value of preNum after pre Increment.
        Console.WriteLine("preNum = " + preNum);
        Console.WriteLine("num2 = " + num2);
    }
}

Output
postNum = 15
num1 = 16
preNum = 21
num2 = 21

Pre and Post Increment Operator in Javascript:

Here are the implementation of Pre and Post Increment Operator in javascript language:

JavaScript
let num1 = 15, num2 = 20;

// Post Increment
let postNum = num1++; 

// Pre Increment
let preNum = ++num2; 

// Printing value of postNum after post Increment.
console.log("postNum = " + postNum);
console.log("num1 = " + num1);

// Printing value of preNum after pre Increment.
console.log("preNum = " + preNum);
console.log("num2 = " + num2);

Output
postNum = 15
num1 = 16
preNum = 21
num2 = 21

Application of Pre and Post Increment:

  1. Looping:You can use both pre and post increment operators to make loops run a certain number of times. For example, if you want to repeat something ten times, you might use one of these operators to count the repetitions.
  2. Adding One: If you want to increase a number by one, you can use either pre or post increment operators. They help you quickly add one to a variable’s value.
  3. Doing Math: Whether you’re adding, subtracting, or doing other math operations, both operators can help you change a variable’s value as part of the calculation.
  4. Changing Things One at a Time: When you need to change a value step by step, these operators come in handy. They let you modify a number in small increments, like going up one step at a time on a staircase.
  5. Controlling the Order: Sometimes, you want to make sure things happen in a specific order. Both pre and post increment operators help with this, ensuring that actions occur in the sequence you intend.

Conclusion:

Pre and post-increment operators are fundamental in programming for increasing variable values. They are extensively utilized in loops, array manipulation, iterator adjustment, control flow, and performance enhancement. Understanding the distinction between pre and post-increment operators and their appropriate application can lead to the creation of efficient and comprehensible code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads