Current Affairs updated daily
KalviGavaniKalviGavani
Class 12 · Tamil Nadu State Board

Samacheer Kalvi 12th Computer Science Chapter 6 Control Structures Exercise Solutions | KalviGavani

Complete TN State Board Class 12 Computer Science Chapter 6 Control Structures exercise solutions — if, elif, else, for loop, while loop, break, continue, pass, range(), nested conditionals, and loop-else with Python code examples.

Updated 13 June 202611 min read

How to Use This Page

1. Attempt every exercise on your own before reading the solution. 2. Compare your approach with the solution — focus on the logic, not just the final answer. 3. Type and run the Python code on your computer; reading alone is not enough. 4. Review the Common Mistakes section to avoid errors that cost marks in exams.


Key Concepts

ConceptSyntaxPurpose
Simple ifif condition:Execute block only when condition is True
if-elseif … else:Choose between two alternatives
elifif … elif … else:Choose among multiple alternatives
Nested ifif … if …If inside another if
for loopfor var in sequence:Iterate over a sequence/range
while loopwhile condition:Repeat while condition is True
breakbreakExit the loop immediately
continuecontinueSkip rest of current iteration
passpassNull placeholder — do nothing
range()range(start, stop, step)Generate a sequence of numbers
loop-elsefor … else: / while … else:Execute block only if loop was not broken

Multiple Choice Questions (MCQs)

Q1. Which keyword is used to exit a loop prematurely in Python?

  • (a) exit
  • (b) break
  • (c) return
  • (d) stop
Answer(b) break

Q2. What does range(1, 10, 2) produce?

  • (a) 1, 2, 3, 4, 5, 6, 7, 8, 9
  • (b) 1, 3, 5, 7, 9
  • (c) 2, 4, 6, 8
  • (d) 1, 3, 5, 7
Answer(b) range(1, 10, 2) starts at 1, goes up to (but not including) 10, stepping by 2 — producing 1, 3, 5, 7, 9.

Q3. What is the output of the following code?

for i in range(5):
    if i == 3:
        continue
    print(i, end=" ")
  • (a) 0 1 2 3 4
  • (b) 0 1 2 4
  • (c) 0 1 2
  • (d) 0 1 2 4 5
Answer(b) When i == 3, continue skips printing, so the output is 0 1 2 4.

Q4. The else clause of a for loop executes when:

  • (a) The loop is terminated by break
  • (b) The loop completes normally
  • (c) The loop condition becomes False
  • (d) The loop never runs
Answer(b) The else block runs only when the loop finishes without encountering a break statement.

Q5. Which of the following is an infinite loop?

  • (a) for i in range(10):
  • (b) while True:
  • (c) while x == 5: (when x is 5)
  • (d) Both (b) and (c)
Answer(d) while True: always runs. while x == 5: is infinite when x is 5 and never changes inside the loop. Both (b) and (c) produce infinite loops.

Fill in the Blanks

Q1. The ______ statement is used to skip the remaining statements in the current iteration and move to the next iteration.

Answercontinue

Q2. range(5) produces the sequence ______.

Answer0, 1, 2, 3, 4

Q3. The ______ statement does nothing and acts as a placeholder.

Answerpass

Q4. In a nested if structure, an inner if is placed inside the block of an ______ if.

Answerouter

Q5. The ______ clause of a loop is executed only when the loop is not terminated by a break.

Answerelse

True or False

Q1. The elif keyword can be used without a preceding if.

AnswerFalse — elif must follow an if (or another elif).

Q2. A for loop in Python can iterate over a string.

AnswerTrue — iterating over a string yields each character.

Q3. The break statement terminates only the innermost loop in case of nested loops.

AnswerTrue — break exits only the loop it is directly inside.

Q4. The pass statement is the same as continue.

AnswerFalse — pass does nothing and continues execution within the same iteration; continue skips to the next iteration.

Q5. range(1, 5) includes the number 5.

AnswerFalse — range stops before the stop value, so it produces 1, 2, 3, 4.

Match the Following

Column AColumn B
1. break(a) Null operation placeholder
2. continue(b) Exits the loop
3. pass(c) Skips to next iteration
4. range()(d) Generates number sequence
5. else (with loop)(e) Runs when loop ends normally
Answer1-b, 2-c, 3-a, 4-d, 5-e

Short Answer Questions

Q1. Differentiate between break and continue with an example.

break terminates the loop entirely; continue skips only the current iteration.

# break example
for i in range(1, 6):
    if i == 3:
        break
    print(i, end=" ")
# Output: 1 2

# continue example
for i in range(1, 6):
    if i == 3:
        continue
    print(i, end=" ")
# Output: 1 2 4 5

Q2. What is the use of range()? Explain with three forms.

range() generates a sequence of integers. It has three forms:

range(stop)              # range(5)        → 0,1,2,3,4
range(start, stop)       # range(2, 6)     → 2,3,4,5
range(start, stop, step) # range(1, 10, 2)  → 1,3,5,7,9

Q3. Explain the pass statement with an example.

pass is a null statement that does nothing. It is used where Python syntax requires a statement but no action is needed.

for i in range(5):
    pass  # placeholder — loop does nothing but is syntactically valid

def empty_function():
    pass  # TODO: implement later

Q4. What is a nested if? Give an example.

A nested if is an if statement placed inside another if or else block.

marks = 85
if marks >= 60:
    if marks >= 90:
        print("Grade A+")
    elif marks >= 75:
        print("Grade A")
    else:
        print("Grade B")
# Output: Grade A

Q5. Write the syntax and meaning of if-elif-else.

if condition1:
    # block when condition1 is True
elif condition2:
    # block when condition2 is True
else:
    # block when no condition is True

Python checks conditions top to bottom. The first True block runs; remaining conditions are skipped.


Detailed Answers

Q1. Explain all control structures in Python with Python programs for each.

Python provides three categories of control structures:

(a) Sequential Structure

Statements execute one after another in order.

name = input("Enter name: ")
print("Hello", name)

(b) Selection Structure (Decision Making)

Simple if:

num = int(input("Enter a number: "))
if num > 0:
    print(num, "is positive")

if-else:

num = int(input("Enter a number: "))
if num % 2 == 0:
    print(num, "is even")
else:
    print(num, "is odd")

if-elif-else:

marks = int(input("Enter marks: "))
if marks >= 90:
    print("Grade A+")
elif marks >= 75:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Grade C")

Nested if:

num = int(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive")
else:
    print("Negative")

(c) Iteration Structure (Loops)

for loop:

# Print squares of numbers 1 to 5
for i in range(1, 6):
    print(i, "squared is", i * i)

while loop:

# Print numbers from 1 to 5 using while
i = 1
while i <= 5:
    print(i, end=" ")
    i += 1
# Output: 1 2 3 4 5

Q2. Write a Python program to check whether a given number is prime using a for-else construct.

num = int(input("Enter a number: "))
if num < 2:
    print(num, "is not a prime number")
else:
    for i in range(2, num):
        if num % i == 0:
            print(num, "is not a prime number")
            break
    else:
        # else block runs only if loop was NOT broken
        print(num, "is a prime number")

How it works: The for loop tries to find a divisor. If it finds one, break exits and the else block is skipped. If no divisor is found, the loop finishes normally and the else block confirms the number is prime.

Q3. Write a program to find the factorial of a number using a while loop.

n = int(input("Enter a number: "))
if n < 0:
    print("Factorial is not defined for negative numbers")
else:
    fact = 1
    i = 1
    while i <= n:
        fact *= i
        i += 1
    print("Factorial of", n, "is", fact)

Trace for n = 5: fact starts at 1. Loop runs for i = 1,2,3,4,5 → fact = 1×2×3×4×5 = 120.

Q4. Write a program to print the Fibonacci series up to n terms using a for loop.

n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
    print(a, end=" ")
    a, b = b, a + b
# Input: 8 → Output: 0 1 1 2 3 5 8 13

Q5. Write a program to find the sum of digits of a number using a while loop.

num = int(input("Enter a number: "))
n = abs(num)  # handle negative input
total = 0
while n > 0:
    digit = n % 10
    total += digit
    n //= 10
print("Sum of digits =", total)
# Input: 1234 → Output: Sum of digits = 10

Q6. Explain break, continue, and pass with examples.

break — exits the loop immediately:

for i in range(1, 11):
    if i == 5:
        break
    print(i, end=" ")
# Output: 1 2 3 4

continue — skips current iteration:

for i in range(1, 11):
    if i % 2 == 0:
        continue
    print(i, end=" ")
# Output: 1 3 5 7 9

pass — does nothing, used as placeholder:

for i in range(5):
    pass  # syntactically required, no action
print("Done")
# Output: Done

Q7. Write a program to print all Armstrong numbers between 1 and 1000.

print("Armstrong numbers between 1 and 1000:")
for num in range(1, 1001):
    temp = num
    power = len(str(num))
    total = 0
    while temp > 0:
        digit = temp % 10
        total += digit ** power
        temp //= 10
    if total == num:
        print(num, end=" ")
# Output: 1 2 3 4 5 6 7 8 9 153 370 371 407

Q8. Write a program using nested loops to print a multiplication table (1 to 5).

for i in range(1, 6):
    for j in range(1, 11):
        print(f"{i} x {j} = {i*j}")
    print("---")

Common Mistakes

MistakeWhy It's WrongCorrection
range(1, 5) expecting 5range stops before the stop valueUse range(1, 6) to include 5
Missing colon after if, elif, else, for, whileSyntaxErrorAlways add : at the end
Forgetting indentation after :Python uses indentation for blocksIndent 4 spaces consistently
Using = instead of == in conditions= is assignment; == is comparisonUse == for comparisons
Infinite while loop — forgetting to update the loop variableCondition never becomes FalseEnsure the loop variable changes inside the loop
Using break when you meant continuebreak exits the whole loopUse continue to skip one iteration only
Forgetting self in method definitions inside a classCauses TypeErrorAlways add self as the first parameter

Exam Checklist

  • [ ] I can write if, if-else, if-elif-else, and nested if structures correctly
  • [ ] I can use for loops with range() in all three forms
  • [ ] I can write while loops and avoid infinite loops
  • [ ] I understand break, continue, and pass differences
  • [ ] I can explain and use the else clause with loops
  • [ ] I can trace loop execution and predict outputs
  • [ ] I can write programs for factorial, Fibonacci, prime check, and digit sum
  • [ ] I always add colons and proper indentation in Python blocks

Frequently Asked Questions

A for loop iterates over a sequence or range with a known number of iterations, while a while loop repeats as long as a condition remains True and is used when the number of iterations is not known in advance.
← Browse all Class 12 Exercise Solutions

Other question types — same chapter