Chapter 23 - Break and Continue Statements
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Quick Recap
- Quick Recap – Repetition Structures in Python
In previous Chapter, I presented
- Real-world Repetitive Task
- Definition
- A Real-world Repetitive Task is made up of a set of action(s) that are repeated over and over again (and may involve small variations in each iteration)
- Purpose
- The main purpose of a Real-world Repetitive Task is to
- Break a big Real-world Task into several (very similar) Sub-tasks and repeat a set of actions(s) over and over again on Sub-tasks to complete the big Real-world Task
- The main purpose of a Real-world Repetitive Task is to
- Definition
- Iteration
- Definition
- Iteration is defined as the repetition of a set of action(s) to produce an Outcome (or Result).
- Definition
- Types of Real-world Repetitive Tasks
- The Two Main Types of Real-world Repetitive Tasks are
- Real-world Repetitive Tasks with Fixed Number of Iterations
- Real-world Repetitive Tasks with Variable Number of Iterations
- The Two Main Types of Real-world Repetitive Tasks are
- Repetitive Programming Task
- A Repetitive Programming Task is defined as a Programming Task in which there is little variation from one item to the next item of the Programming Task
- Types of Repetitive Programming Task
- The Two Main Types of Repetitive Programming Tasks are
- Repetitive Programming Task with Fixed Number of Iterations
- Repetitive Programming Task with Variable Number of Iterations
- The Two Main Types of Repetitive Programming Tasks are
- Menu Driven Program
- Definition
- A Menu Driven Program is a Program which takes Input from a User by displaying a Menu (i.e., a List of Options (Choices)) and the User indicates his / her Choice form the given Menu
- Definition
- Repetition Structure
- Definition
- A Repetition Structure (a.k.a. Loop) is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
- Purpose
- The main purpose of Repetition Structure it to perform Repetitive Programming Tasks
- Definition
- Four Main Components of a Loop (Repetition Structure)
- In Python Programming, the Four Main Components of a Loop are
- Initialization
- Condition (Test)
- Body of Loop
- Increment / Decrement
- In Python Programming, the Four Main Components of a Loop are
- while Loop
- Definition
- In Python Programming, a while loop is used to execute a Set of Python Statements (i.e., Perform a Repetitive Programming Task) if a Condition is True
- Purpose
- The main purpose of while Loop is to
- Perform Repetitive Programming Tasks
- The main purpose of while Loop is to
- for Loops
- Definition
- The for loop in Python is used to iterate the statements or a part of the program several times
- Purpose
- The for loop is a generic sequence iterator in Python: it can step through the items in any ordered sequence objects
- Definition
Continue Statement in Python
- Continue Statement
- Definition
- The continue statement causes an immediate jump to the top of a loop. It also sometimes lets you avoid statement nesting
- Purpose
- The main purpose of Continue is also a loop control statement
- it forces to execute the next iteration of the loop
- The main purpose of Continue is also a loop control statement
- Importance
- Continue statement forces the loop to continue or execute the next iteration.
- When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin
- Suitable to Use
- continue statement continues to the next iteration. It skips back to the start of the loop. We use the continue statement where we need it inside the code block
- Continue Statement – Flow Chart
- Example 1 - Continue Statement (Print First Five Odd Numbers)
Plan and Design Solution to the Repetitive Programming Task |
Identify Input-Processing-Output |
1 3 5 7 9
1 3 5 7 9
|
Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task |
SET number = 0 WHILE number is less than 10 INCREMENT number by +1 IF number % 2 == 0 CONTINUE PRINT number EndWhile |
Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed |
|
Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3) |
Suitable Programming Environment to Write Software (Code) |
'''
Program Details
__author_ = Ms. Samavi Suleman
__copyright__ = Copyright (C) 2020 Ms. Samavi Suleman
__license__ = Public Domain
__program__name__ = Display First Five Odd Numbers
__program__version__ = 1.1
__programing__language__ = python 3.8.3
__operating__system__ = Ubuntu 18.04.1 LTS 64 bit
Operating System
__IDE __ = jupyter__notebook 5.5.0
___Start__Date__ = 18-05-2021
____End__Date___ = 01-06-2021
'''
'''
Purpose of Program
------------------
The main purpose of this Program is to Display first Five Odd Numbers on the Output Screen'''
try:
# Initialize Integer Variable
number = 0
# Processing + Output
while number < 10:
number += 1
if number % 2 == 0:
continue
print(number)
except:
print("Exception Occured")
Execution of Code – Dry Run | ||||
Python Statement | Processing | Memory | Output Screen | |
Initialization | ||||
number = 0 | Initialize Variable number with value = 0 | number = 0 | – | |
while number < 10: number += 1 if number % 2 == 0: continue print(number) Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code) | ||||
Condition – Loop | ||||
Iteration 01 – While Loop | Condition
Condition is True, So Execute Body of Loop | number = 0 | – | |
Body of While Loop | ||||
number += 1 number = 0 + 1 | number = 1 | – | ||
Condition – If Statement | ||||
Condition
Condition is False, Skip the Body of if Statement | number = 1 | – | ||
print(number) | number = 1 | 1- | ||
Iteration 02 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 1 | 1- | ||
Body of While Loop | ||||
number += 1 number = 1 + 1 | number = 2 | 1- | ||
Condition – If Statement | ||||
Condition
Condition is True, Execute the Body of if Statement | number = 2 | 1- | ||
Body of If Condition | ||||
Continue | number = 2 | 1- | ||
Iteration 03 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 2 | 1- | ||
Body of While Loop | ||||
number += 1 number = 2 + 1 | number = 3 | 1- | ||
Condition – If Statement | ||||
Condition
Condition is False, Skip the Body of if Statement | number = 3 | 1- | ||
print(number) | number = 3 | 1 3- | ||
Iteration 04 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 3 | 1 3- | ||
Body of While Loop | ||||
number += 1 number = 3 + 1 | number = 4 | 1 3- | ||
Condition – If Statement | ||||
Condition
Condition is True, Execute the Body of if Statement | number = 4 | 1 3- | ||
Body of If Condition | ||||
Continue | number = 4 | 1 3- | ||
Iteration 05 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 4 | 1 3- | ||
Body of While Loop | ||||
number += 1 number = 4 + 1 | number = 5 | 1 3- | ||
Condition – If Statement | ||||
Condition
Condition is False, Skip the Body of if Statement | number = 5 | 1 3- | ||
print(number) | number = 5 | 1 3 5- | ||
Iteration 06 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 5 | 1 3 5- | ||
Body of While Loop | ||||
number += 1 number = 5 + 1 | number = 6 | 1 3 5- | ||
Condition – If Statement | ||||
Condition
Condition is True, Execute the Body of if Statement | 1 3 5- | |||
Body of If Condition | ||||
Continue | number = 6 | 1 3 5- | ||
Iteration 07 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 6 | 1 3 5- | ||
Body of While Loop | ||||
number += 1 number = 6 + 1 | number = 7 | 1 3 5- | ||
Condition – If Statement | ||||
Condition
Condition is False, Skip the Body of if Statement | number = 7 | 1 3 5- | ||
print(number) | number = 7 | 1 3 5 7- | ||
Iteration 08 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 7 | 1 3 5 7- | ||
Body of While Loop | ||||
number += 1 number = 7 + 1
| number = 8 | 1 3 5 7- | ||
Condition – If Statement | ||||
Condition
Condition is True, Execute the Body of if Statement | number = 8 | 1 3 5 7- | ||
Body of If Condition | ||||
Continue | number = 8 | 1 3 5 7- | ||
Iteration 09 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 8 | 1 3 5 7- | ||
Body of While Loop | ||||
number += 1 number = 8 + 1 | number = 9 | 1 3 5 7- | ||
Condition – If Statement | ||||
Condition
Condition is False, Skip the Body of if Statement | 1 3 5 7- | |||
print(number) | number = 9 | 1 3 5 7 9- | ||
Iteration 10 – While Loop | Condition – Loop | |||
Condition
Condition is True, So Execute Body of Loop | number = 9 | 1 3 5 7 9- | ||
Body of While Loop | ||||
number += 1 number = 9 + 1 | number = 10 | 1 3 5 7 9- | ||
Condition – If Statement | ||||
Condition
Condition is True, Execute the Body of if Statement | number = 10 | 1 3 5 7 9- | ||
Body of If Condition | ||||
Continue | number = 10 | 1 3 5 7 9- | ||
Iteration 11 – While Loop | Condition – Loop | |||
Condition
Condition is False, Loop will Terminate | 1 3 5 7 9 | |||
Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊 |
Step 07: Implementation |
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for the Tasks given above by following the Template given in this Chapter i.e.,
- Write Down the Algorithm to Solve the Repetitive Programming Task
- Use Algorithm Description to Write Pseudo Code
- Design and Draw Flow Chart(s)
- Suitable Programming Environment to Write Software (Code)
- Implementation
- Execution of Code – Dry Run
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
0 2 4 6 7 8 10 12 14 16 18 20 22 24 |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter Name of Employee: Samavi Enter Salary: 60000 Enter Name of Employee: Ayesha Enter Salary: 50000 Enter Name of Employee: Awais Enter Salary: 100000 |
Your Turn Task 1
- Task
- Write at least 10 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Template mentioned in this Chapter i.e.,
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for the Tasks given above by following the Template given in this Chapter i.e.,
- Write Down the Algorithm to Solve the Repetitive Programming Task
- Use Algorithm Description to Write Pseudo Code
- Design and Draw Flow Chart(s)
- Suitable Programming Environment to Write Software (Code)
- Implementation
- Execution of Code – Dry Run
- Write Python Programs for the Tasks given above by following the Template given in this Chapter i.e.,
Break Statement in Python
- Break Statement in Python
- Definition
- The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break
- Purpose
- To exit a loop immediately without running any remaining code in the loop, regardless of the results of any conditional test, use the break statement
- The break statement directs the flow of your program; you can use it to control which lines of code are executed and which aren’t, so the program only executes code that you want it to, when you want it to
- To exit a loop immediately without running any remaining code in the loop, regardless of the results of any conditional test, use the break statement
- Importance
- The break statement is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement
- If the break statement is present in the nested loop, then it terminates only those loops which contains break statement
- The break statement is used to terminate the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement
- Suitable to Use
- The break control statement will stop execution of the loop and break out of it, jumping to the next statement after and outside the loop.
- We use the break keyword anywhere we need inside the code block
- Break Statement – Flow Chart
- Example 1 - Break Statement (Print Name and Age)
Plan and Design Solution to the Repetitive Programming Task |
Identify Input-Processing-Output |
Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 Assalam-o-Alaikum Mariam => 25 Enter name: stop
|
Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Repetitive Programming Task |
WHILE true INPUT – name if name is equal to ‘stop’: BREAK INPUT – age PRINT – name and age EndWhile |
Use Algorithm Description (Step 6.2) to Write Pseudo Code for Software to be Developed |
|
Design and Draw Flow Chart(s) for Software to be Developed (based on Step 6.3) |
Suitable Programming Environment to Write Software (Code) |
'''
Program Details
__author_ = Ms. Samavi Suleman
__copyright__ = Copyright (C) 2020 Ms. Samavi Suleman
__license__ = Public Domain
__program__name__ = Display Name and Age
__program__version__ = 1.1
__programing__language__ = python 3.8.3
__operating__system__ = Ubuntu 18.04.1 LTS 64 bit
Operating System
__IDE __ = jupyter__notebook 5.5.0
___Start__Date__ = 18-05-2021
____End__Date___ = 01-06-2021
'''
'''
Purpose of Program
------------------
The main purpose of this Program is to Display Name and Age on the Output Screen'''
try:
# Processing + Output
while True:
name = input('Enter name: ')
if name == 'stop': break
age = int(input('Enter age: '))
print('Assalam-o-Alaikum', name, '=>', age)
except ValueError:
print("Please Enter Integer Value")
Execution of Code – Dry Run |
|||
while True: name = input(‘Enter name: ‘) if name == ‘stop’: break age = input(‘Enter age: ‘) print(‘Assalam-o-Alaikum’, name, ‘=>’, age) Note: In Sha Allah, In the Remaining Table we will show Iterations of the above for Loop (Code) | |||
Python Statement |
Processing |
Memory |
Output Screen |
Condition – Loop |
|||
Iteration 01 – While Loop | Condition
|
– | – |
Body of While Loop | |||
name = input(‘Enter name: ‘) | name = Samavi | Enter name: Samavi | |
Condition – If Statement | |||
Condition
|
name = Samavi | Enter name: Samavi | |
age = input(‘Enter age: ‘) | name = Samavi age = 24 | Enter name: Samavi Enter age: 24 | |
print(‘Assalam-o-Alaikum’, name, ‘=>’, age) | name = Samavi age = 24 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 | |
Iteration 02 – While Loop | Condition – Loop | ||
Condition
|
name = Samavi age = 24 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 | |
Body of While Loop | |||
name = input(‘Enter name: ‘) | name = Ayesha age = 24 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha | |
Condition – If Statement | |||
Condition
|
name = Ayesha age = 24 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha | |
age = input(‘Enter age: ‘) | name = Ayesha age = 26 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 | |
print(‘Assalam-o-Alaikum’, name, ‘=>’, age) | name = Ayesha age = 26 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 | |
Iteration 03 – While Loop | Condition – Loop | ||
Condition
|
name = Ayesha age = 26 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 | |
Body of While Loop | |||
name = input(‘Enter name: ‘) | name = Mariam age = 26 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam | |
Condition – If Statement | |||
Condition
|
name = Mariam age = 26 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam | |
age = input(‘Enter age: ‘) | name = Mariam age = 25 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 | |
print(‘Assalam-o-Alaikum’, name, ‘=>’, age) | name = Mariam age = 25 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 Assalam-o-Alaikum Mariam => 25 | |
Iteration 04 – While Loop | Condition – Loop | ||
Condition
|
name = Mariam age = 25 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 Assalam-o-Alaikum Mariam => 25 | |
Body of While Loop | |||
name = input(‘Enter name: ‘) | name = stop age = 25 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 Assalam-o-Alaikum Mariam => 25 Enter name: stop | |
Condition – If Statement | |||
Condition
|
name = Mariam age = 25 | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 Assalam-o-Alaikum Mariam => 25 Enter name: stop | |
Body of If Condition | |||
Break Break statement will Terminate the Loop | Enter name: Samavi Enter age: 24 Assalam-o-Alaikum Samavi => 24 Enter name: Ayesha Enter age: 26 Assalam-o-Alaikum Ayesha => 26 Enter name: Mariam Enter age: 25 Assalam-o-Alaikum Mariam => 25 Enter name: stop | ||
Program Termination: All the Python Statements in the Program are executed. Therefore, Program will Terminate 😊 |
TODO and Your Turn
TODO Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for the Tasks given above by following the Template given in this Chapter i.e.,
- Write Down the Algorithm to Solve the Repetitive Programming Task
- Use Algorithm Description to Write Pseudo Code
- Design and Draw Flow Chart(s)
- Suitable Programming Environment to Write Software (Code)
- Implementation
- Execution of Code – Dry Run
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
0 2 4 6 7 8 10 12 14 16 18 20 22 24 |
Task 2 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter Name of Employee: Samavi Enter Salary: 60000 Enter Name of Employee: Ayesha Enter Salary: 50000 Enter Name of Employee: Awais Enter Salary: 100000 |
Your Turn Task 1
- Task
- Write at least 10 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Template mentioned in this Chapter i.e.,
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Python Programs for the Tasks given above by following the Template given in this Chapter i.e.,
- Write Down the Algorithm to Solve the Repetitive Programming Task
- Use Algorithm Description to Write Pseudo Code
- Design and Draw Flow Chart(s)
- Suitable Programming Environment to Write Software (Code)
- Implementation
- Execution of Code – Dry Run
- Write Python Programs for the Tasks given above by following the Template given in this Chapter i.e.,
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
In this Chapter, I presented the following main concepts:
- Continue Statement
- Definition
- The continue statement causes an immediate jump to the top of a loop. It also sometimes lets you avoid statement nesting
- Purpose
- The main purpose of Continue is also a loop control statement
- it forces to execute the next iteration of the loop
- The main purpose of Continue is also a loop control statement
- Definition
- Break Statement
- Definition
- The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break
- Purpose
- To exit a loop immediately without running any remaining code in the loop, regardless of the results of any conditional test, use the break statement
- The break statement directs the flow of your program; you can use it to control which lines of code are executed and which aren’t, so the program only executes code that you want it to, when you want it to
- Definition
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Main concepts related to Functions in Python