KwickAcademy Course Topics · 7 min · free
Nested statements; counts and totals
Nesting puts one statement inside another; totalling adds each value (total = total + value), counting adds 1. Start both at 0 before the loop and output after it.
Follows the syllabus of: Cambridge IGCSE Grade 9 Computer Science (0478)
On screen in this lesson
What is nesting?
| Nesting: one statement placed inside another |
| Selection inside selection: an IF inside an IF |
| Selection inside iteration: an IF inside a loop |
| Iteration inside iteration: a loop inside a loop |
Totalling
| Total: the sum of many values |
| Set total to 0 BEFORE the loop |
| Inside the loop: total = total + value |
| Output the total AFTER the loop |
Counting
| Count: how many times something happens |
| Set count to 0 BEFORE the loop |
| Add 1 each time: count = count + 1 |
| Often nested inside an IF |
Pause and predict
| Change 33 in the list to 40 |
| Is 40 >= 40 True or False? |
| Answer: True, so the count becomes 4 |
Counting or totalling?
| Question | Use | Line inside loop |
|---|---|---|
| Runs in a season | Totalling | total + runs |
| Matches played | Counting | count + 1 |
| Fifties scored | Count in IF | count + 1 |
| Bill amount | Totalling | total + price |
Common mistakes
| Forgetting to set total or count to 0 |
| Setting total = 0 inside the loop |
| Printing the total inside the loop |
| Adding the value when you should add 1 |
Quick answers
marks = [55, 30, 75, 33, 90]. If 33 becomes 40, what is the pass count?
4, because 40 >= 40 is True.
Where should total = 0 go?
Before the loop, not inside it.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
The full lesson, in text
Hello students, welcome to Kwickprep. A cricket scorer adds every run and counts every boundary. How does a program do the same job? Today we will learn nested statements, and then two exam favourites: counting and totalling.
First, a new word. Nesting means placing one statement inside another statement, like a small box inside a big box. Selection means choosing a path with if, and we can put one if inside another if. Iteration means repeating with a loop, and we can put an if inside a loop. We can also put one loop inside another loop.
Here is a school promotion rule written in Python. The outer if asks, are the marks forty or more? Only when that answer is yes, the inner if asks about attendance. Marks are seventy two and attendance is eighty percent, so both answers are yes. The program prints Promoted. Notice the inner block is indented two levels.
In exam pseudocode, the same idea is written this way. The inner if sits completely inside the then part of the outer if. Each if must have its own end if. Indenting the inner lines makes the nesting easy for the examiner to read.
Now an if nested inside a loop. Scores is a list, which means many values stored under one name. The for loop picks each score in turn and calls it the variable s. For every score, the if checks, is the variable s forty or more? Only forty five and eighty pass, so only they are printed.
Here, one loop is nested inside another. Range of one comma three gives the numbers one and two. The outer loop sets the variable r to one. Then the inner loop runs completely, with the variable c as one and then two. Only after that does the variable r become two, and the inner loop runs again. So the inner loop runs four times in total.
Now the second big idea, totalling. Totalling means adding up many values to get one sum, like a canteen bill. First, we set the total to zero before the loop starts. Inside the loop, we add each new value to the total. After the loop ends, we output the final total.
A student buys a samosa for fifteen rupees, tea for ten and a sandwich for twenty. Total starts at zero. The loop adds fifteen, then ten, then twenty. So the total goes zero, fifteen, twenty five, forty five. The print is outside the loop, so it prints forty five only once.
Exams often ask for totalling in pseudocode. The arrow means store, so Total arrow zero stores zero in Total. The loop runs five times and inputs five marks. Each mark is added to Total. After the line next Count, the total of all five marks is output.
Counting is different from totalling. Counting finds how many times something happens, like how many students passed. Again, we set count to zero before the loop. Each time the event happens, we add one, not the value. Counting is often placed inside an if, so we count only the values we want.
This program counts how many students passed. The pass mark is forty. Fifty five, seventy five and ninety are forty or more, so count goes up three times. Thirty and thirty three are skipped. The output is three.
Pause and predict. Suppose we change thirty three in the list to exactly forty. Is forty greater than or equal to forty? Yes, it is True, so this student is counted, and the output becomes four.
Totalling and counting often work together. Here are a batter's runs in three matches: twenty four, sixty and twelve. The total becomes ninety six and the count becomes three. The average is the total divided by the count. Ninety six divided by three is thirty two, and Python shows it as thirty two point zero.
Let us decide which one each question needs. Runs in a season needs totalling, because we add the runs. Matches played needs counting, because we add one per match. Fifties scored needs counting inside an if, because we count only scores of fifty or more. A bill amount needs totalling, because we add every price.
Examiners see the same mistakes every year. Some students forget to start the total or count at zero. Others set total to zero inside the loop, so it resets every time. Printing inside the loop shows many running totals instead of one answer. And a counter must add one, not the value itself.
Let us revise what we learned today. Nesting means placing one statement inside another. We can nest an if inside an if, an if inside a loop, or a loop inside a loop. Totalling adds each value to a running total. Counting adds one each time something happens. Start both at zero before the loop, and output the answer after the loop.
Courses that teach this
| Course | Unit |
|---|---|
| Cambridge IGCSE Grade 9 Computer Science (0478) | 8. Programming |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.



