LOOPS:

In computer programming, a loop is a sequence of statements that is continually repeated until a certain condition is reached. In simple they are used to repeat a specific block of code. Python supports two kinds of loops.

  • while loop
  • for loop

The while loop

The while loop is used to iterate over a block of code (the body of the loop) as long as the expression is evaluated to True. The body of the loop is identified by indentation. The first unindented line marks the end of the loop. The syntax of the while loop is as shown below:

        while expression:
            statement1
            statement2
            # ... more statements
            

The program evaluates the expression first. If the expression is evaluated to True the body of the loop will be executed else it will be skipped. Once the execution of the body is finished the expression will be evaluated again and the same process is repeated till the expression is evaluated to False.

The following example explains the usage of a while loop:

        # Program to print squares
        # of first ten numbers

        number = 1
        while number <= 10:
            square = number * number
            print("Square of {} is {}".format(number, square))
            number += 1 # increments the number by 1
        print("The loop is finished.")

    In the above example the expression number <= 10 is evaluated first. As it evaluates to True (since the number is initialised as 1) the body of the loop is executed and the square of the number is printed. Now the number is incremented by 1 and the expression is evaluated again and the same process is repeated till the expression is evaluated to False. Then the program will come out of the loop and the statement The loop is finished is printed.

When the above program is executed, the output will be as shown below:

        Square of 1 is 1
        Square of 2 is 4
        Square of 3 is 9
        Square of 4 is 16
        Square of 5 is 25
        Square of 6 is 36
        Square of 7 is 49
        Square of 8 is 64
        Square of 9 is 81
        Square of 10 is 100
        The loop is finished.

In the above example increasing the value of variable number is important. Otherwise the expression is never evaluated to False and results in an infinite loop.

The while loop with else statement:

A while loop can have an optional else block. The else block is executed when the expression in the while loop is evaluated to False. If the while loop is terminated by a break statement, the else block is ignored. Therefore, a while loop's else block is executed only when no break occurs and the condition is evaluated to False. The following example illustrates this.

        for item in iterable_sequence:
            statement1
            statement2
            # ... more statements
            

something to say

The for loop

The for loop in python is used to iterate over an iterable sequence. The body of the loop is identified using indentation. The first unindented statement indicates the end of the body of the loop. The body of the loop will be executed for each item in the sequence till the end of the sequence is reached. The variable item holds the corresponding value from the sequence in each iteration. The syntax of the for loop is as shown below:

        for each_item in iterable_sequence:
            statement1
            statement2
            # ... more statements
            

In the above example iterable_sequence can denote any iterable sequence such as Lists, Tuples, Strings, Dictionaries etc.. Each item from the sequence is stored in the variable each_item and the body of the loop is executed and the loop proceeds to the next element of sequence till all the elements of the sequence are finished.

The break and continue in loops:

Sometimes we may want to skip a specific iteration of a loop or we may want to abort the execution of the whole loop when a particular condition is met. To fulfill this type of scenarios we have two statements called "break" and "continue" in python. These statements can alter the flow of normal loop execution based on their usage.

The break statement terminates the loop in which the statement is present. The program flow will be moved to immediate statement after the body of the loop.

The following example explains the usage of break statement:

			while expression:
				statement1
				statement2
				if condition:
					break
				statement3

			for item in iterable_sequence:
				statement1
				statement2
				if condition:
					break
				statement3

The continue statement when present skips the execution of the rest of code followed by it and moves to the next iteration.

The following example explains the usage of continue statement:

			while expression:
				statement1
				statement2
				if condition:
					continue
				statement3

			for item in iterable_sequence:
				statement1
				statement2
				if condition:
					continue
				statement3

When a break or continue statements are present inside a nested loop (loop inside another loop), they will be applicable to the innermost loop.

Conditions Functions