Python Looping Statements

Looping statements

Looping statements are used to execute the same block of code multiple times in python based on the test condition. Let’s have look some of the looping statement in python.

while loop:

The while loop is used to execute a piece of code as long as the test condition is true. While loop is preferred whenever the number of iterations is not known.

Syntax :

while loop syntax in python
CODE/PROGRAM/EXAMPLE
//Program Example :
	num=5
	count=1
	while count <= num:
		print(“the current number is:”,count)
		count+=1
		
Output:
the current number is: 1
the current number is: 2
the current number is: 3
the current number is: 4
the current number is: 5

In python, for loop allows the loop to run over a specific sequence of values. In other words, for every value in the sequence, the loop runs once. Thus we can avoid infinite loops by using a for loop.

Syntax :

for loop syntax in python
CODE/PROGRAM/EXAMPLE
//Program Example :
	for number in 1,2,3,4,5:
	print(“The current number is:”,number)
	
Output:
The current number is  1
The current number is  2
The current number is  3
The current number is  4
The current number is  5

Another variation! :

In Python, there is an easy way to achieve this by using range(x,y,step). It creates a sequence from x to y-1 with a difference of step between each value.

CODE/PROGRAM/EXAMPLE
//Program Example :
	start=1
	end=10
	step=2

	for number in range (start, end, step):
	print(“The current number is:”, number)
	
Output:
The current number is  1
The current number is  3
The current number is  5
The current number is  7
The current number is  9

Tryout :

Execute the below code in python playground and observe the output

CODE/PROGRAM/EXAMPLE
for number in range(1,5):
	print (“The current number is:”,number)

	print("---------------------------")

	for number in range(1,7,2):
	print (“The current number is:”,number)

	print("---------------------------")

	for number in range(5,0,-1):
	print (“The current number is:”,number)

Nested Loops :

A loop within a loop is known as nested loop.

Assume that there are 5 passengers and each of them have 2 baggage. The below code will make sure that all baggage of each passenger have undergone the security check.

CODE/PROGRAM/EXAMPLE
//Program Example :

number_of_passengers=5
number_of_baggage=2
security_check=True
for passenger_count in range(1, number_of_passengers+1):
	for baggage_count in range(1,number_of_baggage+1):
		if(security_check==True):
			print(“Security check of passenger:”, passenger_count, “-- baggage:”, baggage_count,“baggage cleared”)
		else:
			print(“Security check of passenger:”, passenger_count, “-- baggage:”, baggage_count,“baggage not cleared”)
				
Output:
Security check of passenger: 1 -- baggage: 1 baggage cleared
Security check of passenger: 1 -- baggage: 2 baggage cleared
Security check of passenger: 2 -- baggage: 1 baggage cleared
Security check of passenger: 2 -- baggage: 2 baggage cleared
Security check of passenger: 3 -- baggage: 1 baggage cleared
Security check of passenger: 3 -- baggage: 2 baggage cleared
Security check of passenger: 4 -- baggage: 1 baggage cleared
Security check of passenger: 4 -- baggage: 2 baggage cleared
Security check of passenger: 5 -- baggage: 1 baggage cleared
Security check of passenger: 5 -- baggage: 2 baggage cleared

The same code in the inner loop can also be written using while loop instead of for loop as shown below:

CODE/PROGRAM/EXAMPLE
number_of_passengers=5
	number_of_baggage=2
	security_check=True
	for passenger_count in range(1, number_of_passengers+1):
		baggage_count =1
		while (baggage_count<=number_of_baggage):
			if(security_check==True):
				print(“Security check of passenger:”, passenger_count, “-- baggage:”, baggage_count,“baggage cleared”)
			else:
				print(“Security check of passenger:”, passenger_count, “-- baggage:”, baggage_count,“baggage not cleared”)
			baggage_count+=1

Similarly, the outer loop can also be written using while loop.

Loop control statements :

The flow inside looping statements are controlled using the looping control statements like pass, break and continue.

When we want to stop a loop or break away from it we can use break statement.

When we want to skip the remaining portion of loop statements and continue with the next iteration, we can use continue statement.

Example : Go through the below code, Assume A – Adult passenger, C- Child, FC – Flight Captain, FA – Flight Attendant, SP – Suspicious passenger.

CODE/PROGRAM/EXAMPLE
for passenger in "A","A", "FC", "C", "FA", "SP", "A", "A":
	if(passenger=="FC" or passenger=="FA"):
		print("No check required")
		continue

	if(passenger=="SP"):
		print("Declare emergency in the airport")
		break

	if(passenger=="A" or passenger=="C"):
		print("Proceed with normal security check")

	print("Check the person")
	print("Check for cabin baggage")
		
Output:
	Proceed with normal security check
	Check the person
	Check for cabin baggage
	Proceed with normal security check
	Check the person
	Check for cabin baggage
	No check required
	Proceed with normal security check
	Check the person
	Check for cabin baggage
	No check required
	Declare emergency in the airport

In python, pass is a null statement which is used to do create empty blocks. When pass is executed, it results in no operation and the control will move to the next statement applicable. Below example program shows how pass can be used to create a empty if block.

CODE/PROGRAM/EXAMPLE
//Program Example :

	num=10
	count=0

	while(count <= num):
		if(count%2 == 0):
			pass
		else:
			print(count)
		count+=1
		
Output:
1
3
5
7
9
#looping_statements_in_python #python_while_loop #python_do_while #python_for_loop_example #break_statement_in_python #python_for_loop_range #control_statements_in_python

(New page will open, for Comment)

Not yet commented...