Python | Print diagonals of 2D list

Learn examples from DataCamp guru:

Using loops in Python automates and repeats tasks efficiently. But sometimes a condition may arise where you want to exit the loop completely, skip iteration, or ignore this condition. This can be done using loop control statements. Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects created in that scope are destroyed. Python supports the following control statements.

  • Continue statement
  • Break statement
  • Go through statement

In this article the focus will be on the continue statement.

Continue statement

Continue is also a loop control statement, just like the break statement. The continue statement is the opposite of the break statement, instead of terminating the loop, it forces the next iteration of the loop to run. 
As the name suggests, the continue statement causes the loop to continue or to perform the next iteration. When the continue statement is executed in a loop, the code within the loop following the continue statement will be skipped and the next iteration of the loop will begin.

Syntax :


Consider the situation when you need to write a program that prints a number between 1 and 10, not 6. It is specified that you should do this with a loop, and only one loop is allowed. 
This is where the continue statement is used. Here we can start a loop from 1 to 10, and every time we need to compare the value of the iterator with 6. If it is 6, we will use the continue statement to go to the next iteration, without printing anything else, we will print the value.

Below is the implementation of the above idea:

# Python program for
# demonstrate continued
# statement

# loop from 1 to 10

for i in range ( 1 , 11 ):


# If I am 6

# go to next iteration

# no printing

if i = = 6 :


else :

# otherwise output the value

# I

print (i, end = " " )


 1 2 3 4 5 7 8 9 10 

The continue statement can be used with any other loop, for example, at the same time as for the for loop described above.

Exercise Problem:
Given the number n, print a triangular pattern. We are only allowed to use one loop.

 Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * *  


Learn examples from DataCamp guru: