C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet280/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   276   277   278   279   280   281   282   283   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   8 :  

A   C l o s e r   L o o k   a t   M e t h o d s   a n d   C l a s s e s  

205


PART IPART I

Original string: this is a test

Reversed string: tset a si siht

Each time 



DisplayRev( )

 is called, it first checks to see if 



str

 has a length greater than zero. If 

it does, it recursively calls 

DisplayRev( )

 with a new string that consists of 



str

 minus its first 

character. This process repeats until a zero-length string is passed. This causes the recursive 

calls to start unraveling. As they do, the first character of 



str

 in each call is displayed. This 

results in the string being displayed in reverse order.

Recursive versions of many routines may execute a bit more slowly than the iterative 

equivalent because of the added overhead of the additional method calls. Too many recursive 

calls to a method could cause a stack overrun. Because storage for parameters and local 

variables is on the system stack, and each new call creates a new copy of these variables, it 

is possible that the stack could be exhausted. If this occurs, the CLR will throw an exception. 

However, you probably will not have to worry about this unless a recursive routine runs wild.

The main advantage to recursion is that some types of algorithms can be more clearly 

and simply implemented recursively than iteratively. For example, the quicksort sorting 

algorithm is quite difficult to implement in an iterative way. Also, some problems, especially 

AI-related ones, seem to lend themselves to recursive solutions.

When writing recursive methods, you must have a conditional statement, such as an 



if

,

somewhere to force the method to return without the recursive call being executed. If you 



don’t do this, once you call the method, it will never return. This type of error is very 

common when working with recursion. Use 



WriteLine( )

 statements liberally so that you 

can watch what is going on and abort execution if you see that you have made a mistake.


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   276   277   278   279   280   281   282   283   ...   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