Class 10 Computer Science Chapter 4 Complete Guide Book — Control Structures in Python | PunjabTest.com

Class 10 Computer Science Chapter 4 — Control Structures in Python
Home > PunjabTest.com > Class 10 > Computer Science — all chapters > Chapter 4: Control Structures in Python

اس chapter میں topics (Topics in this chapter)

Source note: This chapter is built from the existing PunjabTest.com library chapter for Class 10 Computer Science. No official book was available on disk for this build, so no book page numbers are claimed, and the exercise below is labelled SLO / board-pattern, as in the library chapter — it is not presented as the book's official exercise.

ماخذ نوٹ: یہ باب کلاس دہم کمپیوٹر سائنس کے موجودہ لائبریری باب سے بنایا گیا ہے۔ اس بلڈ کے لیے سرکاری کتاب ڈسک پر موجود نہیں تھی، اس لیے کتاب کے صفحہ نمبرز کا دعویٰ نہیں کیا گیا اور نیچے دی گئی مشق ایس ایل او / بورڈ پیٹرن کے طور پر لیبل کی گئی ہے — یہ کتاب کی سرکاری ایکسرسائز نہیں ہے۔

4.1 Three Control Structuresتین کنٹرول اسٹرکچرز
🏷 Tags: Three Control Structures, Control structures
ENGLISH

Normally a program runs top to bottom, one statement after another — this is sequence. Control structures change that flow:

1. Sequence — steps run in order. 2. Selection — the program chooses between paths using a condition (if). 3. Repetition (loop) — a block repeats while a condition holds (for, while).

Every program, from a marks calculator to a game, is built from these three structures combined. Python shows a block by indentation (spaces at the start of lines) — there are no { } braces. Wrong indentation is a real error in Python, so keep blocks lined up evenly (usually 4 spaces).

اردو

عام طور پر پروگرام اوپر سے نیچے، ایک کے بعد ایک بیان چلتا ہے — یہ سیکوینس ہے۔ کنٹرول اسٹرکچرز اس بہاؤ کو بدلتے ہیں:

1۔ سیکوینس — مراحل ترتیب سے چلتے ہیں۔ 2۔ سلیکشن — پروگرام شرط کی بنیاد پر راستوں میں سے انتخاب کرتا ہے (if)۔ 3۔ ریپیٹیشن (لوپ) — کوئی بلاک شرط قائم رہنے تک دہراتا ہے (for، while)۔

ہر پروگرام، نمبر کیلکولیٹر سے گیم تک، انہی تین اسٹرکچرز کے امتزاج سے بنتا ہے۔ پائتھون بلاک کو انڈینٹیشن (لائنوں کے شروع میں خالی جگہ) سے ظاہر کرتا ہے — اس میں { } بریکٹس نہیں ہوتے۔ غلط انڈینٹیشن پائتھون میں حقیقی ایرر ہے، اس لیے بلاکس کو برابر (عام طور پر 4 اسپیس) رکھیں۔

SEQUENCE: step → step → step▼SELECTION: if condition → path A / path B▼REPETITION: repeat while condition true
Teen control structures se samjho: Sequence, selection, repetition — the three structures

سیکوینس، سلیکشن، ریپیٹیشن — تین اسٹرکچرز
4.2 Selection: if, elif, elseسلیکشن: if، elif، else
🏷 Tags: Selection: if, elif, else
ENGLISH
marks = int(input("Marks: "))
if marks >= 80:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Work harder")

if tests a condition; if it is True, its block runs. elif (else-if) tests another condition only when the earlier ones failed. else catches everything left over. Only one block runs — Python stops at the first true condition.

Nested if: an if inside another if tests a second condition only when the first passed (e.g. first check pass/fail, then inside, check grade). Conditions use comparison operators (== < > >=) and logical operators (and or not): if age >= 18 and has_card:.

اردو
marks = int(input("Marks: "))
if marks >= 80:
    print("Grade A")
elif marks >= 60:
    print("Grade B")
else:
    print("Work harder")

