C# 0 The Complete Reference


PART I C h a p t e r   7



Download 4,07 Mb.
Pdf ko'rish
bet220/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   216   217   218   219   220   221   222   223   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   7 :  

A r r a y s   a n d   S t r i n g s  

155


PART IPART I

    // Give nums some values.

    for(int i = 0; i < 3; i++)

      for(int j=0; j < 5; j++)

        nums[i,j] = (i+1)*(j+1);

    // Use foreach to display and sum the values.

    foreach(int x in nums) {

      Console.WriteLine("Value is: " + x);

      sum += x;

    }


    Console.WriteLine("Summation: " + sum);

  }


}

The output from this program is shown here:

Value is: 1

Value is: 2

Value is: 3

Value is: 4

Value is: 5

Value is: 2

Value is: 4

Value is: 6

Value is: 8

Value is: 10

Value is: 3

Value is: 6

Value is: 9

Value is: 12

Value is: 15

Summation: 90

Since the 

foreach

 loop can only cycle through an array sequentially, from start to finish

you might think that its use is limited. However, this is not true. A large number of algorithms 

require exactly this mechanism, of which one of the most common is searching. For example, 

the following program uses a 

foreach

 loop to search an array for a value. It stops if the 

value is found.

// Search an array using foreach.

using System;

class Search {

  static void Main() {

    int[] nums = new int[10];

    int val;

    bool found = false;

    // Give nums some values.

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

      nums[i] = i;

    val = 5;

www.freepdf-books.com



156

 

P a r t   I :  



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

    // Use foreach to search nums for key.

    foreach(int x in nums) {

      if(x == val) {

        found = true;

        break;

      }

    }


    if(found)

      Console.WriteLine("Value found!");

  }

}

The output is shown here:



Value found!

The



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   216   217   218   219   220   221   222   223   ...   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