PYTHON
UNIT – 2
CONTROL STRUCTURE
Unit Outcomes
- Understand different control structures in Python
- Learn the usage of loops and decision-making statements
What are Control Structures?
- Control structures dictate the flow of program execution
- Three types: Sequential, Selection, Looping
Selection Statements
- if
- if…else
- if…elif…else
Looping
- for loop
- while loop
- Nested loop
Loop Control
- break
- continue
- pass
Range Function
- Used to generate a sequence of numbers
- range(start, stop, step)
- Often used in loops
Looping Examples
- Sum of numbers
- Factorial
- Fibonacci series
Conditional Statements
- if condition:
statement
- if else:
if condition:
statement
else:
statement
Example Program
- Write a program to find the largest of three numbers
Looping Example
- Write a program to check prime number
- Use for loop and conditional statements
Example Program
- Write a program to find factorial using for loop
Nested Loop
- A loop inside another loop
- Useful in matrix operations
Break and Continue
- break: exit the loop
- continue: skip the current iteration
Control Flow Graph
- Graphical representation of control flow
- Nodes represent blocks
- Edges represent control paths
Summary
- Control structures manage flow
- if, elif, else used for decision making
- for and while used for loops
- break and continue alter loop behavior
Thank You
Thank You :)
?