C# 0 The Complete Reference


i  is not accessible outside the  for



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

i

 is not accessible outside the 



for

 loop. If you need to use the loop control 

variable elsewhere in your program, you will not be able to declare it inside the 

for

 loop.


Before moving on, you might want to experiment with your own variations on the 

for

loop. As you will find, it is a fascinating loop.



The while Loop

Another of C#’s loops is the 



while

. The general form of the



 while

 loop is


while(

condition

)

statement

;

where 


statement

 can be a single statement or a block of statements, and 



condition

 defines the 

condition that controls the loop and may be any valid Boolean expression. The statement is 

performed while the condition is true. When the condition becomes false, program control 

passes to the line immediately following the loop.

www.freepdf-books.com




100

 

P a r t   I :  



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

Here is a simple example in which a 



while

 is used to compute the order of magnitude of 

an integer:

// Compute the order of magnitude of an integer

using System;

class WhileDemo {

  static void Main() {

    int num;

    int mag;

    num = 435679;

    mag = 0;

    Console.WriteLine("Number: " + num);

    while(num > 0) {

      mag++;

      num = num / 10;

    };


    Console.WriteLine("Magnitude: " + mag);

  }


}

The output is shown here:

Number: 435679

Magnitude: 6

The

while

 loop works like this: The value of 



num

 is tested. If 



num

 is greater than 0, the 



mag

counter is incremented, and 



num

 is divided by 10. As long as the value in 



num

 is greater 

than 0, the loop repeats. When 

num

 is 0, the loop terminates and 



mag

 contains the order of 

magnitude of the original value.

As with the 



for

 loop, the 



while

 checks the conditional expression at the top of the loop, 

which means that the loop code may not execute at all. This eliminates the need for performing 

a separate test before the loop. The following program illustrates this characteristic of the 



while

loop. It computes the integer powers of 2 from 0 to 9.

// Compute integer powers of 2.

using System;

class Power {

  static void Main() {

    int e;

    int result;

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

      result = 1;

      e = i;

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   151   152   153   154   155   156   157   158   ...   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