C# 0 The Complete Reference


PART I C h a p t e r   5



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

103


PART IPART I

This program generates the following output:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 Done

As you can see, although the 



for

 loop is designed to run from –10 to 10, the 



break

 statement 

causes it to terminate early, when 

i

 becomes positive.

The

break

 statement can be used with any of C#’s loops. For example, here is the 

previous program recoded to use a 

do

-

while

 loop:

// Using break to exit a do-while loop.



using System;

class BreakDemo2 {

  static void Main() {

    int i;

    i = -10;

    do {


      if(i > 0) break;

      Console.Write(i + " ");

      i++;

    } while(i <= 10);

    Console.WriteLine("Done");

  }


}

Here is a more practical example of 



break

. This program finds the smallest factor of a 

number.

// Find the smallest factor of a value.

using System;

class FindSmallestFactor {

  static void Main() {

    int factor = 1;

    int num = 1000;

    for(int i=2; i <= num/i; i++) {

      if((num%i) == 0) {

        factor = i;

        break; // stop loop when factor is found

      }


    }

    Console.WriteLine("Smallest factor is " + factor);

  }

}

The output is shown here:



Smallest factor is 2

www.freepdf-books.com




104

 

P a r t   I :  



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

The



Download 4,07 Mb.

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