SlideShare a Scribd company logo
1 of 16
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
1
Decision Control Statements
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
2
Control Statements
A control statement is a statement that determines the control flow of a set of instructions, i.e., it
decides the sequence in which the instructions in a program are to be executed.
Types of Control Statements —
• Sequential Control: A Python program is executed sequentially from the first line of the program to
its last line.
• Selection Control: To execute only a selected set of statements.
• Iterative Control: To execute a set of statements repeatedly.
If Statement
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
3
Example:
If-Else Statement
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
4
Example:
Nested if Statements
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
5
A statement that contains other statements is called a compound statement. To perform more complex checks, if
statements can be nested, that is, can be placed one inside the other. In such a case, the inner if statement is the
statement part of the outer one. Nested if statements are used to check if more than one conditions are satisfied.
Example:
If-elif-else Statement
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
6
Python supports if-elif-else statements to test additional conditions apart from the initial test expression. The if-
elif-else construct works in the same way as a usual if-else statement. If-elif-else construct is also known as
nested-if construct.
Example:
While Loop
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
7
Example:
For Loop
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
8
For loop provides a mechanism to repeat a task until a particular condition is True. It is usually known as a
determinate or definite loop because the programmer knows exactly how many times the loop will repeat.
The for...in statement is a looping statement used in Python to iterate over a sequence of objects.
For Loop and Range() Function
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
9
The range() function is a built-in function in Python that is used to iterate over a sequence of numbers. The syntax
of range() is range(beg, end, [step])
The range() produces a sequence of numbers starting with beg (inclusive) and ending with one less than the
number end. The step argument is option (that is why it is placed in brackets). By default, every number in the
range is incremented by 1 but we can specify a different increment using step. It can be both negative and positive,
but not zero.
Examples:
Range() Function
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
10
If range() function is given a single argument, it produces an object with values from 0 to argument-1. For example:
range(10) is equal to writing range(0, 10).
• If range() is called with two arguments, it produces values from the first to the second. For example, range(0,10).
• If range() has three arguments then the third argument specifies the interval of the sequence produced. In this
case, the third argument must be an integer. For example, range(1,20,3).
Examples:
Condition-controlled and Counter-controlled Loops
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
11
Nested Loops
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
12
Python allows its users to have nested loops, that is, loops that can be placed inside other loops. Although this
feature will work with any loop like while loop as well as for loop.
A for loop can be used to control the number of times a particular set of statements will be executed. Another
outer loop could be used to control the number of times that a whole loop is repeated.
Loops should be properly indented to identify which statements are contained within each for statement.
Example:
The Break Statement
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
13
The break statement is used to terminate the execution of the nearest enclosing loop in which it appears. The
break statement is widely used with for loop and while loop. When compiler encounters a break statement, the
control passes to the statement that follows the loop in which the break statement appears.
Example:
The Continue Statement
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
14
Like the break statement, the continue statement can only appear in the body of a loop. When the compiler
encounters a continue statement then the rest of the statements in the loop are skipped and the control is
unconditionally transferred to the loop-continuation portion of the nearest enclosing loop.
Example:
The Pass Statement
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
15
Pass statement is used when a statement is required syntactically but no command or code has to be executed. It
specified a null operation or simply No Operation (NOP) statement. Nothing happens when the pass statement is
executed.
Difference between comment and pass statements In Python programming, pass is a null statement. The
difference between a comment and pass statement is that while the interpreter ignores a comment entirely, pass
is not ignored. Comment is not executed but pass statement is executed but nothing happens.
Example:
The Else Statement Used With Loops
© OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
16
Unlike C and C++, in Python you can have the else statement associated with a loop statements. If the else
statement is used with a for loop, the else statement is executed when the loop has completed iterating. But
when used with the while loop, the else statement is executed when the condition becomes false.
Examples:

More Related Content

Similar to FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov-2022_Chapter_4.pptx

Similar to FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov-2022_Chapter_4.pptx (20)

Exception handling.pptx
Exception handling.pptxException handling.pptx
Exception handling.pptx
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Loops
LoopsLoops
Loops
 
Loops
LoopsLoops
Loops
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
 
R loops
R   loopsR   loops
R loops
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Ch-5.pdf
Ch-5.pdfCh-5.pdf
Ch-5.pdf
 
Python session3
Python session3Python session3
Python session3
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Decision making and looping - c programming by YEASIN NEWAJ
Decision making and looping -  c programming by YEASIN NEWAJDecision making and looping -  c programming by YEASIN NEWAJ
Decision making and looping - c programming by YEASIN NEWAJ
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Control statements
Control statementsControl statements
Control statements
 

More from jaychoudhary37

Reflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptxReflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptxjaychoudhary37
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Metamaterial Devices and types of antenna
Metamaterial Devices and types of antennaMetamaterial Devices and types of antenna
Metamaterial Devices and types of antennajaychoudhary37
 
smart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.pptsmart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.pptjaychoudhary37
 
introduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptintroduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptjaychoudhary37
 
