C# 0 The Complete Reference


y  will be set to 11. This is because  x



Download 4,07 Mb.
Pdf ko'rish
bet124/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   120   121   122   123   124   125   126   127   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

y

 will be set to 11. This is because 



x

 is first incremented and then its value is 

returned. However, if the code is written as

x = 10;


y = x++;

then


 y

 will be set to 10. In this case, the value of 



x

 is first obtained



x

 is incremented, and 

then the original value of 

x

 is returned. In both cases, 



x

 is still set to 11. The difference is 

what is returned by the operation.

There are significant advantages in being able to control when the increment or decrement 

operation takes place. Consider the following program, which generates a series of numbers:

// Demonstrate the difference between prefix and

// postfix forms of ++.

using System;

class PrePostDemo {

  static void Main() {

    int x, y;

    int i;

    x = 1;

    Console.WriteLine("Series generated using y = x + x++;");

    for(i = 0; i < 10; i++) {

      y = x + x++; // postfix ++

www.freepdf-books.com



66

 

P a r t   I :  



T h e   C #   L a n g u a g e

      Console.WriteLine(y + " ");

    }

    Console.WriteLine();



    x = 1;

    Console.WriteLine("Series generated using y = x + ++x;");

    for(i = 0; i < 10; i++) {

      y = x + ++x; // prefix ++

      Console.WriteLine(y + " ");

    }


    Console.WriteLine();

  }


}

The output is shown here:

Series generated using y = x + x++;

2

4



6

8

10



12

14

16



18

20

Series generated using y = x + ++x;



3

5

7



9

11

13



15

17

19



21

As the output confirms, the statement

y = x + x++;

adds the original value of 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   120   121   122   123   124   125   126   127   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish