While sikl operatori
Kirish
While
ham for kabi takrorlanish operatori hisoblanadi While
ni for
operatoridan farqi whileda ma’lum shartlar qanoatlantirganda buyruqlar blokining ko’p marta bajarilish imkonini beradi.
Misollar
1-misol
2-misol
3-misol
# 1 dan 10 gacha bo’lgan sonlar yig’indisini hisoblash
n = 1
s = 0
while n <= 10:
s += n
n += 1
print(s)
4-misol
5-misol
# n soni necha xonaligini aniqlash
n = int(input('n = '))
c = 0
while n != 0:
n //= 10
c += 1
print(c)
6-misol
# n sonini raqamlari yig’indisini hisoblash
n = int(input('n = '))
c = 0
s = 0
while n != 0:
n //= 10
c += 1
s += c
print(s)
7-misol
# n sonini m soniga bo’lgandagi
# butun va qoldiq qismini bo’lish
# va qoldiqsiz bo’lish amalisiz chiqarish
n = 10
m = 2
c = 0
while n > m:
n -= m
c += 1
print(n, c)