0% found this document useful (0 votes)
12 views11 pages

2nd Class - Control Structures

Control structures allow programmers to control the flow of execution of code. The main types are decision-making statements like if/else that allow branching based on conditions, and looping statements like for and while that repeatedly execute code. For loops iterate over objects like lists or strings, while while loops repeat as long as a condition is true. Keywords like break, continue, and pass alter loop execution flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

2nd Class - Control Structures

Control structures allow programmers to control the flow of execution of code. The main types are decision-making statements like if/else that allow branching based on conditions, and looping statements like for and while that repeatedly execute code. For loops iterate over objects like lists or strings, while while loops repeat as long as a condition is true. Keywords like break, continue, and pass alter loop execution flow.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Control Structures

Python – 2nd Class


Agenda
 Control Structures
 Types
 Execution Flow
 Examples
Control Structures

Decision Making Statements Looping Statements

 for
 if / elif / else
 while

Syntax: Syntax:
if condition: for VariableName in ObjectName / range( ):
--statements-- --statements--
elif condition:
--statements-- Syntax:
else: while condition:
--statements-- --statements--
Executions of Controls Structures
 if / elif / else: Note: A if block can have multiple elif block, but it should have only one else
block.

Compiler Enters into

if
True
conditio Example:
n a = 200
Output
Fals b = 33
elif
e
conditio True
if b > a:
n   print("b is greater than a")
Output elif a == b:
Fals   print("a and b are equal")
e else:
else   print("a is greater than b")
Output
Executions of Controls Structures……
 for
Compiler Enters into
1

Object
Name
2 Assigns All Values to

No
in
Yes
4
3
Finds Assigns Next
Assign 1st Values to
Next Value
Value Variabl
e
Output
 for - Examples Output:
• To loop through a set of code a specified number of times, we can use Example: 0
the range( ) function, for a in range(4) 12
• The range( ) function returns a sequence of numbers, starting from 0 by : 3

default, and increments by 1 (by default), and ends at a specified number. Note:  print(a)
that range(4) is not the values of 0 to 4, but
the values 0 to 3.

Note: The range( ) function Note: The range( ) function  Nested for


defaults to 0 as a starting defaults to increment the Output:
0
value, however it is possible sequence by 1, however it is for i in range(3): 1
to specify the starting value possible to specify the increment for j in range(2): 0
1
by adding a parameter. value by adding a third parameter print(j) 0
1
Output:
Example: Output: Example: 2
for a in range(2, 4) 2 for a in range(2, 10, 2 4
3 6 for i in range(3):
: ): 8 Output:
for j in range(2): 01
  print(a)   print(a) 01
print(j, end=' ') 01
String: print(sep = '\n')
Output:
Example: Hello
for x in "Hello":
  print(x, end=' ')
Nested For Loop Execution:

 FOR loop inside another FOR loop is called Nested FOR loop.
 Outer FOR is considered as Main Loop and Inner FOR is considered as Sub
Loops.
 When the Main Loop condition gets true, then compiler enters into sub loop and
executed continuously in the sub loop until sub loop condition gets failed.
 After sub loop condition gets failed, then compiler will return back to Main Loop.
 When the Main Loop condition gets failed, then compiler displays the output.
 Note: Whenever the compiler enters into Sub Loop, then the sub loop will start
from it’s initial value.
 while Example:
 break
We can stop the loop before it has looped through all the elements.
Note: We can execute a set of i = 1 Output:  continue
1
statements as long as a while i < 4: 2
We can stop the current iteration of the loop, and continue with the
  print(i) next
condition is true. 3

  i += 1  pass


Used to pass the set of block and continue the next

Example:
Pass Example: for a in
for a in range(6): Output: Output:
0 for a in range(6): range(4):
print(a) 0
1 print(a) if a == 2: 1
if a == 2: 2 3
if a == 2:
break; continue
pass
print(a)
Example: i = 0
print(“Hello”)
i = 1
while i < 4: Output:
while i < 6: Output:
  print(i) 1
  i += 1 1
2
  if i == 3: 2   if i == 3: 4
3
    break     continue
  i += 1   print(i)
FAQ:

1. What is Control Structure?


2. Types in Control Structures?
3. Difference between Decision Making and Looping Statements?
4. Difference between for and while Loop?
5. What is the use of range( ) function?
6. Various Parameters in range( ) function?
7. What is the default starting range in range( ) function?
8. What is the default incrementation value in range( ) function?
9. Difference between break, continue and pass?

You might also like