Microsoft Visual C++/cli step by Step



Download 2,23 Mb.
Pdf ko'rish
bet70/102
Sana31.12.2021
Hajmi2,23 Mb.
#228061
1   ...   66   67   68   69   70   71   72   73   ...   102
Bog'liq
Visual studio C step by step

Using do-while loops

The third loop construct you’ll look at here is the do-while loop (remember, there’s still the for-each 

loop, which you will meet later). The do-while loop is fundamentally different from the while and for 

loops because the test comes at the end of the loop body, which means that the loop body is always 

executed at least once.



72  

Microsoft Visual C++/CLI Step by Step

The following example shows how to write a simple do-while loop in C++/CLI. This example gener-

ates random numbers between 1 and 6, inclusive, to simulate a die. It then counts how many throws 

are needed to get a 6.

Random ^r = gcnew Random(); 

int randomNumber; 

int throws = 0; 

do 


    randomNumber = r->Next(1, 7); 

    Console::WriteLine(randomNumber); 

    throws++; 

while (randomNumber != 6); 



 

Console::Write("You took "); 

Console::Write(throws); 

Console::WriteLine(" tries to get a 6");

The loop starts with the do keyword, followed by the loop body, followed by the while keyword 

and the test condition. A semicolon is required after the closing parenthesis of the test condition.

The preceding example displays the output shown in the following screen shot:

In this exercise, you will modify your Calendar Assistant application so that it performs input vali-

dation, which is a typical use of the do-while loop.

1. 

Continue working with the project from the previous exercise.



2. 

Modify the GetMonth function as follows, which forces the user to type a valid month:

int GetMonth() 

    int month = 0; 



    do 

    { 


        Console::Write("Month [1 to 12]? "); 

        String ^input = Console::ReadLine(); 

        month = Convert::ToInt32(input); 

    } 




Download 2,23 Mb.

Do'stlaringiz bilan baham:
1   ...   66   67   68   69   70   71   72   73   ...   102




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