LESSON 11. PYTHON -- // While Loop In Python // With Illustrative Examples //

Pubblicato il: 07 marzo 2024
sul canale di: Mavro_yayo
121
5

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.


In questa pagina del sito puoi guardare il video online LESSON 11. PYTHON -- // While Loop In Python // With Illustrative Examples // della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Mavro_yayo 07 marzo 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 121 volte e gli è piaciuto 5 spettatori. Buona visione!