while loops - Integrated Circuits - - An Online Book - |
||||||||
Integrated Circuits http://www.globalsino.com/ICs/ | ||||||||
================================================================================= | ||||||||
while loop is an infinite loop. The while loop repeats a block, identified by indentation, until the test condition remains true. The while loop is the most commonly used construct for repeating a task over and over again. The loop uses break and continue. code1, code2, code3. With for, you generally get a fixed number of loops, one for each item in a range or one for each item in a list. With a while loop, the loop keeps going as long as (while) some condition is true. while loop cannot be rewitten as a for loop. ============================================ A while loop that continuously prompts the user to input their name. If the input matches the string "Yougui", the loop will break. Otherwise, it will continue prompting for input. Here's a breakdown: (code): Output: ============================================ A while loop that continuously prompts the user to input their name. If the entered name is not equal to "Yougui", it uses the continue statement to skip the rest of the loop's code and goes back to the beginning of the loop to prompt for input again. If the entered name is "Yougui", it breaks out of the loop (break), ending the program. (code): Output:
============================================ Print even numbers: code: Function: code: ============================================ Program function: calculate a power of 13: code: ============================================ Break a code line after a certain time. Code: ============================================ Try a number of times before exception or fail: code:
|
||||||||
================================================================================= | ||||||||
|
||||||||