© 2026 schoolict.net (Ruwan Mangala Suraweera)
1.8 Nested Control Structuresකැදැලි පාලන ව්යුහ
G.C.E. O/L Grade 11 ICT · Lesson 1G.C.E. O/L ICT · 11 ශ්රේණිය · පාඩම 1
In this videoමේ වීඩියෝවේ
- Nesting: a structure inside another.කැදැලි: ව්යුහයක් තුළ තවත් එකක්.
- Nested IF: largest of three.කැදැලි IF: තුනෙන් විශාලතම.
- Inner loop runs fully for each outer run.පිටත සෑම ධාවනයකටම ඇතුළු ලූපය සම්පූර්ණයෙන් ධාවනය වේ.
- IF inside a loop: count passes.ලූපය තුළ IF: සමත් ගණන් කිරීම.
- Indent each level; close inner first.සෑම මට්ටමක්ම ඉන්ඩෙන්ට් කරන්න; ඇතුළත මුලින් වසන්න.
Notesසටහන්
Syllabus 10.8. A structure placed inside another structure is nested – like a small box inside a bigger box. We can put a selection inside a selection, a loop inside a loop, a selection inside a loop, or a loop inside a selection. Real programs use nesting all the time.විෂය නිර්දේශය 10.8. වෙනත් ව්යුහයක් තුළ තැබූ ව්යුහයක් කැදැලි (nested) වේ – විශාල පෙට්ටියක් තුළ ඇති කුඩා පෙට්ටියක් මෙන්. වරණයක් තුළ වරණයක්, ලූපයක් තුළ ලූපයක්, ලූපයක් තුළ වරණයක් හෝ වරණයක් තුළ ලූපයක් තැබිය හැක. සැබෑ වැඩසටහන් නිතරම කැදැලි භාවිත කරයි.
1. Selection inside selection1. වරණයක් තුළ වරණයක්
To find the largest of three numbers A, B and C: first ask “Is A > B?”. Then, inside each answer, ask a second question with C. The inner IF runs only when the outer IF reaches it.A, B සහ C සංඛ්යා තුනෙන් විශාලතම එක සොයා ගැනීමට: මුලින් “A > B ද?” යැයි අසන්න. පසුව එක් එක් පිළිතුර තුළ C සමඟ දෙවන ප්රශ්නයක් අසන්න. ඇතුළු IF එක ධාවනය වන්නේ පිටත IF එක එයට ළඟා වූ විට පමණි.
📝 Pseudocodeව්යාජ කේතය (Pseudocode)
BEGIN INPUT A, B, C IF A > B THEN IF A > C THEN Largest = A ELSE Largest = C ENDIF ELSE IF B > C THEN Largest = B ELSE Largest = C ENDIF ENDIF DISPLAY Largest END
program Largest3; var a, b, c, largest: integer; begin write('Enter A: '); readln(a); write('Enter B: '); readln(b); write('Enter C: '); readln(c); if a > b then begin if a > c then largest := a else largest := c; end else begin if b > c then largest := b else largest := c; end; writeln('Largest = ', largest); end.
a = int(input("Enter A: ")) b = int(input("Enter B: ")) c = int(input("Enter C: ")) if a > b: if a > c: largest = a else: largest = c else: if b > c: largest = b else: largest = c print("Largest =", largest)
▶ Sample runනියැදි ධාවනය 1 · same output in Pascal and Python (typed input in yellow)Pascal සහ Python දෙකෙහිම එකම ප්රතිදානය (ටයිප් කළ ආදානය කහ පාටින්)
Enter A: 12 Enter B: 45 Enter C: 30 Largest = 45
▶ Sample runනියැදි ධාවනය 2 · same output in Pascal and Python (typed input in yellow)Pascal සහ Python දෙකෙහිම එකම ප්රතිදානය (ටයිප් කළ ආදානය කහ පාටින්)
Enter A: 50 Enter B: 20 Enter C: 35 Largest = 50
2. Iteration inside iteration2. ලූපයක් තුළ ලූපයක්
An inner loop runs completely for every single run of the outer loop. In the star triangle, the outer loop chooses the row; the inner loop prints that many stars.පිටත ලූපයේ සෑම ධාවනයකටම ඇතුළු ලූපය සම්පූර්ණයෙන් ධාවනය වේ. තරු ත්රිකෝණයේ පිටත ලූපය පේළිය තෝරයි; ඇතුළු ලූපය එම ගණනට තරු මුද්රණය කරයි.
📝 Pseudocodeව්යාජ කේතය (Pseudocode)
BEGIN FOR Row = 1 TO 4 FOR Col = 1 TO Row DISPLAY "*" (same line) ENDFOR Move to a new line ENDFOR END
program Stars; var row, col: integer; begin for row := 1 to 4 do begin for col := 1 to row do write('*'); writeln; end; end.
for row in range(1, 5): for col in range(1, row + 1): print("*", end="") print()
▶ Sample runනියැදි ධාවනය · same output in Pascal and PythonPascal සහ Python දෙකෙහිම එකම ප්රතිදානය
* ** *** ****
| RowRow | Inner loop runs (times)ඇතුළු ලූපය ධාවනය (වාර) | Printedමුද්රණය වූ දේ |
|---|---|---|
| 11 | 11 | ** |
| 22 | 22 | **** |
| 33 | 33 | ****** |
| 44 | 44 | ******** |
3. Selection inside iteration3. ලූපයක් තුළ වරණයක්
A loop reads 5 marks. Inside the loop, an IF decides whether each mark is a pass (≥ 35) or a fail, and counts them.ලූපයක් ලකුණු 5ක් කියවයි. ලූපය තුළ, IF එකක් එක් එක් ලකුණ සමත්ද (≥ 35) අසමත්ද යන්න තීරණය කර ඒවා ගණන් කරයි.
📝 Pseudocodeව්යාජ කේතය (Pseudocode)
BEGIN Passes = 0 Fails = 0 FOR i = 1 TO 5 INPUT Mark IF Mark >= 35 THEN Passes = Passes + 1 ELSE Fails = Fails + 1 ENDIF ENDFOR DISPLAY Passes, Fails END
program PassCount; var i, mark, passes, fails: integer; begin passes := 0; fails := 0; for i := 1 to 5 do begin write('Enter mark ', i, ': '); readln(mark); if mark >= 35 then passes := passes + 1 else fails := fails + 1; end; writeln('Pass = ', passes); writeln('Fail = ', fails); end.
passes = 0 fails = 0 for i in range(1, 6): mark = int(input(f"Enter mark {i}: ")) if mark >= 35: passes = passes + 1 else: fails = fails + 1 print("Pass =", passes) print("Fail =", fails)
▶ Sample runනියැදි ධාවනය · same output in Pascal and Python (typed input in yellow)Pascal සහ Python දෙකෙහිම එකම ප්රතිදානය (ටයිප් කළ ආදානය කහ පාටින්)
Enter mark 1: 45 Enter mark 2: 30 Enter mark 3: 78 Enter mark 4: 22 Enter mark 5: 60 Pass = 3 Fail = 2
4. Iteration inside selection4. වරණයක් තුළ ලූපයක්
First check that the input is positive. Only if it is, run a loop that displays the squares 1², 2², …මුලින් ආදානය ධනදැයි පරීක්ෂා කරන්න. එසේ නම් පමණක්, 1², 2², … වර්ග පෙන්වන ලූපයක් ධාවනය කරන්න.
📝 Pseudocodeව්යාජ කේතය (Pseudocode)
BEGIN INPUT N IF N > 0 THEN FOR i = 1 TO N DISPLAY i * i ENDFOR ELSE DISPLAY "Please enter a positive number" ENDIF END
program Squares; var n, i: integer; begin write('How many squares? '); readln(n); if n > 0 then begin for i := 1 to n do writeln('Square of ', i, ' = ', i * i); end else writeln('Please enter a positive number'); end.
n = int(input("How many squares? ")) if n > 0: for i in range(1, n + 1): print("Square of", i, "=", i * i) else: print("Please enter a positive number")
▶ Sample runනියැදි ධාවනය 1 · same output in Pascal and Python (typed input in yellow)Pascal සහ Python දෙකෙහිම එකම ප්රතිදානය (ටයිප් කළ ආදානය කහ පාටින්)
How many squares? 4 Square of 1 = 1 Square of 2 = 4 Square of 3 = 9 Square of 4 = 16
▶ Sample runනියැදි ධාවනය 2 · same output in Pascal and Python (typed input in yellow)Pascal සහ Python දෙකෙහිම එකම ප්රතිදානය (ටයිප් කළ ආදානය කහ පාටින්)
How many squares? -2 Please enter a positive number
5. Tips for nested code5. කැදැලි කේත සඳහා උපදෙස්
- Indent each inner level a little more, so you can see which statement belongs where.සෑම ඇතුළු මට්ටමක්ම තව ටිකක් ඉන්ඩෙන්ට් කරන්න, එවිට කුමන ප්රකාශය කොතැනට අයත්දැයි පෙනේ.
- Close the inner structure before the outer one (ENDIF before ENDFOR).පිටත එකට පෙර ඇතුළු ව්යුහය වසන්න (ENDFOR ට පෙර ENDIF).
- In Pascal, use begin … end when a part has more than one statement.Pascal හි කොටසක ප්රකාශ එකකට වඩා ඇති විට begin … end භාවිත කරන්න.
- Use different counter names for nested loops (Row and Col, i and j).කැදැලි ලූප සඳහා වෙනස් ගණක නම් භාවිත කරන්න (Row සහ Col, i සහ j).
Key Wordsප්රධාන පද
Sample Q&Aආදර්ශ ප්රශ්න සහ පිළිතුරු
1What is a nested structure?කැදැලි ව්යුහයක් යනු කුමක්ද?
2For A = 12, B = 45, C = 30, what does the largest-of-three program display?A = 12, B = 45, C = 30 සඳහා විශාලතම-තුනෙන් වැඩසටහන පෙන්වන්නේ කුමක්ද?
3If the outer loop runs 3 times and the inner loop runs 4 times each time, how many times does the inner body run?පිටත ලූපය 3 වරක් සහ ඇතුළු ලූපය සෑම විටම 4 වරක් ධාවනය වේ නම් ඇතුළත කී වරක් ධාවනය වේද?
4Marks 45, 30, 78, 22, 60 – how many passes and fails?ලකුණු 45, 30, 78, 22, 60 – සමත් සහ අසමත් කීයද?
5What does the squares program show for −2?squares වැඩසටහන −2 සඳහා පෙන්වන්නේ කුමක්ද?
6Why do we indent nested code?කැදැලි කේත ඉන්ඩෙන්ට් කරන්නේ ඇයි?
MCQබහුවරණ ප්රශ්න
Choose the correct answer for each question.සෑම ප්රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.
1A loop placed inside another loop is a…වෙනත් ලූපයක් තුළ තැබූ ලූපයක්…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Nested.කැදැලි.
2For A = 50, B = 20, C = 35, the largest is…A = 50, B = 20, C = 35 සඳහා විශාලතම…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
A is biggest.A විශාලතමයි.
3In the largest-of-three flowchart, the first question is…විශාලතම-තුනෙන් ගැලීම් සටහනේ පළමු ප්රශ්නය…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
Outer IF.පිටත IF.
4Outer loop runs 4 times, inner loop 3 times each. Inner body runs…පිටත ලූපය 4 වරක්, ඇතුළු ලූපය සෑම විටම 3 වරක්. ඇතුළත ධාවනය වන්නේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
4 × 3.4 × 3.
5The last line of the star triangle (Row = 4) is…තරු ත්රිකෝණයේ අවසාන පේළිය (Row = 4)…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
4 stars.තරු 4.
6Total stars in the 4-row triangle…පේළි 4ක ත්රිකෝණයේ මුළු තරු…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
1+2+3+4.1+2+3+4.
7In the star program, the inner loop runs while…තරු වැඩසටහනේ ඇතුළු ලූපය ධාවනය වන්නේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Inner condition.ඇතුළු කොන්දේසිය.
8Marks 45, 30, 78, 22, 60 give Pass = …ලකුණු 45, 30, 78, 22, 60 අනුව Pass = …
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
45, 78, 60.45, 78, 60.
9Counting passes inside a loop is an example of…ලූපයක් තුළ සමත් ගණන් කිරීම … සඳහා උදාහරණයකි
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
IF inside FOR.FOR තුළ IF.
10The squares program checks the number first, then loops. This is…squares වැඩසටහන මුලින් සංඛ්යාව පරීක්ෂා කර පසුව ලූප කරයි. මෙය…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D
FOR inside IF.IF තුළ FOR.
11For input 4, the last line of the squares program is…ආදානය 4 සඳහා squares වැඩසටහනේ අවසාන පේළිය…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
4 × 4.4 × 4.
12Which should be closed first?මුලින්ම වැසිය යුත්තේ කුමක්ද?
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Inner first.ඇතුළත මුලින්.
13In Pascal, several statements inside an IF must be grouped with…Pascal හි IF එකක් තුළ ප්රකාශ කිහිපයක් කාණ්ඩ කළ යුත්තේ…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B
begin … end.begin … end.
14Good names for two nested loop counters are…කැදැලි ලූප ගණක දෙකකට හොඳ නම්…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C
Different names.වෙනස් නම්.
15Nesting means…කැදැලි කිරීම යනු…
✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A
Definition.අර්ථය.
MCQ answer keyMCQ පිළිතුරු
- C – Nested.කැදැලි.
- A – A is biggest.A විශාලතමයි.
- D – Outer IF.පිටත IF.
- B – 4 × 3.4 × 3.
- B – 4 stars.තරු 4.
- A – 1+2+3+4.1+2+3+4.
- C – Inner condition.ඇතුළු කොන්දේසිය.
- D – 45, 78, 60.45, 78, 60.
- A – IF inside FOR.FOR තුළ IF.
- D – FOR inside IF.IF තුළ FOR.
- B – 4 × 4.4 × 4.
- C – Inner first.ඇතුළත මුලින්.
- B – begin … end.begin … end.
- C – Different names.වෙනස් නම්.
- A – Definition.අර්ථය.
Structured Questionsව්යුහගත ප්රශ්න
1Draw a flowchart to input three numbers A, B and C and display the largest, using nested IFs. (5 marks)කැදැලි IF භාවිතයෙන් A, B සහ C සංඛ්යා තුනක් ආදානය කර විශාලතම එක පෙන්වන ගැලීම් සටහනක් අඳින්න. (ලකුණු 5) (5 marksලකුණු 5)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
2Write a program (Pascal or Python) that uses nested loops to print the triangle *, , *, ****. (4 marks)*, , *, **** ත්රිකෝණය මුද්රණය කිරීමට කැදැලි ලූප භාවිත කරන වැඩසටහනක් (Pascal හෝ Python) ලියන්න. (ලකුණු 4) (4 marksලකුණු 4)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
program Stars; var row, col: integer; begin for row := 1 to 4 do begin for col := 1 to row do write('*'); writeln; end; end.
for row in range(1, 5): for col in range(1, row + 1): print("*", end="") print()
▶ Sample runනියැදි ධාවනය · same output in Pascal and PythonPascal සහ Python දෙකෙහිම එකම ප්රතිදානය
* ** *** ****
3Write pseudocode to read 5 marks and count how many are passes (≥ 35) and how many are fails. (5 marks)ලකුණු 5ක් කියවා සමත් (≥ 35) කීයද අසමත් කීයද යන්න ගණන් කරන ව්යාජ කේතය ලියන්න. (ලකුණු 5) (5 marksලකුණු 5)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
📝 Pseudocodeව්යාජ කේතය (Pseudocode)
BEGIN Passes = 0 Fails = 0 FOR i = 1 TO 5 INPUT Mark IF Mark >= 35 THEN Passes = Passes + 1 ELSE Fails = Fails + 1 ENDIF ENDFOR DISPLAY Passes, Fails END
4Name the four kinds of nesting and give an example of each. (4 marks)කැදැලි කිරීමේ වර්ග හතර නම් කර එක් එකකට උදාහරණයක් දෙන්න. (ලකුණු 4) (4 marksලකුණු 4)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
5Complete a trace table for the star program (4 rows) showing Row, how many times the inner loop runs, and what is printed. (3 marks)තරු වැඩසටහන (පේළි 4) සඳහා Row, ඇතුළු ලූපය ධාවනය වන වාර ගණන සහ මුද්රණය වන දේ පෙන්වන අනුරේඛන වගුවක් සම්පූර්ණ කරන්න. (ලකුණු 3) (3 marksලකුණු 3)
Show model answerආදර්ශ පිළිතුර පෙන්වන්න
| RowRow | Inner loop runs (times)ඇතුළු ලූපය ධාවනය (වාර) | Printedමුද්රණය වූ දේ |
|---|---|---|
| 11 | 11 | ** |
| 22 | 22 | **** |
| 33 | 33 | ****** |
| 44 | 44 | ******** |
🎮 Game🎮 ක්රීඩා
Summary & Downloadsසාරාංශය සහ බාගත කිරීම්
Key pointsප්රධාන කරුණු
- Nesting = a structure inside another.කැදැලි = ව්යුහයක් තුළ තවත් එකක්.
- Nested IF: largest of three.කැදැලි IF: තුනෙන් විශාලතම.
- Nested loop: inner loop runs fully each time.කැදැලි ලූපය: ඇතුළු ලූපය සෑම විටම සම්පූර්ණයෙන් ධාවනය වේ.
- Inner runs = outer × inner (fixed sizes).ඇතුළත ධාවන = පිටත × ඇතුළු (ස්ථාවර ප්රමාණ).
- IF inside a loop: count passes.ලූපයක් තුළ IF: සමත් ගණන් කිරීම.
- Loop inside an IF: run only when valid.IF එකක් තුළ ලූපය: වලංගු නම් පමණක් ධාවනය.
- Indent every level; close inner first.සෑම මට්ටමක්ම ඉන්ඩෙන්ට් කරන්න; ඇතුළත මුලින් වසන්න.
- Pascal: begin … end groups statements.Pascal: begin … end ප්රකාශ කාණ්ඩ කරයි.
