C# 0 The Complete Reference


PART I C h a p t e r   5



Download 4,07 Mb.
Pdf ko'rish
bet151/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   147   148   149   150   151   152   153   154   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   5 :  

P r o g r a m   C o n t r o l   S t a t e m e n t s  

97

PART IPART I



Missing Pieces

Some interesting 



for

 loop variations are created by leaving pieces of the loop definition 

empty. In C#, it is possible for any or all of the initialization, condition, or iteration portions 

of the 


for

 loop to be empty. For example, consider the following program:

// Parts of the for can be empty.

using System;

class Empty {

  static void Main() {

    int i;

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

      Console.WriteLine("Pass #" + i);

      i++; // increment loop control var

    }

  }


}

Here, the iteration expression of the 



for

 is empty. Instead, the loop control variable 



i

 is 


incremented inside the body of the loop. This means that each time the loop repeats, 

i

 is tested 

to see whether it equals 10, but no further action takes place. Of course, since 

i

 is incremented 

within the body of the loop, the loop runs normally, displaying the following output:

Pass #0


Pass #1

Pass #2


Pass #3

Pass #4


Pass #5

Pass #6


Pass #7

Pass #8


Pass #9

In the next example, the initialization portion is also moved out of the 



for

:

// Move more out of the for loop.



using System;

class Empty2 {

  static void Main() {

    int i;

    i = 0; // move initialization out of loop

    for(; i < 10; ) {

      Console.WriteLine("Pass #" + i);

      i++; // increment loop control var

    }

  }


}

www.freepdf-books.com




98

 

P a r t   I :  



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

In this version, 



i

 is initialized before the loop begins, rather than as part of the 



for

.

Normally, you will want to initialize the loop control variable inside the 



for

. Placing the 

initialization outside of the loop is generally done only when the initial value is derived 

through a complex process that does not lend itself to containment inside the 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   147   148   149   150   151   152   153   154   ...   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