Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Control Flow, Functions
  5. Loop Control Statements

Loop Control Statements

Loop Control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in the scope are destroyed

Python supports the following loop control statements:

continue

It returns the control to the beginning of the loop,

Syntax,

while(test conition):
if(condition for continue):
continue

Flowchart

Example

for i in "Learn Python":
      if(i == "n"):
         continue
      print("current letter",i)

Output,

current letter L
current letter e
current letter a
current letter r
current letter
current letter P
current letter y
current letter t
current letter h
current letter o

break

It brings control out of the loopIt terminates the current loop and executes the remaining statement outside the loopIf the loop has else statement, that will also gets terminated and come out of the loop completely

Syntax,

while(test conition):
if(condition for break):
break

Flowchart

Example

for i in "Learn Python":
    if(i == "n"):
        break
    print("current letter",i)

Output,

current letter L
current letter e
current letter a
current letter r

pass

pass statement is used to write empty loopsIt is used when a statement is required syntactically but you don’t want any code to executeIt is a null statement, nothing happens when it is executed

Syntax

pass

Example,

for i in "Learn Python":
    pass
print("current letter",i)

Output

current letter n

Difference between break and continue

Loading

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments