
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.
ماخذ نوٹ: یہ باب کلاس دہم کمپیوٹر سائنس کے موجودہ لائبریری باب سے بنایا گیا ہے۔ اس بلڈ کے لیے سرکاری کتاب ڈسک پر موجود نہیں تھی، اس لیے کتاب کے صفحہ نمبرز کا دعویٰ نہیں کیا گیا اور نیچے دی گئی مشق ایس ایل او / بورڈ پیٹرن کے طور پر لیبل کی گئی ہے — یہ کتاب کی سرکاری ایکسرسائز نہیں ہے۔
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 اسپیس) رکھیں۔
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:۔
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) # 55for لوپ کسی بلاک کو معلوم تعداد میں دہراتا ہے — عام طور پر کسی ترتیب یا 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) # 55A 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 استعمال کریں۔
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 استعمال کرتا ہے۔
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 کا ٹیبل) سے آزمائیں۔
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 کی پنجاب لسٹنگز اور بورڈ سلیبس سے تصدیق شدہ ہیں۔ کتاب کا پی ڈی ایف اس بلڈ کے دوران صفحہ بہ صفحہ نہیں کھولا جا سکا، اس لیے نیچے دیے گئے سوالات ایس ایل او / بورڈ پیٹرن کے سوالات ہیں — یہ کتاب کی سرکاری ایکسرسائز نہیں ہیں۔
Answer: (b) Selection — implemented with if / elif / else.
Answer: (b) Only the first true one — Python stops at the first true condition.
Answer: (b) 1 2 3 4 5 — the stop value 6 is never included.
Answer: (b) Repeats depend on a condition — e.g. keep asking until a valid value is entered.
Answer: (b) The loop variable never changes — the condition stays true forever.
Answer: (b) Exits the loop at once; continue skips only the current round.
Answer: (a) 2 4 6 8 10 — start 2, step 2, stop before 11.
Answer: (b) Indentation — consistent spacing (usually 4 spaces); wrong indentation is an error.
Answer: Sequence (no keyword — statements in order), Selection (if / elif / else), Repetition (for / while).
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.
Answer: 1, 4, 9, 16, 25 — i takes 1, 2, 3, 4, 5 and each square is printed.
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).
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.
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.
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.
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 کو سب سے جلد پکڑتی ہے۔
Chapter-wise tests, fresh random papers اور instant score کے لیے PunjabTest.com کا میں test section دیکھو:
PunjabTest.com — Complete Guide Book • Class 10 Computer Science, Chapter 4 (Session 2026–27) • English + Urdu