if شرط کو جانچتا ہے؛ اگر وہ True ہو تو اس کا بلاک چلتا ہے۔ elif (else-if) دوسری شرط صرف تب جانچتا ہے جب پہلی ناکام ہو گئی ہوں۔ else باقی سب کو پکڑتا ہے۔ صرف ایک بلاک چلتا ہے — پائتھون پہلی سچی شرط پر رک جاتا ہے۔

نیسٹڈ if: ایک if کے اندر دوسرا if دوسری شرط صرف تب جانچتا ہے جب پہلی پاس ہو گئی ہو (مثلاً پہلے پاس/فیل چیک کریں، پھر اندر گریڈ چیک کریں)۔ شرطوں میں موازنہ آپریٹرز (== < > >=) اور منطقی آپریٹرز (and or not) استعمال ہوتے ہیں: if age >= 18 and has_card:۔

CONDITION 1 true? → Block A▼else CONDITION 2 true? → Block B▼else → Block C (catch-all)
if-elif-else se samjho: if → elif → else: only one block runs

if → elif → else: صرف ایک بلاک چلتا ہے
4.3 The for Loop and range()for لوپ اور range()
🏷 Tags: The for Loop and range()
ENGLISH

A for loop repeats a block a known number of times — usually over a sequence or a range():

for i in range(1, 6):
    print(i)   # prints 1 2 3 4 5

range(1, 6) gives 1, 2, 3, 4, 5 — the stop value is never included. range(5) gives 0–4; range(2, 11, 2) gives 2, 4, 6, 8, 10 (step 2). A for loop also walks through strings and lists: for ch in "Ali": visits A, l, i one by one.

Accumulator pattern (very important for exams): start a total at 0 before the loop, add inside it, use it after it:

total = 0
for i in range(1, 11):
    total += i
print(total)   # 55
اردو

for لوپ کسی بلاک کو معلوم تعداد میں دہراتا ہے — عام طور پر کسی ترتیب یا range() پر:

for i in range(1, 6):
    print(i)   # prints 1 2 3 4 5

range(1, 6) دیتا ہے 1، 2، 3، 4، 5 — اسٹاپ ویلیو کبھی شامل نہیں ہوتی۔ range(5) دیتا ہے 0–4؛ range(2, 11, 2) دیتا ہے 2، 4، 6، 8، 10 (اسٹپ 2)۔ for لوپ اسٹرنگز اور لسٹس پر بھی چلتا ہے: for ch in "Ali": ایک ایک کر کے A، l، i پر جاتا ہے۔

ایکیومیولیٹر پیٹرن (امتحانات کے لیے بہت اہم): لوپ سے پہلے ٹوٹل 0 سے شروع کریں، اندر جمع کریں، بعد میں استعمال کریں:

total = 0
for i in range(1, 11):
    total += i
print(total)   # 55
START: i = first value▼RUN block for this i▼MOVE to next value▼STOP after last value
for loop se samjho: for loop: known number of repeats

for لوپ: معلوم تعداد میں دہراؤ
4.4 The while Loopwhile لوپ
🏷 Tags: The while Loop
ENGLISH

A while loop repeats while a condition stays true — use it when you do not know in advance how many repeats are needed:

num = 1
while num <= 5:
    print(num)
    num += 1   # without this line the loop never ends!

The loop variable must change inside the loop, or the condition stays true forever — an infinite loop that you must stop with Ctrl+C. Common uses: keep asking until the user enters a valid value, keep a game running until the player quits, read data until a stop value (sentinel) appears.

for vs while: use for when the count is known (tables, lists, ranges); use while when repetition depends on a condition (password attempts, menus, games).

اردو

while لوپ اس وقت تک دہراتا ہے جب تک شرط سچی رہے — اسے تب استعمال کریں جب آپ کو پہلے سے معلوم نہ ہو کہ کتنی بار دہرانا ہے:

num = 1
while num <= 5:
    print(num)
    num += 1   # without this line the loop never ends!

