What is a condition-controlled loop?
True
. Once the condition becomes False
, the loop terminates, and the program execution continues with the next statement after the loop.Python provides two types of condition-controlled loops: the while
loop and the for
loop.
while
loop, the loop body is executed repeatedly as long as a given condition is true.The syntax for a while
loop is as follows:
while condition:
# Code to be executed
The loop body is denotes under the while
statement, and the condition is checked before each iteration. If the condition is true, the loop body is executed, and then the condition is checked again. If the condition becomes false, the loop is exited, and the program continues with the next statement after the loop.
for
loop, the loop body is executed for each item in an iterable object (such as a list, tuple, string, or range).for
loop is as follows:
for item in iterable:
# Code to be executed
The loop variable item
takes on the value of each item in the iterable sequentially, and the loop body is executed for each value. Once all the items have been processed, the loop terminates, and the program continues with the next statement after the loop.
Both types of loops provide a way to repeatedly execute a block of code until a specific condition is met, allowing for efficient and controlled repetition for python programs.