C# 0 The Complete Reference



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

Two Control Statements

Inside a method, execution proceeds from one statement to the next, top to bottom. It 

is possible to alter this flow through the use of the various program control statements 

supported by C#. Although we will look closely at control statements later, two are briefly 

introduced here because we will be using them to write sample programs.

www.freepdf-books.com




PART I

C h a p t e r   2 :  

A n   O v e r v i e w   o f   C #  

27

PART IPART I



The if Statement

You can selectively execute part of a program through the use of C#’s conditional statement: 

the

if

. The 


if

 statement works in C# much like the IF statement in any other language. For 

example, it is syntactically identical to the 

if

 statements in C, C++, and Java. Its simplest 

form is shown here:

if(


condition

)

 statement

;

Here, 


condition

 is a Boolean (that is, true or false) expression. If 



condition

 is true, then the 

statement is executed. If 

condition

 is false, then the statement is bypassed. Here is an 

example:

if(10 < 11) Console.WriteLine("10 is less than 11");

In this case, since 10 is less than 11, the conditional expression is true, and 

WriteLine( )

 will 


execute. However, consider the following:

if(10 < 9) Console.WriteLine("this won’t be displayed");

In this case, 10 is not less than 9. Thus, the call to 

WriteLine( )

 will not take place.

C# defines a full complement of relational operators that can be used in a conditional 

expression. They are shown here:

Operator

Meaning


<

Less than



<=

Less than or equal to

>

Greater than



>=

Greater than or equal to

= =

Equal to


!=

Not equal 

Here is a program that illustrates the 

if

 statement:

// Demonstrate the if.

using System;

class IfDemo {

  static void Main() {

    int a, b, c;

    a = 2;

    b = 3;

    if(a < b) Console.WriteLine("a is less than b");

    // This won’t display anything.

    if(a == b) Console.WriteLine("you won’t see this");

    Console.WriteLine();

www.freepdf-books.com




28

 

P a r t   I :  



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

    c = a - b; // c contains -1

    Console.WriteLine("c contains -1");

    if(c >= 0) Console.WriteLine("c is non-negative");

    if(c < 0) Console.WriteLine("c is negative");

    Console.WriteLine();

    c = b - a; // c now contains 1

    Console.WriteLine("c contains 1");

    if(c >= 0) Console.WriteLine("c is non-negative");

    if(c < 0) Console.WriteLine("c is negative");

  }

}

The output generated by this program is shown here:



a is less than b

c contains -1

c is negative

c contains 1

c is non-negative

Notice one other thing in this program. The line

int a, b, c;

declares three variables, 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   74   75   76   77   78   79   80   81   ...   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