Increment ++ and Decrement -- Operator as Prefix and Postfix (2024)

In this article, you will learn about the increment operator ++ and the decrement operator -- in detail with the help of examples.

In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.

a = 5++a; // a becomes 6a++; // a becomes 7--a; // a becomes 6a--; // a becomes 5

Simple enough till now. However, there is an important difference when these two operators are used as a prefix and a postfix.

++ and -- operator as prefix and postfix

  • If you use the ++ operator as a prefix like: ++var, the value of var is incremented by 1; then it returns the value.
  • If you use the ++ operator as a postfix like: var++, the original value of var is returned first; then var is incremented by 1.

The -- operator works in a similar way to the ++ operator except -- decreases the value by 1.

Let's see the use of ++ as prefixes and postfixes in C, C++, Java and JavaScript.

Example 1: C Programming

#include <stdio.h>int main() { int var1 = 5, var2 = 5; // 5 is displayed // Then, var1 is increased to 6. printf("%d\n", var1++); // var2 is increased to 6 // Then, it is displayed. printf("%d\n", ++var2); return 0;}

Example 2: C++

#include <iostream>using namespace std;int main() { int var1 = 5, var2 = 5; // 5 is displayed // Then, var1 is increased to 6. cout << var1++ << endl; // var2 is increased to 6 // Then, it is displayed. cout << ++var2 << endl; return 0;}

Example 3: Java Programming

