Control Flow Statements in Python
Introduction
1. Sequential Statements:
- This is the default flow of execution, where statements are executed one after the other in the order they appear in the code.
2. Conditional Statements (Decision Making):
- if statement: Executes a block of code if a specified condition is true.
- if-else statement: Executes one block of code if a condition is true, and a different block if the condition is false.
- if-elif-else statement: Allows for multiple conditions to be tested, executing the corresponding code block for the first true condition.
3. Iterative Statements (Loops):
- for loop: Iterates over a sequence (e.g., list, tuple, string) or a range of numbers, executing a block of code for each item in the sequence.
- while loop: Repeats a block of code as long as a specified condition remains true.
4. Jump Statements:
- break statement: Terminates the current loop prematurely.
- continue statement: Skips the remaining code within the current loop iteration and moves to the next iteration.
- pass statement: Acts as a placeholder, allowing you to write syntactically correct code without actually doing anything.
So far, Every programming language provides constructs to support sequences, selection or iteration constructs. Similarly, In a python program, statement May be executed sequentially, selectively or iteratively. For all these types of program executions, python also facilitates some Control Flow Statements to use.
Examples:
# if-else statement
age = 18
if age >= 18:
print(“You are eligible to vote.”)
else:
print(“You are not eligible to vote yet.”)
# for loop
fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits:
print(fruit)
# while loop
count = 0
while count < 5:
print(“Count:”, count)
count += 1
Control Flow Statements in Python
In this section “Control Flow Statements in Python“, we will provide a comprehensive introduction to all types of control flow statements which are used in programing with examples including the following topics
- Types of statements in python
- Sequence Type Control Flow Statements in Python
- Selection Type Control Flow Statements in Python
- Iteration (Looping) Types Control Flow Statements in Python
And, by the end of this tutorial, you will have a solid understanding of all about Control Flow Statements in Python like Sequence, Selection and Looping and will be able to use this knowledge in their own network projects.
Also this tutorial covers all necessary topics/concepts required to complete your exams preparations in CBSE schools / classes 11th and 12th.
Also , you can Sign Up our free Computer Science Courses for classes 11th and 12th.
Generally, a program executes its statement from beginning to end. but not many programs execute all their statements in strict order from beginning to end. programs, depending upon the need can chose to executes one of the available alternatives or even repeat a set of statements to perform their, manipulative miracles, programs need tools for performing repetitive actions and for making decisions. python of course provides such tools by providing statements to attain so. Such statements are program control statements. This section discusses such statements in details. Firstly, selection statement if and later iteration statements for and while are discussed.
Types of statements in python
Statement are the instructions given to the computer to perform any kind of action, be it data movement, and be it making decisions or be it repeating action any kind of actions. Statement forms the smallest executable unit within a python program. Python statement can belong to one of the following three type:
- Empty statement
- Compound statement
- Simple statement
1. Empty statement
The simple statement is the empty statement i.e., a statement which does nothing. In python an empty statement is pass statement. it takes the following from:
Pass
Wherever python encounters a pass statement, python does nothing and moves to next statement in the flow of control.
A pass statement is useful in those instances where the syntax in the language requires the presence of a statement but where the logic of the program does not. We will see it in loops and their bodies.
2. Simple statement
Any single executable statement in a simple statement in a python. for example, following is a simple statement in python:
name = input (“your name”)
Another example of simple statement is:
Print (name) #print function called
As you can make out that simple statement are single line statements.
3. Compound statement
A compound statement represents a group of statement executed as a unit. the compound statement of python is written in a specific pattern as shown below:
<compound statement header >:
<indented body containing multiple and /or compound statement >
That is, a compound statement has:
- A header line which begins with a keyword and ends with a colon.
- A body consisting of one or more python statements, each intendent1 inside the header line. all statement in the body is at the same level of indentation.
Sequence Type Control Flow Statements in Python
Every python program begins with the first statement of the program. Each statement is the turn is executed (sequences construct). When the final statement of program is executed, the program is done. Sequences refers to the normal flow of control in a program and is the simplest one.
Selection Type Control Flow Statements in Python
The selection construct means the execution of statement (S) depending upon a condition-test. If a condition evaluates to true, a course of action (a set of statement) is followed otherwise another course-of-action (a different set of statement) if followed. this construct (selection construct) is also called decision construct because it helps in making decision about which set-of-statement is to be executed.
Following figure explains selection construct.
You apply decision-making or selection in your real life so many times e.g., if the traffic signal light is red, then stop; if the traffic signal light is yellow then wait; and if the signal light is green then go.
You can think of many such real lie examples of selection / decision-making.
Iteration (Looping) Types Control Flow Statements in Python
The iteration constructs mean repetition of a set-of- statement depending upon a condition-test. till the time a condition is a true (or false depending upon the loop), a set-of-statement are repeated again and again. as soon as the condition becomes false (or true), the repetition stops. the iteration construct is also called lopping construct.
The adjacent figure illustrates an iteration construct.
The set-of-statements that are repeated again and again is called the body of the loop. The condition on which the execution or exit of the loop depends is called the exit condition or test-condition.
You can find many examples of iteration or looping around you. for instance, you often see your mother cook chapati or dosas or appams for you.
Let’s see, what she does for it:
- Put rolled chapati or dosa batter on flat pan or tawa
- Turn it 2-3 times
- Once done take it off.
Repeat the above process [steps (1) (2) (3)] for the next chapati/dosa/appam. This is looping or iteration.
You can find numerous other examples of repetitive work in your real life e.g., washing clothes; colouring book etc. etc.
Every programming language must support these three types of constructs as the sequential program execution (the default mode) is inadequate to the problem we must solve. Python also provides statement that support these constructs. Coming section discuss these pythons’ statement-if that supports selection and for and while that supports iteration. Using this statement, you can create programs as per your need.
But before we talk about these statements, you should know basic tools that will help you decide about the logic to solve a given problem i.e., the algorithm. For this, there are multiple tools available. In the following section, we are going to talk about three such tools-flow charts, decision trees and pseudocode.