You are on page 1of 3

Word to PDF - UnRegistered

http://www.word-to-pdf.abdio.com/

(
Word
to
PDF Unreg

istered ) http://www.word-to-pdf.abdio.com/
Skip to main content
Home
Search form
Search

Home
C Programming
C++ Programming
Python

Recent Articles

Interpreter Vs Compiler : Difference Between Interpreter and Compiler


Insertion Sort Algorithm in Programming
Creating Algorithm in Programming
Flowchart In Programming
Bubble Sort Algorithm in Programming
Bitwise Operators in C programming
C++ Programming Source Code to Print Pyramid and Triangles
C Programming Code To Create Pyramid and Structure
Difference in Increment ++ Operator as Prefix and Postfix
Selection Sort Algorithm In Programming

Follow Us
Programiz.com Facebook Page
Programiz.com Twitter Page
Programiz.com Google+ Page
Difference in Increment ++ Operator as Prefix and Postfix
In any programming (Java, C Programming, PHP etc. ), increment ++ and decrement -- operator are used for
increasing and decreasing the value of operand by 1 respectively.
Suppose, a=5 then,
++a;
//a becomes
a++;
//a becomes
--a;
//a becomes
a--;
//a becomes

6
7
6
5

Simple enough till now but, there is a slight but very important difference that a programmer should remember while
using these operators.

++ and -- operator as prefix and postfix


If you use ++ operator as prefix like: ++var; then, the value of operand is increased by 1 then, only it is returned but,
if you use ++ as postfix like: var++; then, the value of operand is returned first then, only it is increased by 1.
This is demonstrated by an example in these 4 different programming languages.

Word to PDF - UnRegistered


http://www.word-to-pdf.abdio.com/

istered ) http://www.word-to-pdf.abdio.com/
Languages
C Programming
C++ Programming
PHP
Java

C Programming Example
#include <stdio.h>
int main(){
int var=5;
printf("%d\n",var++);
/* 5 is displayed then, only var is increased to 6 */
printf("%d",++var);
/* Initially, var=6 then, it is increased to 7 then, only displayed */
return 0;
}

C++ Programming Example


#include <iostream>
using namespace std;
int main(){
int var=5;
cout<<var++<<"\n";
/* 5 is displayed then, only var is increased to 6 */
cout<<++var<<"\n";
/* Initially, var=6 then, it is increased to 7 then, only displayed */
return 0;
}

PHP Example
<?php
Svar=5;
echo $var++."<br/>";
echo ++$var;
?>

Java Example
class Operator {
public static void main(String[] args){
int var=5;
System.out.println(var++);
System.out.println("\n"+ ++var);
}

(
Word
to
PDF Unreg

(
Word
to
PDF Unreg

Word to PDF - UnRegistered


http://www.word-to-pdf.abdio.com/

istered ) http://www.word-to-pdf.abdio.com/
}

Output of all these program is same as below:


5
7

Add new comment


Your name Comment *
More information about text formats

Comment Text
Web page addresses and e-mail addresses turn into links automatically.
Lines and paragraphs break automatically.
Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
Code snippets in <code>...</code> or <source>...</source> automatically will be pretty printed.
Word verification * Type the characters you see in this picture. (verify using audio) Type the characters you see in the
picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Web Toolbar by Wibiya

Copyright programiz.com, 2011-2013 | All rights reserved

You might also like