C# 0 The Complete Reference


for  statement. The Infinite Loop



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

for

 statement.



The Infinite Loop

You can create an 



infinite loop

 (a loop that never terminates) using the 



for

 by leaving the 

conditional expression empty. For example, the following fragment shows the way many 

C# programmers create an infinite loop:

for(;;) // intentionally infinite loop

{

  //...



}

This loop will run forever. Although there are some programming tasks, such as operating 

system command processors, that require an infinite loop, most “infinite loops” are really 

just loops with special termination requirements. (See “Using 



break

 to Exit a Loop,” later in 

this chapter.)

Loops with No Body

In C#, the body associated with a 



for

 loop (or any other loop) can be empty. This is because 

a

empty statement

 is syntactically valid. Bodyless loops are often useful. For example, the 

following program uses a bodyless loop to sum the numbers 1 through 5:

// The body of a loop can be empty.

using System;

class Empty3 {

  static void Main() {

    int i;

    int sum = 0;

    // Sum the numbers through 5.

    for(i = 1; i <= 5; sum += i++) ;

    Console.WriteLine("Sum is " + sum);

  }

}

The output from the program is shown here:



Sum is 15

Notice that the summation process is handled entirely within the 



for

 statement, and no 

body is needed. Pay special attention to the iteration expression:

sum += i++

Don’t be intimidated by statements like this. They are common in professionally written 

C# programs and are easy to understand if you break them down into their parts. In words, 

www.freepdf-books.com



Download 4,07 Mb.

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