لوپ ویری ایبل لوپ کے اندر بدلنا چاہیے، ورنہ شرط ہمیشہ سچی رہے گی — ایک لامتناہی لوپ جسے Ctrl+C سے روکنا پڑے گا۔ عام استعمال: صارف کے درست قدر داخل کرنے تک پوچھتے رہنا، کھلاڑی کے چھوڑنے تک گیم چلانا، اسٹاپ ویلیو آنے تک ڈیٹا پڑھنا۔

for بمقابلہ while: جب گنتی معلوم ہو (ٹیبلز، لسٹس، رینجز) تو for استعمال کریں؛ جب دہراؤ شرط پر منحصر ہو (پاس ورڈ کی کوششیں، مینوز، گیمز) تو while استعمال کریں۔

CHECK condition first▼TRUE → run block, update variable▼CHECK again...▼FALSE → exit loop
while loop se samjho: while: check → run → check again

while: چیک کریں → چلائیں → دوبارہ چیک کریں
4.5 break, continue and Nested Loopsbreak، continue اور نیسٹڈ لوپس
🏷 Tags: break, continue and Nested Loops
ENGLISH

break exits the loop immediately, even if the loop is not finished — e.g. stop searching when the item is found. continue skips the rest of the current round and jumps to the next one — e.g. skip printing when a value is zero:

for i in range(1, 11):
    if i == 5:
        continue   # skips 5
    if i == 8:
        break      # stops at 7
    print(i)

Output: 1 2 3 4 6 7. Nested loops are loops inside loops: the inner loop finishes all its rounds for each single round of the outer loop. They make tables and patterns: outer loop = rows, inner loop = columns. Example: a multiplication table uses an outer loop for the number and an inner loop for 1–10.

اردو

break لوپ کو فوراً ختم کرتا ہے، چاہے لوپ مکمل نہ ہوا ہو — مثلاً چیز ملنے پر تلاش روک دینا۔ continue موجودہ چکر کا باقی حصہ چھوڑ کر اگلے چکر پر چلا جاتا ہے — مثلاً قدر صفر ہو تو پرنٹ کرنا چھوڑ دینا:

for i in range(1, 11):
    if i == 5:
        continue   # skips 5
    if i == 8:
        break      # stops at 7
    print(i)

آؤٹ پٹ: 1 2 3 4 6 7۔ نیسٹڈ لوپس لوپس کے اندر لوپس ہیں: اندرونی لوپ بیرونی لوپ کے ہر ایک چکر کے لیے اپنے تمام چکر مکمل کرتا ہے۔ یہ ٹیبلز اور پیٹرنز بناتے ہیں: بیرونی لوپ = قطاریں، اندرونی لوپ = کالم۔ مثال: ضرب کا ٹیبل بیرونی لوپ میں عدد اور اندرونی لوپ میں 1–10 استعمال کرتا ہے۔

LOOP round starts▼continue → skip to NEXT round▼break → EXIT loop at once▼normal → finish round, next round
break aur continue se samjho: break exits; continue skips one round

break باہر نکلتا ہے؛ continue ایک چکر چھوڑتا ہے
4.6 Practice Programs and Debuggingپریکٹس پروگرامز اور ڈیبگنگ
🏷 Tags: Practice Programs and Debugging
ENGLISH

Three exam-favourite programs: (1) Table: for i in range(1, 11): print(n, "x", i, "=", n*i). (2) Even/odd count: loop over numbers, use % 2 to test, count each kind. (3) Largest of three: nested if comparing a, b, c.

Dry run means tracing a program on paper: write each variable's value round by round. It catches logic errors before you run the code — examiners love dry-run questions, e.g. “what is the value of total after this loop?”

Debugging checklist: (1) Read the error message — it names the line. (2) Check indentation first. (3) Check = vs == and missing colons : after if/for/while. (4) Print variable values inside the loop to see what is happening. (5) Test with small, known inputs (e.g. table of 2) before big ones.

اردو

