1. Chapter at a glance
- Flow of control is the order of execution of statements in a program and is implemented using selection and repetition control structures.
- Selection implements decision making through the
if, if..else and if..elif..else statements.
- Python uses indentation (leading whitespace) to group statements into blocks; incorrect indentation raises syntax errors.
- Repetition (iteration) is achieved with the
for and while looping constructs.
- The
for loop iterates over a sequence or the values produced by range(); the number of iterations is known in advance.
- The
while loop executes its body repeatedly while the test condition remains true; the condition is checked before each iteration.
break terminates the current loop and transfers control to the statement following the loop; continue skips the remaining statements of the current iteration and begins the next iteration.
- A loop contained inside another loop is called a nested loop; any type of loop may be nested inside any other.
2. Key terms and definitions
- Flow of control: The order of execution of the statements in a program.
- Selection: The control structure that allows choosing between two or more alternative paths on the basis of a condition (implemented by
if, if..else, if..elif).
- Repetition / Iteration: Execution of a set of statements repeatedly under a given condition (implemented by
for and while).
- Indentation: Leading whitespace (spaces or tabs) at the beginning of a statement that groups statements into a single block; Python enforces it strictly.
- Control variable: The variable whose value decides whether the loop body is executed.
- Nested if: An
if..else construct placed inside another if or elif block.
- Infinite loop: A loop that never terminates because its condition never becomes false.
- Break statement: A statement that terminates the current loop and resumes execution with the statement immediately following the loop.
- Continue statement: A statement that skips the remaining statements in the current iteration and transfers control to the beginning of the next iteration.
- Nested loop: A loop placed inside the body of another loop.
3. Syntax and constructs
if statement
python
if condition:
statement(s)
Example:
python
if age >= 18: print("Eligible to vote")
if..else statement
python
if condition:
statement(s)
else:
statement(s)
Example:
python
if num1 > num2: diff = num1 - num2
else: diff = num2 - num1
if..elif..else statement
python
if condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Example:
python
if number > 0: print("positive")
elif number < 0: print("negative")
else: print("zero")
for loop
python
for control-variable in sequence/items in range:
statement(s)
Example:
python
for letter in 'PYTHON': print(letter)
range() function
python
range([start], stop[, step])
Example:
python
for num in range(5): print(num * 10)
while loop
python
while test_condition:
body of while
Example:
python
count = 1
while count <= 5:
print(count)
count += 1
break statement
python
break
Example:
python
if num == 8: break
continue statement
python
continue
Example:
python
if num == 3: continue
4. Algorithms and worked logic
- Positive difference algorithm (from flowchart): Read two numbers; if first > second subtract second from first, else subtract first from second; output the result.
- Simple calculator logic: Accept two numbers and an operator; use
if..elif chain for +, -, *, /; for - apply the positive-difference logic; for / check that the second operand is not zero; otherwise display error.
- Loop to print first N natural numbers: Initialise count to 1; while/for count ≤ N, print count and increment count.
- Factor-finding procedure (while): Print 1; start factor at 2; while factor ≤ num/2, if num % factor == 0 print factor and increment factor; finally print the number itself.
- Prime-checking logic (nested): Assume number is prime (flag = 0); for each possible divisor from 2 to num/2, if any divides the number set flag = 1 and break; after the loop decide prime/not-prime from flag.
- Pattern printing (nested for): Outer loop runs from 1 to num; inner loop runs from 1 to current outer value and prints the inner value followed by space; after inner loop, print newline.
- Factorial calculation: If number < 0 display error; if 0 output 1; else initialise fact = 1 and multiply fact by each integer from 1 to number using a
for loop.
5. Common errors and exam pitfalls
- Missing or inconsistent indentation (different number of spaces/tabs for statements belonging to the same block) produces syntax errors.
- Forgetting to update the control variable inside a
while loop, resulting in an infinite loop.
- Placing statements that should be inside a loop or conditional block at the wrong indentation level, causing incorrect output or logical errors.
- Using
elif without a preceding if, or writing multiple else clauses for a single if.
- Forgetting that
range(stop) excludes the stop value, or omitting the required type conversion when using input().
- Not handling the case when the divisor is zero in division operations or when the loop condition is initially false (body executes zero times).
- Incorrect placement of
break/continue outside any loop or using them without proper conditional guards, leading to loss of marks in dry-run or program questions.