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

Veröffentlicht am: 07 März 2024
auf dem Kanal: 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.


Auf dieser Seite können Sie das Online-Video LESSON 11. PYTHON -- // While Loop In Python // With Illustrative Examples // mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Mavro_yayo 07 März 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 121 Mal angesehen und es wurde von 5 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!