Example 1: Simple while loop
In this example, a while loop is used to repeatedly execute a block of code as long as a specified condition is true. The loop continues as long as the count variable is less than 5. In each iteration of the loop, the value of count is printed and then incremented by 1. Once count reaches 5, the loop ends and "Loop ended" is printed.
Example 2: Using while loop for user input
Here, a while loop is used to continuously prompt the user for input until the user enters 'quit'. Each time the user enters a word, it is printed back to them. The loop checks if the user's input is equal to 'quit' and continues until that condition is met. Once the user enters 'quit', the loop ends and "Loop ended" is printed.
Example 3: Using while loop with break and continue
In this example, the first while loop runs indefinitely (while True:) until it finds the first even number greater than 10. Once it finds the number, it uses break to exit the loop.
The second while loop prints numbers from 1 to 10, but it skips any numbers that are multiples of 3 using the continue statement. This means that when a number is a multiple of 3, the loop skips that iteration and moves to the next number.
These examples illustrate how while loops can be used for various purposes such as iterating through a sequence of numbers, handling user input, and controlling the flow of the loop using break and continue statements.
En esta página del sitio puede ver el video en línea LESSON 11. PYTHON -- // While Loop In Python // With Illustrative Examples // de Duración hora minuto segunda en buena calidad , que subió el usuario Mavro_yayo 07 marzo 2024, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 121 veces y le gustó 5 a los espectadores. Disfruta viendo!