Loops: Control flow statements that execute code multiple times.
While Loops: Executes code as long as a condition is true.
Syntax: while condition: body
Consists of while
keyword, a boolean expression, and loop body.
If true, body executes; if false, control passes to the next statement.
Iteration: One execution cycle through the loop.
Re-evaluates condition until false to exit the loop.
Initialization: current_power
to 2, user_character
to 'y'.
Uses while user_character == 'y':
to print current_power
, double it, and read new input.
Example flow: 2, 4, 8, exits on 'n'.
Sentinel Values: Indicate end of data, e.g., 'Q'.
Structure: while user_value != 'Q':
continues until 'Q'.
Extract digits via while loop: while number > 0:
prints last digit, reduces number.
Example for 902: prints 2, 0, 9.
Occurs when condition is always true, freezing the program.
Algorithm with two integers A and B: while A != B:
subtracts the smaller from the larger until equal.
Example: A = 20, B = 15 results in GCD = 5.
Uses while user_input != 'goodbye':
for random responses based on input.
Ends input with zero; sums and counts input until sentinel.
Average calculated as {sum} / {count}
.