C# 0 The Complete Reference


PART I C h a p t e r   5



Download 4,07 Mb.
Pdf ko'rish
bet156/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   152   153   154   155   156   157   158   159   ...   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  

101


PART IPART I

      while(e > 0) {

        result *= 2;

        e--;

      }

      Console.WriteLine("2 to the " + i + " power is " + result);

    }

  }


}

The output from the program is shown here:

2 to the 0 power is 1

2 to the 1 power is 2

2 to the 2 power is 4

2 to the 3 power is 8

2 to the 4 power is 16

2 to the 5 power is 32

2 to the 6 power is 64

2 to the 7 power is 128

2 to the 8 power is 256

2 to the 9 power is 512

Notice that the 

while

 loop executes only when 



e

 is greater than 0. Thus, when 



e

 is 0, as it is 

in the first iteration of the 

for

 loop, the 



while

 loop is skipped.



The do-while Loop

The third C# loop is the 



do

-

while

. Unlike the 

for

 and the 



while

 loops, in which the condition 

is tested at the top of the loop, the 

do-while

 loop checks its condition at the bottom of the 

loop. This means that a 

do-while

 loop will always execute at least once. The general form of 

the

do-while

 loop is


do {

    


statements

;

} while(



condition

);

Although the braces are not necessary when only one statement is present, they are often 



used to improve readability of the 

do-while

 construct, thus preventing confusion with the 



while

. The 


do-while

 loop executes as long as the conditional expression is true.

The following program uses a 

do

-

while

 loop to display the digits of an integer in 

reverse order:

// Display the digits of an integer in reverse order.

using System;

class DoWhileDemo {

  static void Main() {

    int num;

    int nextdigit;

    num = 198;

www.freepdf-books.com




102

 

P a r t   I :  



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

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

    Console.Write("Number in reverse order: ");

    do {


      nextdigit = num % 10;

      Console.Write(nextdigit);

      num = num / 10;

    } while(num > 0);

    Console.WriteLine();

  }


}

The output is shown here:

Number: 198

Number in reverse order: 891

Here is how the loop works: With each iteration, the leftmost digit is obtained by computing 

the remainder of an integer division by 10. This digit is then displayed. Next, the value in 



num

 is divided by 10. Since this is an integer division, this results in the leftmost digit being 

removed. This process repeats until 

num

 is 0.


The foreach Loop

The


foreach

 loop cycles through the elements of a 



collection.

 A collection is a group of objects. 

C# defines several types of collections, of which one is an array. The 

foreach

 loop is examined 

in Chapter 7, when arrays are discussed.

Using break to Exit a Loop

It is possible to force an immediate exit from a loop, bypassing any code remaining in the 

body of the loop and the loop’s conditional test, by using the 

break

 statement. When a 



break

 statement is encountered inside a loop, the loop is terminated, and program control 

resumes at the next statement following the loop. Here is a simple example:

// Using break to exit a loop.

using System;

class BreakDemo {

  static void Main() {

    // Use break to exit this loop.

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

      if(i > 0) break; // terminate loop when i is positive

      Console.Write(i + " ");

    }


    Console.WriteLine("Done");

  }


}

www.freepdf-books.com




Download 4,07 Mb.

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