Infinite Loops
Here is the question: what if the condition always has a constant value of True or False? If it is False, we simply skip the instructions inside the loop's body. But for True, we get the infinite loop: you'll never leave it, and your code can execute without stopping (or until the process will be interrupted). Beginners (and even experienced programmers) sometimes accidentally code the infinite loops due to different reasons.
For example, the following code snippet is an infinite loop because the counter i
is constant, probably, the programmer forgot about updating counter i += 1
.
123i = 1 while i < 100: print(i*i)
This loop is infinite because the stop condition is constantly True.
12while 2 + 2 == 4: print('Math works!')
You should avoid the infinite loops and double check if the condition ever becomes False.
Swipe to start coding
Be careful, this loop is infinite. Change the condition so that the loop is finite and list_
contains following values:
1 2 4 8 16 32 64
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Résumer ce chapitre
Expliquer le code dans file
Expliquer pourquoi file ne résout pas la tâche
Awesome!
Completion rate improved to 6.25
Infinite Loops
Glissez pour afficher le menu
Here is the question: what if the condition always has a constant value of True or False? If it is False, we simply skip the instructions inside the loop's body. But for True, we get the infinite loop: you'll never leave it, and your code can execute without stopping (or until the process will be interrupted). Beginners (and even experienced programmers) sometimes accidentally code the infinite loops due to different reasons.
For example, the following code snippet is an infinite loop because the counter i
is constant, probably, the programmer forgot about updating counter i += 1
.
123i = 1 while i < 100: print(i*i)
This loop is infinite because the stop condition is constantly True.
12while 2 + 2 == 4: print('Math works!')
You should avoid the infinite loops and double check if the condition ever becomes False.
Swipe to start coding
Be careful, this loop is infinite. Change the condition so that the loop is finite and list_
contains following values:
1 2 4 8 16 32 64
Solution
Merci pour vos commentaires !
Awesome!
Completion rate improved to 6.25single