csaccept.com is a computer awareness website dedicated to providing reliable and easy-to-understand information about computer technology and digital safety. The website focuses on educating students, beginners, and general users about computer basics, cyber security, emerging technologies, and practical IT skills. Through informative articles, quizzes, and real-life examples, csaccept.com aims to increase digital literacy and help users stay safe and confident in today’s technology-driven world.
Facebook ………………..
Instagram ……………..
Twitter ( X ) ……………..
YouTube
Flow Control in Python Programming
Flow control in programming refers to the mechanism that controls the sequence in which statements are executed in a program. By default, programs run line by line, but flow control allows developers to make decisions, repeat tasks, and execute different blocks of code based on conditions.
In Python, flow control is mainly achieved using:
- Conditional statements (
if,elif,else) - Loops (
for,while)
Conditional statements help the program decide what to do, while loops help the program decide how many times to do it.
Python Indentation
Indentation is a fundamental concept in Python. Unlike other programming languages that use braces {} to define blocks of code, Python uses indentation (spaces) to indicate code blocks and structure.
Key Points:
- Indentation defines the scope of statements.
- All statements within the same block must have equal indentation.
- The standard convention is to use four spaces for indentation.
- Incorrect indentation can lead to errors or unexpected output.
Example of Indentation in Python
# Author: CodewithCurious.com
if condition:
statement_1
statement_2
statement_3
else:
statement_4
statement_5
In the above example:
- The statements under
ifform the if-block - The statements under
elseform the else-block - Python identifies these blocks purely through indentation
Decision-Making Statements in Python
Decision-making allows a program to choose different paths of execution based on conditions. Python provides the following decision-making statements:
Types of Decision-Making Statements:
- If statement
- If-else statement
- Elif statement
- Nested if statement
These statements are widely used in real-world applications such as grading systems, eligibility checks, login validation, and more.
1. If Statement
The if statement is used to execute a block of code only when a given condition is true. It is the simplest form of decision-making in Python.
Flowchart (Conceptual):
Start → Condition → True → If Block → Stop
Syntax:
if condition:
# code to be executed if the condition is true
If the condition evaluates to False, the program skips the if-block and moves to the next statement.
Example:
age = 25
if age >= 18:
print("You are an adult")
print("You can vote")
Explanation:
Here, the program checks whether age is greater than or equal to 18. Since the condition is true, both print statements are executed.
2. If-Else Statement
The if-else statement allows the program to choose between two alternative blocks of code depending on whether the condition is true or false.
Syntax:
if condition:
# code executed if condition is true
else:
# code executed if condition is false
Example:
age = 15
if age >= 18:
print("You are an adult.")
print("You can vote.")
else:
print("You are not an adult!")
Explanation:
Since the age is less than 18, the condition becomes false and the else-block is executed.
3. Elif Statement
The elif (else-if) statement is used when there are multiple conditions to be checked. It helps avoid writing multiple nested if statements and improves code readability.
Syntax:
if condition1:
# executes if condition1 is true
elif condition2:
# executes if condition2 is true
elif condition3:
# executes if condition3 is true
else:
# executes if all conditions are false
Important Points:
- Conditions are checked from top to bottom
- Only one block is executed
- Once a condition is true, remaining conditions are skipped
Example:
score = 75
if score >= 90:
print("Excellent!")
elif score >= 80:
print("Very good!")
elif score >= 70:
print("Good!")
else:
print("Keep practicing!")
Explanation:
Since the score is 75, the condition score >= 70 becomes true, and the program prints “Good!”.
4. Nested If Statement
A nested if statement is an if statement placed inside another if statement. It is useful when a decision depends on multiple levels of conditions.
Syntax:
if condition1:
if condition2:
# executes if both conditions are true
else:
# executes if condition1 is true and condition2 is false
else:
# executes if condition1 is false
Example:
age = 25
income = 50000
if age >= 18:
print("You are eligible.")
if income >= 30000:
print("You qualify for a loan.")
else:
print("You do not qualify for a loan")
else:
print("You are not eligible.")
Explanation:
- First, the program checks if the person is 18 or older.
- If true, it then checks whether income is sufficient.
- This allows more precise decision-making.
Conclusion
Flow control statements are the foundation of logical programming in Python. Understanding if, if-else, elif, and nested if statements helps students:
- Write efficient programs
- Handle real-world conditions
- Prepare for exams, interviews, and projects
Mastering these concepts is essential for anyone learning Python programming.
Short Question–Answer
(Flow Control & Decision Making in Python)
1. What is flow control in programming?
Flow control refers to controlling the order in which statements are executed in a program.
2. Why is flow control important?
It helps in decision-making and repeating actions based on conditions.
3. How is flow control achieved in Python?
Using conditional statements (if, elif, else) and loops (for, while).
4. What are conditional statements?
Statements that execute different code blocks based on conditions.
5. What is indentation in Python?
Indentation defines the structure and scope of code blocks in Python.
6. Why is indentation important in Python?
Python uses indentation instead of braces to define code blocks.
7. What is the standard indentation in Python?
Four spaces.
8. What happens if indentation is incorrect?
It causes syntax or logical errors.
9. What is a decision-making statement?
Statements that allow execution of code based on conditions.
10. Name decision-making statements in Python.
If, if-else, elif, and nested if.
11. What is an if statement?
It executes code only when the condition is true.
12. What happens if an if condition is false?
The code inside the if block is skipped.
13. What is an if-else statement?
It executes one block if the condition is true and another if false.
14. What is the use of else?
It executes code when the condition is false.
15. What is elif?
It checks multiple conditions sequentially.
16. When is elif used?
When there are more than two possible conditions.
17. How many elif statements can be used?
Any number.
18. What is a nested if statement?
An if statement inside another if statement.
19. When is the inner if executed?
Only when the outer if condition is true.
20. What does Python evaluate first in nested if?
The outer if condition.
Multiple Choice Questions (MCQs)
(50 MCQs – Flow Control & Decision Making)
1. Flow control is used to:
A) Store data
B) Control execution order
C) Design UI
D) Compile code
✅Answer: B
2. Which keyword is used for decision making in Python?
A) loop
B) switch
C) if
D) define
✅Answer: C
3. Python uses ______ to define code blocks.
A) Brackets
B) Semicolon
C) Indentation
D) Comma
✅Answer: C
4. Standard indentation in Python is:
A) 2 spaces
B) 3 spaces
C) 4 spaces
D) 5 spaces
✅Answer: C
5. Which statement checks a condition?
A) for
B) if
C) print
D) input
✅Answer: B
6. Which statement executes code when condition is false?
A) if
B) elif
C) else
D) break
✅Answer: C
7. Which is not a decision-making statement?
A) if
B) elif
C) else
D) for
✅Answer: D
8. The condition in if statement returns:
A) int
B) string
C) True or False
D) float
✅Answer: C
9. What happens if if condition is false?
A) Error occurs
B) Code runs
C) Code is skipped
D) Program stops
✅Answer: C
10. Which statement is checked after if when first condition fails?
A) else
B) while
C) elif
D) for
✅Answer: C
11. elif stands for:
A) end if
B) else if
C) exit if
D) enable if
✅Answer: B
12. How many else blocks are allowed?
A) One
B) Two
C) Many
D) None
✅Answer: A
13. Which statement must come last?
A) if
B) elif
C) else
D) while
✅Answer: C
14. Nested if means:
A) Loop inside if
B) if inside if
C) else inside elif
D) for inside while
✅Answer: B
15. Inner if runs when:
A) Outer if is false
B) Program starts
C) Outer if is true
D) Always
✅Answer: C
16. Which improves decision clarity?
A) Nested if
B) elif
C) indentation
D) variables
✅Answer: B
17. Python ignores code block without indentation.
A) True
B) False
✅Answer: A
18. Which is correct syntax?
A) if(condition)
B) if condition
C) if condition:
D) if {condition}
✅Answer: C
19. Which symbol ends if statement?
A) ;
B) .
C) :
D) ,
✅Answer: C
20. Which statement is optional?
A) if
B) elif
C) else
D) both B and C
✅Answer: D
21. elif is used to:
A) Repeat code
B) End program
C) Check multiple conditions
D) Define function
✅Answer: C
22. Which keyword is compulsory?
A) elif
B) else
C) if
D) pass
✅Answer: C
23. Indentation error is a:
A) Runtime error
B) Logical error
C) Syntax error
D) Semantic error
✅Answer: C
24. Which block executes only once?
A) if
B) for
C) while
D) loop
✅Answer: A
25. Flow control helps in:
A) Decision making
B) Repetition
C) Logic building
D) All of the above
✅Answer: D
26. Which statement is not conditional?
A) if
B) else
C) elif
D) print
✅Answer: D
27. else executes when:
A) condition is true
B) condition is false
C) program ends
D) loop runs
✅Answer: B
28. Multiple elif improves:
A) Speed
B) Readability
C) Memory
D) Compilation
✅Answer: B
29. Nested if is used for:
A) Simple decision
B) Complex decision
C) Looping
D) Printing
✅Answer: B
30. Which is evaluated first?
A) inner if
B) else
C) outer if
D) elif
✅Answer: C
31. Python conditions use:
A) =
B) ==
C) !=
D) All of the above
✅Answer: D
32. Flow control statements are:
A) Sequential
B) Conditional
C) Looping
D) Both B and C
✅Answer: D
33. Which is correct?
A) if age > 18
B) if age > 18:
C) if(age > 18)
D) if {age > 18}
✅Answer: B
34. elif cannot be used without:
A) else
B) loop
C) if
D) print
✅Answer: C
35. else cannot appear without:
A) if
B) elif
C) loop
D) function
✅Answer: A
36. Which is mandatory after if?
A) indentation
B) print
C) loop
D) variable
✅Answer: A
37. Decision making is based on:
A) Data types
B) Conditions
C) Functions
D) Classes
✅Answer: B
38. Which keyword checks first condition?
A) else
B) elif
C) if
D) pass
✅Answer: C
39. Which helps execute alternate code?
A) if
B) elif
C) else
D) continue
✅Answer: C
40. Flow control changes:
A) Syntax
B) Execution order
C) Output format
D) Memory size
✅Answer: B
41. Which statement has no condition?
A) if
B) elif
C) else
D) nested if
✅Answer: C
42. Python code block starts after:
A) comma
B) colon
C) semicolon
D) dot
✅Answer: B
43. Indentation groups:
A) Variables
B) Statements
C) Functions
D) Loops
✅Answer: B
44. Which block executes when all conditions fail?
A) if
B) elif
C) else
D) nested if
✅Answer: C
45. Multiple decisions use:
A) if
B) if-else
C) elif
D) while
✅Answer: C
46. Which is not a keyword?
A) if
B) elif
C) else
D) when
✅Answer: D
47. Python evaluates conditions:
A) Randomly
B) Bottom to top
C) Top to bottom
D) Twice
✅Answer: C
48. Which is best for eligibility checks?
A) Loop
B) if
C) Function
D) Class
✅Answer: B
49. Nested if increases:
A) Speed
B) Complexity
C) Errors
D) Memory
✅Answer: B
50. Flow control is essential for:
A) Static programs
B) Logical programs
C) No programs
D) Only loops
✅Answer: B
flow-control-mcq

