## C / C++ Courses ? 10 (Pre-increment and Post-increment)

25 Aralık 2010 1.897 views 2 Yorum

### Pre-increment and Post-increment

Pre-increment changes the value of the variable, and then makes the calculation or processes which should be done. (It?s written like this: ?++x ?)

Post-increment makes the processes and then changes the value of the variable. (It?s written like this: ?x++ ?)

Decrement versions of these are the same. (?–x? or? x–?)

int x=3;

printf(?%d?, ++x); //it will change the value of x to 4 and then print 4

printf(?%d?, x); // it will print 4 again

int y=3;

printf(?%d?, y++); // first it will print 3 and then change the value of y to 4.

printf(?%d?, y); // it will print 4

 Ex:

Find the output of below program.

int a = 5;

printf(?%d?, a++); //5

printf(?%d?, a–); //6

printf(?%d?, a); //5

a++;

printf(?%d?, a); //6

++a;

printf(?%d?, a); //7

 Ex:

Find the output of below program.

int y=7, b=2;

int result = b+++–y+3; // Don?t think that I wrote incorrectly. It is completely true =)

printf(?%d-%d-%d?, b, y, result); // 3-6-11

Yeees, it is getting hard =P

 Ex:

Find the output of below program.

int x, a=2, b, c=5;

x= ++a*5;

printf(?x= %d a= %d \n?, x, a); //x=15 a=3

b= a*c–;

printf(?b= %d c= %d \n?, x, a); //b=15 c=4

This is the one that I really like. Here it comes =)

 Ex:

Find the output of below.

int x, y, z;

x = 1;

y = 5;

z = 8;

while (z != x){

if(x%2 == 0)

printf(?z= %d\n?, –z);

printf(?y= %d \n?, y++);

x++;

}

Output will be like that:

y=5

z=7

y=6

y=7

z=6

y=8

y=9

 Ex:

Find the output of below.

int i,j;

i=1;

j=5;

while(i <=10){

if(i%2 == 1)

printf(?%d?, j++);

else

printf(?%d \n?, ++j);

i++;

}

Output will be like that:

5
7
7
9
9
11
11
13
13
15

Take care of yourself..

### 2 Yorum »

• davut selami dedi ki:

Selamlar. Post-increment (“++x” olarak gösterilmiş, “x++” olarak değiştirebilirseniz daha iyi olur. İyi günler.

• Ziynet (admin) dedi ki:

Düzelttim ve teşekkür ettim:) Sana da iyi günler

Be nice. İçeriği çirkin olmadığı sürece her yoruma, öneriye ve soruya "evet" :)

Etiketler: , , , ,