C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet81/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   77   78   79   80   81   82   83   84   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Using Code Blocks

Another key element of C# is the 



code block.

 A code block is a grouping of statements. This is 

done by enclosing the statements between opening and closing curly braces. Once a block of 

code has been created, it becomes a logical unit that can be used any place a single statement 

can. For example, a block can be a target for 

if

 and 


for

 statements. Consider this 



if

 statement:

if(w < h) {

  v = w * h;

www.freepdf-books.com



30

 

P a r t   I :  



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

  w = 0;


}

Here, if 



w

 is less than 



h

, then both statements inside the block will be executed. Thus, the two 

statements inside the block form a logical unit, and one statement cannot execute without the 

other also executing. The key point here is that whenever you need to logically link two or 

more statements, you do so by creating a block. Code blocks allow many algorithms to be 

implemented with greater clarity and efficiency.

Here is a program that uses a code block to prevent a division by zero: 

// Demonstrate a block of code.

using System;

class BlockDemo {

  static void Main() {

    int i, j, d;

    i = 5;

    j = 10;

    // The target of this if is a block.

    if(i != 0) {

      Console.WriteLine("i does not equal zero");

      d = j / i;

      Console.WriteLine("j / i is " + d);

    }


  }

}

The output generated by this program is shown here:



i does not equal zero

j / i is 2

In this case, the target of the 

if

 statement is a block of code and not just a single statement. 

If the condition controlling the 

if

 is true (as it is in this case), the three statements inside the 

block will be executed. Try setting 

i

 to zero and observe the result.

Here is another example. It uses a code block to compute the sum and the product of the 

numbers from 1 to 10.

// Compute the sum and product of the numbers from 1 to 10.

using System;

class ProdSum {

  static void Main() {

    int prod;

    int sum;

    int i;

    sum = 0;

    prod = 1;

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   77   78   79   80   81   82   83   84   ...   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