22. While Loop
Sunnat Qayumov
Posted on October 13, 2024
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
}
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);
b) Quyidagi while loop qanday ishlayd va natijasi nima bo'ladi?
int x = 0;
while(x < 3)
{
Console.WriteLine(x);
x++;
}
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.
Posted on October 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024