DEV Community

Sunnat Qayumov
Sunnat Qayumov

Posted on

22. While Loop

a) while loop va do while loop o'rtasidagi farqni tushuntiring.

1. while sikli
Siklning bajarilish sharti: while sikli birinchi navbatda shartni tekshiradi va shart to'g'ri(true) bo'lsa, sikl tanasi bajariladi.

Sintaksis:

while (shart)
{
    // Sikl tanasi
}
Enter fullscreen mode Exit fullscreen mode

2. do-while sikli
Siklning bajarilish sharti: do-while siklida dastlab sikl tanasi bir marta bajariladi, keyin esa shart tekshiriladi. Shart to'g'ri (true) bo'lsa, sikl takroran bajariladi.

Sintaksis:

do
{
    // Sikl tanasi
} while (shart);
Enter fullscreen mode Exit fullscreen mode

b) Quyidagi while loop qanday ishlayd va natijasi nima bo'ladi?

int x = 0;
while(x < 3)
{
    Console.WriteLine(x);
    x++;
}
Enter fullscreen mode Exit fullscreen mode

int tipida x nomli o'zgaruvchi yaratib, uni 0 ga tenglaymiz. while() shartiga x kichik bo'lsa 3 dan degan shartni beramiz. shart 3 martta to'g'ri ishlaydi. Javob: 0, 1, 2.

Top comments (0)