Pre Increment and Post Increment Operator in Programming
Last Updated :
26 Mar, 2024
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;
}
OutputpostNum = 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;
}
OutputpostNum = 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);
}
}
OutputpostNum = 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);
}
}
OutputpostNum = 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);
OutputpostNum = 15
num1 = 16
preNum = 21
num2 = 21
Application of Pre and Post Increment:
- 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.
- 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.
- 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.
- 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.
- 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.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read