A while loop is used to repeatedly execute a block of code as long as a certain condition is true. It allows us to create a loop that keeps running until a specific condition is met.
Here's the basic structure of a while loop:
while condition: # code to be executed
condition is an expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. After each iteration, the condition is checked again, and the loop continues until the condition becomes false.
Let's see an example to understand it better:
count = 0 while count < 5: print("Count:", count) count += 1
In this code, we initialize a variable
0. The while loop runs as long as
count is less than
5. Inside the loop, we print the current value of
count and then increment it by
1 using the
+= operator. This process continues until
5, at which point the condition becomes false and the loop stops.
The output of this code will be:
As you can see, the loop executed five times because the condition
count < 5 was true for the first five values of
It's important to be careful when using while loops to avoid creating infinite loops. An infinite loop is a loop that never stops because the condition always remains true. For example:
# This is an infinite loop! while True: print("This is an infinite loop!")
In this code, the condition
True is always true, so the loop will continue indefinitely. To stop an infinite loop, you can press
Ctrl + C in the terminal or stop the execution of the program.
While loops are useful when you want to repeat a block of code until a specific condition is met. They give you control over the flow of your program and allow us to create more dynamic and interactive applications.
Wakati kitanzi ni kutumika mara kwa mara kutekeleza block ya kanuni kwa muda mrefu kama hali fulani ni kweli. Inatuwezesha kuunda kitanzi ambacho huendelea kukimbia mpaka hali maalum itakapokutana. "Hii ni muundo wa msingi wa mzunguko wa wakati: ""wakati hali: # code kutekelezwa hali ni usemi kwamba ni tathmini kabla ya kila iteration ya mzunguko.""" Kama hali ni kweli, code ndani ya kitanzi ni kutekelezwa. Baada ya kila iteration, hali ni kuchunguzwa tena, na kitanzi inaendelea mpaka hali inakuwa uongo. "Kama mfano, ""Count"" ni sawa na ""Count"" na ""Count"" ni sawa na ""Count"" na ""Count"" na ""Count"" ni sawa na ""Count"" na ""Count"" na ""Count"" ni sawa na ""Count"" na ""Count"" na ""Count"" ni sawa na ""Count"" na ""Count"" ni sawa na ""Count"" na ""Count"" ni sawa na ""Count"" na ""Count"" ni sawa na ""Count""." Wakati loop anaendesha muda mrefu kama hesabu ni chini ya 5. Katika loop, tunachapisha thamani ya sasa ya hesabu na kisha kuongeza kwa 1 kwa kutumia operator + =. Utaratibu huu unaendelea hadi 5, wakati ambapo hali inakuwa ya uwongo na mzunguko unasimama. "Kama unavyoona, mzunguko huo ulifanywa mara tano kwa sababu ""hesabu ya hali"" ilikuwa kweli kwa maadili matano ya kwanza ya ""Ni muhimu kuwa makini wakati wa kutumia wakati loops ili kuepuka kuunda loops zisizo na mwisho.""" Mzunguko usio na mwisho ni mzunguko ambao hauwezi kamwe kuacha kwa sababu hali daima inabaki kweli. Kwa mfano: # Hii ni mzunguko usio na mwisho! wakati: print("Hii ni mzunguko usio na mwisho!") Katika kanuni hii, hali ya kweli ni kweli daima, hivyo kitanzi itaendelea kwa muda usiojulikana. Ili kuacha kitanzi cha kutokuwa na mwisho, unaweza kubonyeza Ctrl + C katika terminal au kuacha utekelezaji wa programu. Wakati loops ni muhimu wakati unataka kurudia block ya kanuni mpaka hali maalum ni kukidhi. Wao kukupa udhibiti juu ya mtiririko wa programu yako na kuruhusu sisi kujenga maombi zaidi nguvu na maingiliano.