Metamaterial Devices and There description
Metamaterial Devices and There descriptionMetamaterial Devices and There description
Metamaterial Devices and There descriptionjaychoudhary37
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...jaychoudhary37
 
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...jaychoudhary37
 
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdfELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdfjaychoudhary37
 

More from jaychoudhary37 (9)

Reflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptxReflector Antennas Design and mathematical expression_1.pptx
Reflector Antennas Design and mathematical expression_1.pptx
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Metamaterial Devices and types of antenna
Metamaterial Devices and types of antennaMetamaterial Devices and types of antenna
Metamaterial Devices and types of antenna
 
smart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.pptsmart antenna and types of smart antenna7453684.ppt
smart antenna and types of smart antenna7453684.ppt
 
introduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.pptintroduction to Microcontrollers 8051.ppt
introduction to Microcontrollers 8051.ppt
 
Metamaterial Devices and There description
Metamaterial Devices and There descriptionMetamaterial Devices and There description
Metamaterial Devices and There description
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_06-Dec...
 
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
Embedded_System_and_Smart_Phone_based_Object_Recognition_Technique_for_Visual...
 
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdfELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
ELECTRONIC COMMUNICATION SYSTEM BY GEORGE KENNEDY.pdf
 

Recently uploaded

MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov-2022_Chapter_4.pptx

  • 1. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 1 Decision Control Statements
  • 2. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 2 Control Statements A control statement is a statement that determines the control flow of a set of instructions, i.e., it decides the sequence in which the instructions in a program are to be executed. Types of Control Statements — • Sequential Control: A Python program is executed sequentially from the first line of the program to its last line. • Selection Control: To execute only a selected set of statements. • Iterative Control: To execute a set of statements repeatedly.
  • 3. If Statement © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 3 Example:
  • 4. If-Else Statement © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 4 Example:
  • 5. Nested if Statements © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 5 A statement that contains other statements is called a compound statement. To perform more complex checks, if statements can be nested, that is, can be placed one inside the other. In such a case, the inner if statement is the statement part of the outer one. Nested if statements are used to check if more than one conditions are satisfied. Example:
  • 6. If-elif-else Statement © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 6 Python supports if-elif-else statements to test additional conditions apart from the initial test expression. The if- elif-else construct works in the same way as a usual if-else statement. If-elif-else construct is also known as nested-if construct. Example:
  • 7. While Loop © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 7 Example:
  • 8. For Loop © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 8 For loop provides a mechanism to repeat a task until a particular condition is True. It is usually known as a determinate or definite loop because the programmer knows exactly how many times the loop will repeat. The for...in statement is a looping statement used in Python to iterate over a sequence of objects.
  • 9. For Loop and Range() Function © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 9 The range() function is a built-in function in Python that is used to iterate over a sequence of numbers. The syntax of range() is range(beg, end, [step]) The range() produces a sequence of numbers starting with beg (inclusive) and ending with one less than the number end. The step argument is option (that is why it is placed in brackets). By default, every number in the range is incremented by 1 but we can specify a different increment using step. It can be both negative and positive, but not zero. Examples:
  • 10. Range() Function © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 10 If range() function is given a single argument, it produces an object with values from 0 to argument-1. For example: range(10) is equal to writing range(0, 10). • If range() is called with two arguments, it produces values from the first to the second. For example, range(0,10). • If range() has three arguments then the third argument specifies the interval of the sequence produced. In this case, the third argument must be an integer. For example, range(1,20,3). Examples:
  • 11. Condition-controlled and Counter-controlled Loops © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 11
  • 12. Nested Loops © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 12 Python allows its users to have nested loops, that is, loops that can be placed inside other loops. Although this feature will work with any loop like while loop as well as for loop. A for loop can be used to control the number of times a particular set of statements will be executed. Another outer loop could be used to control the number of times that a whole loop is repeated. Loops should be properly indented to identify which statements are contained within each for statement. Example:
  • 13. The Break Statement © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 13 The break statement is used to terminate the execution of the nearest enclosing loop in which it appears. The break statement is widely used with for loop and while loop. When compiler encounters a break statement, the control passes to the statement that follows the loop in which the break statement appears. Example:
  • 14. The Continue Statement © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 14 Like the break statement, the continue statement can only appear in the body of a loop. When the compiler encounters a continue statement then the rest of the statements in the loop are skipped and the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing loop. Example:
  • 15. The Pass Statement © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 15 Pass statement is used when a statement is required syntactically but no command or code has to be executed. It specified a null operation or simply No Operation (NOP) statement. Nothing happens when the pass statement is executed. Difference between comment and pass statements In Python programming, pass is a null statement. The difference between a comment and pass statement is that while the interpreter ignores a comment entirely, pass is not ignored. Comment is not executed but pass statement is executed but nothing happens. Example:
  • 16. The Else Statement Used With Loops © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED. 16 Unlike C and C++, in Python you can have the else statement associated with a loop statements. If the else statement is used with a for loop, the else statement is executed when the loop has completed iterating. But when used with the while loop, the else statement is executed when the condition becomes false. Examples: