📘 CodingMarble Learn

Flow of Control in Python

Flow of control is the order in which statements run. Python has three kinds: sequential (top to bottom), selection (if, if-else, if-elif-else) and repetition (for and while loops). Blocks are marked by indentation (usually 4 spaces after a colon). range(start, stop, step) gives numbers from start up to but not including stop. while repeats as long as a condition is true. break exits a loop at once; continue skips to the next turn. A loop inside another is a nested loop and is used for patterns; loops with an accumulator are used for series sums.

🎬 Step-by-step story

  1. Python groups lines by indentation. Lines moved in by the same spaces after a colon form one block.
  2. if-elif-else checks conditions from the top. The first true one runs. For 72 marks the grade is C.
  3. for i in range(1, 6) gives 1, 2, 3, 4, 5. It stops before 6. The total grows to 15.
  4. A while loop repeats while its condition is true. continue skips one turn. break stops the loop.
  5. In nested loops, the outer loop picks the row. The inner loop prints the stars in that row.
  6. Your turn: choose rows and a pattern, and see the output.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why does Python use spaces instead of brackets?

Indentation makes the code neat and shows the structure at a glance. Python reads the spaces to know which lines belong together.

If marks are 95, does the '≥ 50' branch also run?

No. The first true condition (≥ 90) runs and Python skips every other elif and else.

Why does range stop before the stop value?

So that range(n) gives exactly n numbers, 0 to n − 1, matching list indexes.

When should I use while instead of for?

When you don't know the number of turns in advance, like 'keep asking until the password is right'.

How many times does the inner loop run in total?

For each outer turn it runs completely. With 4 rows it runs 1 + 2 + 3 + 4 = 10 times.

Indentation and blocks

Python uses indentation (spaces at the start of a line) to group statements into a block. A block starts after a line ending with a colon (if, else, for, while) and all its lines must be indented equally (4 spaces is the standard). Wrong indentation gives an IndentationError.

if marks >= 33:
    print('Pass')      # inside if
print('Done')          # outside, always runs

Selection: if, if-else, if-elif-else

if n > 0:
    print('positive')

if n % 2 == 0:
    print('even')
else:
    print('odd')

if m >= 90:
    g = 'A'
elif m >= 75:
    g = 'B'
elif m >= 50:
    g = 'C'
else:
    g = 'D'

In if-elif-else, only the first true branch runs; the rest are skipped. else is optional and has no condition.

The for loop and range()

for repeats a block once for each item in a sequence.

The stop value is never included. for ch in 'cat': loops over each character.

The while loop

while repeats as long as its condition is true. Something inside must change the condition, or it loops forever (infinite loop).

n = 1
while n <= 5:
    print(n)
    n += 1

Use for when you know how many times to repeat; use while when you repeat until something happens.

break and continue

break ends the loop immediately; the program goes to the first line after the loop.

continue skips the rest of the current turn and goes to the next turn.

for n in range(1, 11):
    if n == 8:
        break
    if n % 3 == 0:
        continue
    print(n)      # 1 2 4 5 7

A loop may have an else part that runs only if the loop ended without break.

Nested loops and pattern programs

A loop inside another loop is a nested loop. For each turn of the outer loop, the inner loop runs completely.

for i in range(1, 5):
    for j in range(1, i + 1):
        print('*', end=' ')
    print()

Output: * / * * / * * * / * * * * (one row per line). Change what is printed to get number (j) or letter (chr(64 + j)) patterns.

Series programs

A series sum uses an accumulator that starts at 0 (or 1 for products) and adds a term each turn.

# 1 + 1/2 + 1/3 + ... + 1/n
s = 0
for i in range(1, n + 1):
    s += 1 / i

# 1 - x + x**2 - x**3 ... (n terms)
s = 0
for i in range(n):
    s += (-1) ** i * x ** i

Other practical programs: factorial, Fibonacci, prime check, Armstrong, perfect and palindrome numbers, GCD/LCM.

Key formulas and definitions

Worked examples

1. Write a program to print the larger of two numbers.

a = int(input()); b = int(input()) if a > b: print(a) else: print(b)

2. What does for i in range(10, 0, -3): print(i, end=' ') print?

10 7 4 1

3. Find the sum 1 + 2 + … + n using while.

n = int(input()); s = 0; i = 1 while i <= n: s += i i += 1 print(s) # n = 10 gives 55

4. Check whether a number is prime.

n = int(input()) if n < 2: print('Not prime') else: for d in range(2, n): if n % d == 0: print('Not prime'); break else: print('Prime')

5. Print the first 8 Fibonacci numbers.

a, b = 0, 1 for _ in range(8): print(a, end=' ') a, b = b, a + b # 0 1 1 2 3 5 8 13

6. Print this number pattern for 4 rows: 1 / 1 2 / 1 2 3 / 1 2 3 4.

for i in range(1, 5): for j in range(1, i + 1): print(j, end=' ') print()

7. Find the sum of 1 + x + x² + … + xⁿ for x = 2, n = 4.

s = 0 for i in range(0, 5): s += 2 ** i # 1 + 2 + 4 + 8 + 16 = 31

8. Check if 153 is an Armstrong number.

n = 153; s = 0; t = n while t > 0: d = t % 10; s += d ** 3; t //= 10 print(s == n) # 1 + 125 + 27 = 153 → True

Common mistakes

Practice quiz

1. range(3, 8) gives:
2. Which statement skips to the next iteration?
3. Python marks a block using:
4. How many times does for i in range(0, 10, 2) run?
5. In if-elif-else, how many branches run?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the types of flow of control in Python?

Sequential, selection (if, if-else, if-elif-else) and repetition (for and while loops).

What is the difference between for and while loops?

for repeats over a sequence or range a known number of times; while repeats as long as a condition stays true.

What do break and continue do?

break exits the loop immediately; continue skips the rest of the current iteration and goes to the next.

Where this is taught

CBSE (India)Class 11Computational Thinking and Programming - 1
CBSE (India)Class 11Introduction to Python
USA (Common Core, NGSS, AP)Grade 8Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 9Algorithms and Programming
USA (Common Core, NGSS, AP)Grade 11Selection and Iteration
South Korea고등학교 2학년Algorithms and programming
South Korea고등학교 3학년Programming

Learn first

Learn next

Related lessons

All Computer Science lessons