What is a loop? A loop is a repetition of set of instructions. It repeats until the condition given by the program holds false.
One type of loop is For loop.
FOR LOOP:
Syntax for FOR LOOP-
for i in range(x)
statement
statement
statement
Here, in for loop, the loop executes till “i” is in range provided by x.
Example:
So, FOR loops could be used for executing statements for certain number of specified times such as x times.
Another type of loop is the while loop.
WHILE LOOP:
Syntax for while loop-
while(condition)
statement
statement
statement
A simple while loop syntax is shown above.
While loop is used in the similar way as the for loop, for executing the statements in the body of the loop for the time defined by putting the condition of it accordingly.
Example:
Loop Control Statements in python:
Loop Control Statements in python works for the controlling the loop iteration by providing it inside the loop body.
Python has three loop control statements:
Break statement: Break Statement is one of loop control statement which terminates the loop whenever it is executed in the loop body and brings the execution out of the loop.
Example:
Continue Statement: Continue Statement is another loop control statement which is used in the loop body, it re-iterates the loop without executing the succeeding statements.
Example:
Pass Statement: Pass Statement is used as a NOP (no operation) in python. Pass Statement is just executed as a no statement that is, it does not affect the program.