تین امتحانی پسندیدہ پروگرامز: (1) ٹیبل: for i in range(1, 11): print(n, "x", i, "=", n*i)۔ (2) جفت/طاق گنتی: اعداد پر لوپ، جانچ کے لیے % 2 استعمال کریں، ہر قسم گنیں۔ (3) تین میں سب سے بڑا: a، b، c کا موازنہ کرنے والا نیسٹڈ if۔

ڈرائی رن کا مطلب پروگرام کو کاغذ پر ٹریس کرنا ہے: ہر چکر میں ہر ویری ایبل کی قدر لکھیں۔ یہ کوڈ چلانے سے پہلے منطقی غلطیاں پکڑتا ہے — ممتحن ڈرائی رن سوالات پسند کرتے ہیں، مثلاً “اس لوپ کے بعد total کی قدر کیا ہے؟”

ڈیبگنگ چیک لسٹ: (1) ایرر پیغام پڑھیں — یہ لائن بتاتا ہے۔ (2) پہلے انڈینٹیشن چیک کریں۔ (3) = بمقابلہ == اور if/for/while کے بعد غائب کولن : چیک کریں۔ (4) لوپ کے اندر ویری ایبل کی قدریں پرنٹ کر کے دیکھیں کیا ہو رہا ہے۔ (5) بڑے ان پٹس سے پہلے چھوٹے، معلوم ان پٹس (مثلاً 2 کا ٹیبل) سے آزمائیں۔

1. READ the error + line number▼2. CHECK indentation, :, = vs ==▼3. PRINT values inside loop▼4. TEST with small inputs
Debugging se samjho: Debug: read → check → print → test

ڈیبگ: پڑھیں → چیک کریں → پرنٹ کریں → آزمائیں
📝 SLO / Board-Pattern Questions — with Answersایس ایل او / بورڈ پیٹرن سوالات — جوابات کے ساتھ
SLO-BASED — NOT THE OFFICIAL EXERCISE

Book note (please read): This page follows the PCTB book Computer Science and Entrepreneurship, Class 10 (session 2026–27). The chapter titles and section order were verified against current 2026–27 Punjab listings and the board syllabus for this book. The book PDF itself could not be opened page-by-page during this build, so the questions below are SLO / board-pattern questions written for this chapter — they are NOT presented as the book's official exercise. Where a point was not confirmed from the book, it is kept general and no invented fact is stated as a book fact.

ایس ایل او پر مبنی

کتاب کے بارے میں نوٹ: یہ صفحہ پی سی ٹی بی کی کتاب کمپیوٹر سائنس اینڈ انٹرپرینیورشپ، جماعت دہم (سیشن 2026–27) کے مطابق ہے۔ باب کے عنوانات اور ترتیب موجودہ 2026–27 کی پنجاب لسٹنگز اور بورڈ سلیبس سے تصدیق شدہ ہیں۔ کتاب کا پی ڈی ایف اس بلڈ کے دوران صفحہ بہ صفحہ نہیں کھولا جا سکا، اس لیے نیچے دیے گئے سوالات ایس ایل او / بورڈ پیٹرن کے سوالات ہیں — یہ کتاب کی سرکاری ایکسرسائز نہیں ہیں۔

Section 1: Multiple Choice Questionsحصہ 1: کثیر الانتخابی سوالات
1. Which structure chooses between paths using a condition?

Answer: (b) Selection — implemented with if / elif / else.

2. In if-elif-else, how many blocks run?

Answer: (b) Only the first true one — Python stops at the first true condition.

3. range(1, 6) produces:

Answer: (b) 1 2 3 4 5 — the stop value 6 is never included.

4. A while loop is best when:

Answer: (b) Repeats depend on a condition — e.g. keep asking until a valid value is entered.

5. What causes an infinite loop?

Answer: (b) The loop variable never changes — the condition stays true forever.

6. break inside a loop:

Answer: (b) Exits the loop at once; continue skips only the current round.

7. for i in range(2, 11, 2) prints:

Answer: (a) 2 4 6 8 10 — start 2, step 2, stop before 11.

