SlideShare a Scribd company logo
Control Structures
A control structure is a block of programming that analyzes variables and
decides which statement to execute next, based on the given parameters.
The term ‘control’ denotes the direction in which the program flows.
Usually, loops are used to execute a control statement, a certain number
of times.
Basically, control structures determine the flow of events in the program.
If statement: This is used to check a condition and executes the
operations/statements within the if block only when the given condition is
true.
Syntax:
if condition:
True Statements
If…else statements
If…else statements: These statements are used to check a
condition and executes the operations/statements within
the if block only when the given condition is true. If the
given condition is false, the statements in the else block
will be executed.
Syntax:
if condition:
True Statements
else:
False Statements
If …elif… else statements
If …elif… else statements: If we want to check more than one condition we can use
the elif statements. If a condition is true then the statements within the if block
will be executed. If the condition is false, we can provide an elif statement with a
second condition and the statements within the elif block will be executed only
when the condition is true. We can provide multiple elif statements and an else
statement at the end if all the above conditions are false.
Syntax:
if condition:
True Statements
elif condition2:
True Statements
elif condition3:
True Statements
…….
else:
False Statements
Loops in Python
• Loops are used to repeat a set of statements/single statement, a certain
number of times. In Python, there are two loops, for loop and while loop. The
Python for loop also works as an iterator to iterate over items in
list/dictionary or characters in strings.
for Loop: It can be used to iterate over a list/string/dictionary or iterate over a
range of numbers.
Syntax:
for variable in range(starting number , ending number + 1 , step size):
statements
(or)
for element in sequence:
statements
While Loop
While Loop: This is used, whenever a set of statements should be
repeated based on a condition. The control comes out of the loop
when the condition is false. In while loop we must explicitly
increment/decrement the loop variable (if any) whereas in for, the
range function would automatically increment the loop variable.
Syntax:
while condition:
statement(s)
increment/decrement
Break and Continue Statement
break statement: This statement is used to terminate the loop it is present in. Control
goes outside the loop it is present in. If a break statement is present in a nested
loop, it only comes out of the innermost loop.
Syntax:
while condition:
statments
if condition:
break
statements
Continue statement: This statement is used to skip the current iteration. The loop will
not be terminated, it just won’t execute the statements below the continue
statement. The incrementing will be done in for loop. If the increment statement is
written below continue, it won’t be executed in while loop.
Syntax:
while condition:
statement(s)
if condition:
continue
statements
Pass Statement
Pass statement: This statement is used as placeholder. For example, we
want to create a function but are not sure of its content. If we create a
function and leave it, an error will occur. To counter this error, we use
pass statement.
Syntax:
def function(parameters):
pass
(or)
for elements in sequence:
pass
(or)
while condition:
pass
(or)
if condition:
pass

More Related Content

Similar to Control Structures Python like conditions and loops

Similar to Control Structures Python like conditions and loops (20)

Flow of control
Flow of controlFlow of control
Flow of control
 
6.pptx
6.pptx6.pptx
6.pptx
 
control statements in python.pptx
control statements in python.pptxcontrol statements in python.pptx
control statements in python.pptx
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
chapter 8 class 10 icse board notes.pptx
chapter 8 class 10 icse board notes.pptxchapter 8 class 10 icse board notes.pptx
chapter 8 class 10 icse board notes.pptx
 
Cse lecture-7-c loop
Cse lecture-7-c loopCse lecture-7-c loop
Cse lecture-7-c loop
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
Python session3
Python session3Python session3
Python session3
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
 
C language 2
C language 2C language 2
C language 2
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
presentation on powerpoint template.pptx
presentation on powerpoint template.pptxpresentation on powerpoint template.pptx
presentation on powerpoint template.pptx
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
Control_Statements.pptx
Control_Statements.pptxControl_Statements.pptx
Control_Statements.pptx
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Do While Repetition Structure
Do While Repetition StructureDo While Repetition Structure
Do While Repetition Structure
 

Recently uploaded

ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 

Recently uploaded (20)

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 

Control Structures Python like conditions and loops

  • 1. Control Structures A control structure is a block of programming that analyzes variables and decides which statement to execute next, based on the given parameters. The term ‘control’ denotes the direction in which the program flows. Usually, loops are used to execute a control statement, a certain number of times. Basically, control structures determine the flow of events in the program. If statement: This is used to check a condition and executes the operations/statements within the if block only when the given condition is true. Syntax: if condition: True Statements
  • 2. If…else statements If…else statements: These statements are used to check a condition and executes the operations/statements within the if block only when the given condition is true. If the given condition is false, the statements in the else block will be executed. Syntax: if condition: True Statements else: False Statements
  • 3. If …elif… else statements If …elif… else statements: If we want to check more than one condition we can use the elif statements. If a condition is true then the statements within the if block will be executed. If the condition is false, we can provide an elif statement with a second condition and the statements within the elif block will be executed only when the condition is true. We can provide multiple elif statements and an else statement at the end if all the above conditions are false. Syntax: if condition: True Statements elif condition2: True Statements elif condition3: True Statements ……. else: False Statements
  • 4. Loops in Python • Loops are used to repeat a set of statements/single statement, a certain number of times. In Python, there are two loops, for loop and while loop. The Python for loop also works as an iterator to iterate over items in list/dictionary or characters in strings. for Loop: It can be used to iterate over a list/string/dictionary or iterate over a range of numbers. Syntax: for variable in range(starting number , ending number + 1 , step size): statements (or) for element in sequence: statements
  • 5. While Loop While Loop: This is used, whenever a set of statements should be repeated based on a condition. The control comes out of the loop when the condition is false. In while loop we must explicitly increment/decrement the loop variable (if any) whereas in for, the range function would automatically increment the loop variable. Syntax: while condition: statement(s) increment/decrement
  • 6. Break and Continue Statement break statement: This statement is used to terminate the loop it is present in. Control goes outside the loop it is present in. If a break statement is present in a nested loop, it only comes out of the innermost loop. Syntax: while condition: statments if condition: break statements Continue statement: This statement is used to skip the current iteration. The loop will not be terminated, it just won’t execute the statements below the continue statement. The incrementing will be done in for loop. If the increment statement is written below continue, it won’t be executed in while loop. Syntax: while condition: statement(s) if condition: continue statements
  • 7. Pass Statement Pass statement: This statement is used as placeholder. For example, we want to create a function but are not sure of its content. If we create a function and leave it, an error will occur. To counter this error, we use pass statement. Syntax: def function(parameters): pass (or) for elements in sequence: pass (or) while condition: pass (or) if condition: pass