C# 0 The Complete Reference



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

i

 and 


j

 are initialized. Each time the loop repeats, 



i

 is incremented 

and

j

 is decremented. Multiple loop control variables are often convenient and can simplify 

certain algorithms. You can have any number of initialization and iteration statements, but 

in practice, more than two make the 



for

 loop unwieldy.

Here is a practical use of multiple loop control variables in a 

for

 statement. This 

program uses two loop control variables within a single 

for

 loop to find the largest and 

smallest factor of a number, in this case 100. Pay special attention to the termination 

condition. It relies on both loop control variables.

// Use commas in a for statement to find the largest and

// smallest factor of a number.

using System;

class Comma {

  static void Main() {

    int i, j;

    int smallest, largest;

    int num;

    num = 100;

    smallest = largest = 1;

    for(i=2, j=num/2; (i <= num/2) & (j >= 2); i++, j--) {

      if((smallest == 1) & ((num % i) == 0))

        smallest = i;

      if((largest == 1) & ((num % j) == 0))

        largest = j;

    }


    Console.WriteLine("Largest factor: " + largest);

    Console.WriteLine("Smallest factor: " + smallest);

  }

}

www.freepdf-books.com




96

 

P a r t   I :  



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

Here is the output from the program:

Largest factor: 50

Smallest factor: 2

Through the use of two loop control variables, a single 

for

 loop can find both the smallest 

and the largest factor of a number. The control variable 

i

 is used to search for the smallest 

factor. It is initially set to 2 and incremented until its value exceeds one half of 

num

. The 


control variable 

j

 is used to search for the largest factor. Its value is initially set to one half 

the

num

 and decremented until it is less than 2. The loop runs until both 




Download 4,07 Mb.

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