Filter by Tags

Pre and post increment operators

Votes

0

0 0

What is the difference between pre and post increment operators. Explain with an example.

Open E-School
Posted on : Wed, Jun 14, 2017
Views : 140
Comments

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Answers (2)


0

Pre-Increament Operator- These operators are uniary operators and hence work on a single variable. The operator is placed before the operand and it is increamented first and then it is used.

Post-Increament Operator- These operator is plcaed after the operand and in this case the operand is used first and then is increamented.

e.g.

int a=10,b=25;

cout<<++a<<endl;

cout<<b++<<endl;

cout<<a<<endl<<b;

 

Output

11

25

11

26

 

Apurva Thakker
Wed, Jun 14, 2017
Loading...
Comments

Edit Answer

Submit

0

"++" is an increment operator. Lets say "i" is a variable. Now if ++ is used before the variable(++i) then its a pre increment operator and if it(++) is used after the variable(i++) then its a post increment operator.

Now the difference betwwen Pre and Post Increment operator is explain below with an example:

Example: Take Three variables

int a=5, b=5, c;

c = a + ++b; // statment 1

c = a + b++ // statment 2

 

Output of above aditions in c will be like : 11 and 10

Now in statment 1 pre increment operator increment the value of b then it add it to value of a so its adds like this: 5 + 6;

but in statment 2 first addition happend and the post increment operator increment the value of b so the addtion happen like this : 5 + 5. then b's value is incremented to 6.

 

 

 

Sahil Gaba
Wed, Jun 14, 2017
Loading...
Comments

Edit Answer

Submit
This answer will permanently deleted. Do you really want to delete this Answer ?
To Add your Answer Please
LogIn | SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp