
Flow of Control Statements in Python | CBSE Class 11| Computer Science
Dear Class 11th STUDENTS, ! Welcome to this tutorial of Control Flow in Python from your CBSE class 11 of Computer Science Syllabus .
In this tutorial, we shall be learning our chapter-7: Control Flow in Python from Unit 2: Programming and Computational Thinking (PCT-1) as CBSE BOARD suggested to learn about computer system and its organisation to complete this section.
Unit 2: Programming and Computational Thinking (PCT-1)
chapter-7: Control Flow in Python
- 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
I advice you to check the latest syllabus given by CBSE Board at its Official website: www.cbseacademic.nic.in
Also, in this tutorial we will covers all necessary topics/concepts required to complete your exams preparations in CBSE classes 11th.
Also, you can Sign Up our free Computer Science Courses for classes 11th and 12th.
NOTE:
- We are also giving some important Questions & Answers for better understanding as well as preparation for your examinations.
- You may also download PDF file of this tutorial from our SHOP for free.
- For your ease and more understanding, we are also giving the video explanation class of each and every topic individually, so that you may clear your topics and get success in your examinations.
Introduction
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 action and for making decision. python of course provides such tools by providing statements to attain so. Such statements in details. firstly, selection statement if and later iteration statements for and while are discussed. this tutorial also discusses some jump statement of python which are break and continue.
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.
Statements Flow Control in Python
In a program, statement May be executed sequentially, selectively or iteratively. Every programming language provides constructs to support sequences, selection or iteration constructs.
In this section “Statements Flow Control in Python“, we will provide have an introduction to all types of control flow statements which are used in programing with examples including the following topics.
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.
Repetition Iteration (Looping) 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.