class Operator { public static void main(String[] args) { int var1 = 5, var2 = 5; // 5 is displayed // Then, var1 is increased to 6. System.out.println(var1++); // var2 is increased to 6 // Then, var2 is displayed System.out.println(++var2); }}

Example 4: JavaScript

let var1 = 5, var2 = 5;// 5 is displayed// Then, var1 is increased to 6console.log(var1++)// var2 is increased to 6// Then, var2 is displayedconsole.log(++var2)

The output of all these programs will be the same.

Output

56
Increment ++ and Decrement -- Operator as Prefix and Postfix (2024)

FAQs

What is i ++ and ++ i explain with an example? ›

2) Pre-Increment(++i): We use ++i in our statement if we want to increment the value of i by 1 and then use it in our statement. Example int i = 3; int a = i++; // a = 3, i = 4 int b = ++a; // b = 4, a = 4.

What is increment and decrement in both prefix and postfix in Java? ›

Increment ++ and Decrement -- Operator as Prefix and Postfix

In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.

What is increment and decrement operator in C++? ›

Definition. Increment Operator is used to increase the value of the operand by 1 whereas the Decrement Operator is used to decrease the value of the operand by 1. In C++, the value of the variable is increased or decreased by 1 with the help of the Increment operator and the Decrement Operator.

What does ++ i and i ++ mean in C? ›

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.

What is the meaning of ++ J and J ++ in C? ›

In the case of “j++”, the operator is being used as a postfix operator, meaning it comes after the variable, and in “++j”, it is being used as a prefix operator, meaning it comes before the variable.

Is ++ i the same as i += 1? ›

These two are exactly the same. It's just two different ways of writing the same thing. i++ is just a shortcut for i += 1 , which itself is a shortcut for i = i + 1 .

What is ++ i and i ++ in Java? ›

i++ and ++i are very similar but not exactly the same. Both increment the number, but ++i increments the number before the current expression is evaluted, whereas i++ increments the number after the expression is evaluated. int i = 3; int a = i++; // a = 3, i = 4 int b = ++a; // b = 4, a = 4.

What is the difference between prefix and postfix increment (++ A and A ++)? ›

The prefix and postfix increment both increase the value of a number by 1. The only difference between the two is their return value. The former increments ( ++ ) first, then returns the value of x , thus ++x . The latter returns the value of x first, then increments ( ++ ), thus x++ .

What will happen ++ i ++ in code? ›

++i will increment the value of i , and then return the incremented value. i++ will increment the value of i , but return the original value that i held before being incremented.

What are the 2 types of increment and decrement operators? ›

Types of Increment and Decrement Operators in C

Postfix Increment operator. Postfix Decrement operator.

Is A ++ and ++ a same in C? ›

As you can see, a++ returns the value of a before incrementing. And ++a returns the value of a after it has been incremented.

What is meaning of ++ prefix in C? ›

Remarks. The prefix increment operator (++) adds one to its operand; this incremented value is the result of the expression.

What does i ++ stand for? ›

i++ increment the variable i by 1. It is the equivalent to i = i + 1. i– decrements (decreases) the variable i by 1.

What does == mean in C++? ›

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( !=

What does += mean in C? ›

+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A.

What is difference between ++$ J and J ++? ›

There is no difference between ++$j and $j++ unless the value of $j is being tested, assigned to another variable, or passed as a parameter to a function.

What is %d in C programming? ›

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

Which one is faster i ++ or i 1 explain? ›

As i++ does automatic typecasting and uses a compiler instruction which internally uses iadd instruction, i=i+1 is faster than i++.

What is a ++ equivalent to? ›

a += b; is equivalent to a = a + b; . a =+ b; is equivalent to a = +b; . This means +b (positive) is assigned to variable a . a++ is post increment of variable a , meaning the value of the variable is used before incrementing by 1 .

What does i += 1 mean in C++? ›

i += 1 produces i after incrementing (like a preincrement), while i++ produces i before incrementing. Thus, int a = 0, b = 0; cout << (a+=1) << " " << b++ << endl; prints 1 0 .

What does i += 2 mean? ›

Using += in loops

You can use += in for loop when you want to increment value of variable by more than 1. In general, you might have used i++ , but if you want to increment it by 2, then you can use i+=2 .

What is ++ and += in Java? ›

scoreTeamB++ returns the previous value of the variable (before it was incremented). += returns the value that was assigned to the variable.

What is == in Java? ›

Both equals() method and the == operator are used to compare two objects in Java. == is an operator and equals() is method. But == operator compares reference or memory location of objects in a heap, whether they point to the same location or not.

What is i += 2 in Java? ›

Using += in Java Loops

The += operator can also be used with for loop: for(int i=0;i<10;i+=2) { System. out. println(i); } The value of i is incremented by 2 at each iteration.

Is A ++ a postfix operator? ›

The effect of applying the postfix increment operator (++) is that the operand's value is increased by one unit of the appropriate type. Similarly, the effect of applying the postfix decrement operator (--) is that the operand's value is decreased by one unit of the appropriate type.

What is postfix and prefix with example? ›

A + B * C would be written as + A * B C in prefix. The multiplication operator comes immediately before the operands B and C, denoting that * has precedence over +. The addition operator then appears before the A and the result of the multiplication. In postfix, the expression would be A B C * +.

What is postfix and prefix operators with example? ›

In prefix, operators are written before their operands. Example:++10. In postfix, operators are written after their operands. Example: 10++

What is difference between a ++ and ++ A? ›

++a returns the value of an after it has been incremented. It is a pre-increment operator since ++ comes before the operand. a++ returns the value of a before incrementing. It is a post-increment operator since ++ comes after the operand.

WHY IS codes are used? ›

Coding creates a set of instructions for computers to follow. These instructions determine what actions a computer can and cannot take. Coding allows programmers to build programs, such as websites and apps. Computer programmers can also tell computers how to process data in better, faster ways.

Why is a code important? ›

A code has value as both an internal guideline and an external statement of corporate values and commitments. A well-written code of conduct clarifies an organization's mission, values and principles, linking them with standards of professional conduct.

Which type of operator ++ is? ›

Example 2: Increment and Decrement Operators

In the above program, we have used the ++ and -- operators as prefixes (++a and --b). However, we can also use these operators as postfix (a++ and b--). To learn more, visit increment and decrement operators.

What is the name of the ++ operator? ›

Increment Prefix

Is ++ an increment operator? ›

In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol.

Is ++ A and A ++ is same with example? ›

Difference between ++a and a++

The difference lies in which value is used in the rest of the expression containing it. With ++a, a is incremented first, and that is the value you get; with a++ you get the value of a, and a is incremented afterwards.

What does += mean? ›

The addition assignment ( += ) operator adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible.

Does C have ++ operator? ›

C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.

What are the 15 prefixes? ›

The prefixes are: de-, dis-, ex-, im-, in-, mis-, non-, pre-, pro-, re-, un-, and uni-.

Is ++ A Faster Than A ++? ›

++i is sometimes faster than, and is never slower than, i++.
...
13.1What's the deal with operator overloading?
13.14Overloading prefix/postfix forms of operators ++ and --?
13.15Which is more efficient: i++ or ++i?
12 more rows

Can we use ++ and -- for expressions? ›

C static code analysis: Increment (++) and decrement (--) operators should not be used in a method call or mixed with other operators in an expression.

What does ++ mean in text? ›

It's shorthand for add 1.

What is the full from of BS? ›

Bachelor of Science in British English.

Does i stand for math? ›

Definition and meaning of the math word i. i. The letter i is used to signify that a number is an imaginary number. It stand for the square root of negative one. In electrical engineering it is often replaced by the letter j to avoid conflict with the symbol for current.

What is the use of * and & in C++? ›

C++ provides two-pointer operators, which are Address of Operator (&) and Indirection Operator (*). A pointer is a variable that contains the address of another variable or you can say that a variable that contains the address of another variable is said to "point to" the other variable.

What is %s and %D in C++? ›

%s tells printf that the corresponding argument is to be treated as a string (in C terms, a 0-terminated sequence of char ); the type of the corresponding argument must be char * . %d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int .

What does += in C++ means? ›

+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A.

What is the * symbol in C? ›

Asterisk (*) − It is used to create a pointer variable.

What are data types in C? ›

Types of Data Types in C

Floating-point, integer, double, character. Union, structure, array, etc. The basic data types are also known as the primary data types in C programming.

Is += different from =+ in C? ›

In modern C, or even moderately ancient C, += is a compound assignment operator, and =+ is parsed as two separate tokens. = and + . Punctuation tokens are allowed to be adjacent.

What is I in programing? ›

i = integer. Comes from Fortran where integer variables had to start with the letters I through N and real variables started with the other letters. Thus I was the first and shortest integer variable name.

What is the difference between I -- and -- I in Java? ›

--i is pre-decrement and i-- is post-decrement.

What does ++ i mean? ›

i++ increment the variable i by 1. It is the equivalent to i = i + 1. i– decrements (decreases) the variable i by 1.

What is diff between i ++ and ++ i? ›

The only difference is the order of operations between the increment of the variable and the value the operator returns. So basically ++i returns the value after it is incremented, while i++ return the value before it is incremented. At the end, in both cases the i will have its value incremented.

What is I in for loop ++ i? ›

Both i++ and ++i are short-hand for i = i + 1 . In addition to changing the value of i, they also return the value of i, either before adding one ( i++ ) or after adding one ( ++i ). In a loop the third component is a piece of code that is executed after each iteration.

Which is faster ++ i or i ++? ›

Though we can say that the ++i is slightly faster than i++. The i++ takes local copy of the value of i before incrementing, while ++i never does.

What is the difference between & in Java? ›

& is a bitwise operator and compares each operand bitwise. It is a binary AND Operator and copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100. Whereas && is a logical AND operator and operates on boolean operands.

What does *= mean in Java? ›

The multiplication assignment ( *= ) operator multiplies a variable by the value of the right operand and assigns the result to the variable.

Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6388

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.