8. Python shows a block by:

Answer: (b) Indentation — consistent spacing (usually 4 spaces); wrong indentation is an error.

Section 2: Short Questionsحصہ 2: مختصر سوالات
1. Name the three control structures and give one Python keyword for each (where it applies).

Answer: Sequence (no keyword — statements in order), Selection (if / elif / else), Repetition (for / while).

2. What does elif do? Why not use a second if instead?

Answer: elif tests another condition only when earlier conditions failed, and only one block runs. A second separate if would be tested independently, so two blocks could both run.

3. Write the output of: for i in range(1, 6): print(i * i).

Answer: 1, 4, 9, 16, 25 — i takes 1, 2, 3, 4, 5 and each square is printed.

4. Differentiate between break and continue with an example.

Answer: break exits the whole loop at once (e.g. stop searching when found). continue skips the rest of the current round and goes to the next round (e.g. skip printing 5).

5. What is a dry run? Why is it useful?

Answer: A dry run is tracing a program on paper, writing each variable's value round by round. It catches logic errors before running the code and answers 'what is the value after this loop?' questions.

Section 3: Long Questionsحصہ 3: تفصیلی سوالات
1. Write a Python program to print the table of a number entered by the user, and explain it.

Answer: n = int(input("Enter number: ")) — reads the number. for i in range(1, 11): — repeats i from 1 to 10. print(n, "x", i, "=", n * i) — prints each line, e.g. for 7: 7 x 1 = 7 ... 7 x 10 = 70. The for loop is right because the count (10) is known.

2. Write a program to find the sum of the first 10 natural numbers using a loop, and dry-run its first three rounds.

Answer: total = 0; for i in range(1, 11): total += i; print(total) → 55. Dry run: start total = 0. Round 1: i = 1, total = 1. Round 2: i = 2, total = 3. Round 3: i = 3, total = 6. ... Round 10: i = 10, total = 55. This is the accumulator pattern: initialise before, add inside, use after.

3. Compare for and while loops and state when each should be used, with one example each.

Answer: for: known number of repeats over a sequence/range — e.g. printing a table (for i in range(1, 11)). while: repeats while a condition holds, count unknown — e.g. keep asking for a password until correct. A while loop needs its variable updated inside, or it becomes infinite; a for loop handles its own counting.

📌 باب کا خلاصہ اور رہنمائی — اس باب میں کیا ہے؟

This chapter teaches control structures in Python: the three structures every program uses, selection with if, elif and else, counted repetition with for and range(), condition-based repetition with while, how break, continue and nested loops behave, and how to practise and debug real programs.

Revision guidance: Revision guidance: trace each loop on paper before running it — write the value of the loop variable each round. That one habit catches infinite loops, off-by-one range() errors and wrong indentation faster than anything else.

یہ باب پائتھون میں کنٹرول اسٹرکچرز سکھاتا ہے: تین اسٹرکچرز جو ہر پروگرام استعمال کرتا ہے، if، elif اور else کے ساتھ سلیکشن، for اور range() کے ساتھ گنتی والی تکرار، while کے ساتھ شرط پر مبنی تکرار، break، continue اور نیسٹڈ لوپس کا رویہ، اور اصلی پروگراموں کی پریکٹس اور ڈیبگنگ۔

نظرثانی کی رہنمائی: نظرثانی کی رہنمائی: ہر لوپ چلانے سے پہلے کاغذ پر trace کرو — ہر چکر میں لوپ variable کی قدر لکھو۔ یہ ایک عادت infinite loops، range() کی off-by-one غلطیوں اور غلط indentation کو سب سے جلد پکڑتی ہے۔

🎯 مزید MCQs اور Tests کے لیے

Chapter-wise tests, fresh random papers اور instant score کے لیے PunjabTest.com کا میں test section دیکھو:

PunjabTest.com — Tests →

PunjabTest.com — Complete Guide Book • Class 10 Computer Science, Chapter 4 (Session 2026–27) • English + Urdu

← Chapter 3Chapter 5 →