LOOP CONTROL STATEMENT
LOOP CONTROL STATEMENTS
Loop control statements in Python are used to alter the execution flow within loops. They allow you to control how the loop behaves and when it should terminate or skip iterations. The common loop control statements in Python include:
break
statement: It is used to exit the loop prematurely. When encountered, the program jumps out of the loop and continues with the next statement after the loop.
SYNTAX:
2.continue
statement: It is used to skip the remaining code in the current iteration of the loop and move to the next iterationfor item in sequencefor item in sequence:
SYNTAX:
if some_condition:
continue
# rest of the loop code
pass
statement: It is a placeholder statement that does nothing. It is used when a statement is syntactically required but you don't want to execute any code.
if condition:
pass # no code to execute
else:
# code block to be executed
else
statement with loops: It is used to define a block of code that executes only when the loop condition becomes false. Theelse
block is skipped if the loop is terminated by abreak
statement
SYNTAX:
while condition:
# loop code
else:
# code to execute when the loop condition becomes false
Comments
Post a Comment