Flow of Control Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- Types of Control Statements: Sequential (default top-to-bottom); Selection / Decision (conditional: if, if-else, if-elif-else); Iteration / Repetition (loops: for, while).
- Indentation: Python uses indentation (standard 4 spaces) instead of curly braces {} to define code blocks. Inconsistent indentation raises an IndentationError.
- Conditional Statements: if condition:; if condition: ... else:; if condition1: ... elif condition2: ... else:. Conditions evaluate to True or False.
- The range() Function: Generates a sequence of integers: range(stop) (from 0 up to stop-1); range(start, stop) (from start up to stop-1); range(start, stop, step) (increments by step, can be negative for reverse counting).
- The while Loop: Condition-controlled loop; continues executing the code block as long as the condition remains True; loop variable must be initialized before and updated inside the loop to prevent infinite loops.
- The for Loop: Count-controlled or sequence-traversing loop; iterates over items of any sequence (string, list, tuple, range).
- Loop Jump Statements: break (terminates loop immediately and transfers control to statement following the loop); continue (skips the remainder of the current iteration and jumps to the next iteration); pass (null statement, does nothing, used as placeholder).
- Loop else Clause: Both for and while loops can have an optional else block. The else block executes ONLY when the loop terminates normally (exhausts range or condition becomes false), NOT when terminated by a break statement.
Chapter Formulas & Equations (10 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | The range() Function Mathematical Sequence |
|
End bound is strictly exclusive; step defaults to 1 if omitted; negative step decrements down towards stop + 1. |
| 2 | Number of Iterations Formula for range() |
|
Calculates the exact total number of loop executions produced by a given range(start, stop, step) statement. |
| 3 | Loop Else Clause Execution Condition |
|
Useful for search algorithms to execute code when an element is NOT found in the collection. |
| 4 | Break vs Continue Control Flow Action |
|
break halts the entire loop immediately, while continue bypasses remaining lines in current pass and restarts. |
| 5 | Nested Loop Total Iterations |
|
For every single cycle of the outer loop, the inner loop executes through its entire sequence of iterations. |
| 6 | Ternary Conditional Operator (Single-Line If) |
|
Concise Python syntactic expression to assign values conditionally in a single statement. |
| 7 | Sentinel-Controlled While Loop Termination |
|
Standard pattern in interactive applications where loop duration cannot be predetermined. |
| 8 | Sum of First N Natural Numbers Loop Formula |
|
Mathematical formula used to verify the correct output of simple counting and accumulator loops. |
| 9 | Factorial Loop Multiplication Accumulator |
|
Standard algorithm for calculating factorials iteratively in Python. |
| 10 | Pass Statement (No-Op Placeholder) |
|
Enables programmers to define empty function shells or stub out loop bodies without causing syntax errors. |
CBSE Board Exam Tips & Frequent Pitfalls
- In loop questions, watch out for range(1, 10): it generates numbers from 1 to 9 (10 is EXCLUDED!).
- If range(start, stop, step) has start > stop and step is positive, it executes 0 times!
- Remember that the else block of a loop does NOT execute if the loop is terminated by a break.
Step-by-Step Worked Board Exam Examples
Example 1: What will be the output of the following Python code snippet? for i in range(1, 6): if i == 3: continue if i == 5: break print(i, end=' ') else: print('Done')
Iteration 1: i = 1. Neither condition met. Prints: 1 .
Iteration 2: i = 2. Neither condition met. Prints: 2 .
Iteration 3: i = 3. i == 3 is True, continue triggered, skips print, jumps to next iteration.
Iteration 4: i = 4. Neither condition met. Prints: 4 .
Iteration 5: i = 5. i == 5 is True, break triggered, loop terminates immediately.
Because the loop terminated due to break, the else block is NOT executed.
Example 2: Write a Python program to find whether a given integer is a prime number.
Take input integer N.
If N <= 1, it is not prime.
Loop divisor d from 2 up to int(N**0.5) + 1:
If N % d == 0, N has a divisor, print 'Not Prime' and break.
Use the else block of the for loop: if no divisor found without breaking, print 'Prime Number'.
Common Mistakes to Avoid in Exam
- Expecting range(1, 5) to include 5. In Python ranges, the stop value is always exclusive (generates 1, 2, 3, 4).
- Writing infinite while loops by forgetting to increment the loop control counter inside the loop body.
- Thinking continue exits the loop. continue only skips the rest of the current iteration; break exits the loop.
Target Practice Questions
Q1. How many times will the loop 'for i in range(10, 2, -2):' execute?
Correct Answer: Executes 4 times (for values: 10, 8, 6, 4).
Q2. What is the output of list(range(5)) in Python?
Correct Answer: [0, 1, 2, 3, 4].
Q3. What error occurs if code blocks are not indented consistently in Python?
Correct Answer: An IndentationError occurs.
Frequently Asked Questions
Q. What is the difference between break and continue?
break terminates the loop completely; continue skips the rest of the current iteration and jumps to the next cycle.
Q. When does the else clause of a for loop execute?
The else clause executes only when the loop terminates naturally after finishing all iterations, and NOT when terminated by a break.
Q. What is the purpose of the pass statement?
It is a null operation used as a placeholder where code is syntactically required but no action needs to be executed.
Q. What does range(5, 0, -1) generate?
Generates the countdown sequence: 5, 4, 3, 2, 1.