Digital Tech Notes (JSS1 - SS3)

Loops In Programming

Learn all about loops in programming — what they mean, types like for loop and while loop, and how they are used in real-life coding. Perfect for JSS


 

INTRODUCTION:

Imagine you are at a school assembly in Lagos, and the principal asks every student to call out their name one by one. Now imagine doing that manually in a computer program — writing a separate line of code for each of the 500 students in the school. That would take forever!

This is exactly the kind of problem that loops in programming were designed to solve. Instead of repeating the same instruction over and over, a loop allows a computer to do it automatically — as many times as you need.

In today's world, where technology is driving everything from mobile banking with apps like Opay and Kuda, to online registration portals like JAMB and WAEC, loops are one of the most important tools programmers use. Every time you scroll through a list of results, receive an automated OTP message, or watch your phone display a countdown timer, there is likely a loop working quietly behind the scenes.

Let us dive in!


LEARNING OBJECTIVES

By the end of this lesson, students should be able to:

  1. Define a loop in the context of computer programming
  2. Explain why loops are important in writing efficient programs
  3. Identify the main types of loops used in programming (for loop and while loop)
  4. Describe the syntax and structure of a for loop and a while loop
  5. Demonstrate how loops can be applied in solving simple real-life problems
  6. Distinguish between situations where each type of loop is most appropriate

WHAT IS A LOOP IN PROGRAMMING?

A loop is a programming instruction that tells a computer to repeat a set of actions a certain number of times or until a specific condition is met.

Think of it this way: when you are pounding yam, you do not lift the pestle just once. You keep lifting and pounding repeatedly until the yam is smooth. That repetitive action is exactly what a loop does in programming — it repeats a task until the job is done.

In simple terms, a loop saves you from writing the same code over and over again. Instead of writing:

Print "Good morning" Print "Good morning" Print "Good morning" ... (50 times)

You can simply write a loop that says: Print "Good morning" 50 times. Done!

WHY ARE LOOPS IMPORTANT?

Loops are important because:

  • They save time and reduce the amount of code a programmer has to write
  • They make programs shorter, cleaner, and easier to read
  • They allow computers to process large amounts of data quickly
  • They are used in almost every real-world software application
  • They help automate repetitive tasks efficiently

Without loops, programming would be extremely tedious, and many of the apps and systems we use today simply would not work the way they do.


TYPES OF LOOPS IN PROGRAMMING

There are two main types of loops every JSS 2 student should know:

  1. The FOR Loop
  2. The WHILE Loop

Let us look at each one carefully.


THE FOR LOOP

What Is a For Loop?

A for loop is a type of loop that repeats a block of code a specific number of times. You use a for loop when you already know — before the loop starts — exactly how many times you want the action to be repeated.

The word "for" in the loop basically means: "For this many times, do this action."

Real-Life Nigerian Example:

Imagine a teacher at a school in Abuja wants to print the names of all 30 students in a class on report cards. Instead of writing a separate print command for each student, she can use a for loop to go through the list of 30 students automatically.

Another example: A fuel station attendant in Port Harcourt records the fuel pumped for each of 10 vehicles. A for loop can process all 10 records one by one without writing 10 separate instructions.

Structure of a For Loop (Using Pseudocode):

FOR counter = 1 TO 10 

PRINT "Student number " + counter 

ENDFOR

This loop will print "Student number 1", "Student number 2", all the way to "Student number 10" — automatically.

Breaking Down the For Loop:

  • FOR — This keyword starts the loop
  • counter = 1 — This is where the loop begins (start value)
  • TO 10 — This is where the loop ends (end value)
  • PRINT... — This is the action that repeats
  • ENDFOR — This signals the end of the loop

Key Features of a For Loop:

  • You know the exact number of repetitions before it starts
  • It has a clear start point and end point
  • It automatically increases (or decreases) a counter each time it runs
  • It stops on its own when the count is complete

Loop in Scratch Programming

In Scratch programming, loops are blocks that repeat actions to save time, reduce code clutter, and keep characters moving. Found inside the gold-colored Control block category, they act as "C-shaped" containers that wrap around other blocks to make them repeat.




THE WHILE LOOP

What Is a While Loop?

A while loop is a type of loop that keeps repeating a block of code for as long as a certain condition remains true. Unlike the for loop, you may not always know exactly how many times it will run. It just keeps going until the condition becomes false.

The word "while" here means: "While this condition is true, keep doing this action."

Real-Life Nigerian Example:

Think about a generator (gen) running in a house in Enugu. The generator keeps running while there is fuel in the tank. The moment the fuel finishes, it stops. That is exactly how a while loop works — it keeps executing as long as the condition (fuel in tank) is true.

Another example: An ATM machine in a GTBank branch in Lagos keeps asking you to "Enter your PIN" while the PIN you enter is wrong. Once you enter the correct PIN, the condition becomes false, and the loop stops.

Structure of a While Loop (Using Pseudocode):

WHILE fuel > 0 RUN generator fuel = fuel - 1 ENDWHILE

This loop will keep running the generator and reducing fuel by 1 each time, until the fuel reaches 0.

Breaking Down the While Loop:

  • WHILE — This keyword starts the loop
  • fuel > 0 — This is the condition being checked (loop continues while this is true)
  • RUN generator — This is the action that repeats
  • fuel = fuel - 1 — This updates the condition so the loop can eventually stop
  • ENDWHILE — This signals the end of the loop

Key Features of a While Loop:

  • The number of repetitions is not always known in advance
  • The loop depends on a condition being true or false
  • It checks the condition before executing the action each time
  • It must have something inside it that will eventually make the condition false (to avoid an infinite loop)

What Is an Infinite Loop?

An infinite loop is a loop that never stops because the condition never becomes false. This is a common mistake in programming and can cause a program to freeze or crash. For example, if a while loop says "while 1 = 1, keep running", it will never stop because 1 will always equal 1.

Always make sure your while loop has a proper exit condition!


DIFFERENCES BETWEEN FOR LOOP AND WHILE LOOP

FOR LOOP:

  • Used when you know the exact number of repetitions
  • Has a built-in counter
  • Easier to use for counting tasks
  • Example: Print 10 names from a list

WHILE LOOP:

  • Used when the number of repetitions is unknown
  • Depends on a true/false condition
  • More flexible for open-ended tasks
  • Example: Keep asking for a password until the correct one is entered

Both types of loops are useful — the key is knowing which one to use in a given situation.


PRACTICAL APPLICATIONS OF LOOPS IN REAL LIFE

Loops are not just classroom concepts — they are used in almost every digital system we interact with daily. Here are some real-life applications relevant to Nigerian students and society:

1. School Result Processing When schools use software to compute students' grades, a loop goes through each student's score, calculates the average, and assigns a grade — automatically, for every student on the list.

2. Mobile Recharge and Data Systems When you recharge your MTN, Airtel, or Glo line, the telecom system uses loops to scan through thousands of customer accounts to update balances and send confirmation messages.

3. JAMB CBT Examination Portal During JAMB exams, the system loops through all the registered candidates to load their question papers, record their answers, and calculate their scores — all using loops in the background.

4. Automated Market Price Displays Some markets and supermarkets in Nigeria use digital price boards. A loop continuously updates and displays prices for hundreds of products on screen.

5. Games and Animations Popular mobile games like those played on Tecno and Infinix phones use loops to keep the game running — checking player moves, updating scores, and refreshing the screen — all in a continuous loop.

6. Payroll Systems Companies and government agencies use payroll software that loops through every employee's record to calculate monthly salaries, tax deductions, and allowances.


ADVANTAGES OF LOOPS IN PROGRAMMING

  • They reduce the amount of code needed to perform repetitive tasks
  • They make programs faster and more efficient
  • They allow programmers to handle large datasets with ease
  • They make code easier to maintain and update
  • They are flexible and can be used in almost any type of program

DISADVANTAGES OF LOOPS IN PROGRAMMING

  • If not written correctly, a loop can run forever (infinite loop), causing the program to crash
  • Poorly structured loops can slow down a computer if they perform too many operations
  • Beginners sometimes find it confusing to choose between a for loop and a while loop
  • A logic error inside a loop can produce wrong results repeatedly, making it harder to debug

ETHICAL AND SAFETY CONSIDERATIONS

As young programmers, it is important to use your knowledge of loops responsibly. Here are a few things to keep in mind:

Avoid Writing Harmful Loops: Some hackers use loops to overwhelm websites and servers with repeated requests — this is called a Denial of Service (DoS) attack. This is illegal and unethical. Never use your programming skills to harm others.

Respect Data Privacy: When using loops to process data (such as student records or personal information), ensure the data is handled with care and not shared without permission. Nigeria's data protection laws, including the Nigeria Data Protection Act, apply to digital information.

Test Your Loops Thoroughly: Always test your loops before deploying a program. An untested loop in a banking or medical application could cause serious damage.

Write Clean and Readable Code: Always write loops in a way that other programmers can understand. Clear, readable code is a sign of a responsible and professional programmer.


CLASSROOM AND HOME ACTIVITIES

Activity 1 — Trace the Loop: Draw the flowchart of a for loop that prints the numbers 1 to 5. Show the start, the condition check, the action, and the end.

Activity 2 — Write in Pseudocode: Write a while loop in pseudocode that simulates a student trying to log into a school portal. The loop should keep asking for a password until the correct password "JSS2Naija" is entered.

Activity 3 — Real-Life Identification: Look around your home or school and identify 3 real-life situations where a loop might be happening (e.g., a fan blade spinning, a song repeating on a playlist). Write a short description of each.

Activity 4 — Class Discussion: In groups of four, discuss: "Which type of loop (for or while) would be best for each of these tasks — printing 20 students' names, running a generator until it's out of fuel, counting down from 10 to 1, and displaying a menu until the user chooses to exit?" Share your answers with the class.


ASSESSMENT QUESTIONS

Section A — Objective Questions

  1. A loop in programming is used to: a) Store data permanently b) Repeat a set of instructions multiple times c) Shut down a computer d) Connect to the internet

  2. Which type of loop is best used when you already know the exact number of times the loop should run? a) While loop b) Do loop c) For loop d) Repeat loop

  3. A while loop keeps running as long as: a) The computer has power b) A condition remains true c) The programmer is watching d) The for loop has stopped

  4. What is an infinite loop? a) A loop that runs exactly 100 times b) A loop that never stops because its condition is always true c) A loop that stores infinite data d) A loop used only in games

  5. Which of the following is a real-life example of a loop? a) Turning on a light switch once b) A student writing their name one time c) An ATM asking for a PIN repeatedly until the correct one is entered d) Saving a file to a USB drive

Section B — Theory Questions

  1. Define a loop in programming and explain why loops are important in writing computer programs. Give two examples of situations where a loop would be useful in a Nigerian context.

  2. Describe the difference between a for loop and a while loop. In your answer, include the structure (pseudocode) of each type and state one situation where each would be most appropriate.

  3. What is an infinite loop? Explain how an infinite loop can occur and what a programmer can do to prevent it. Why is an infinite loop dangerous in a real-world application like a banking system?


SUMMARY

In this lesson, we learned the following key points:

  • A loop is a programming instruction that repeats a block of code multiple times
  • Loops save time, reduce code, and make programs more efficient
  • The FOR loop is used when we know the exact number of repetitions in advance
  • The WHILE loop is used when repetitions depend on a condition being true or false
  • An infinite loop is a dangerous error where a loop never stops
  • Loops are used in real-life systems like JAMB portals, telecom networks, payroll systems, and mobile games
  • Programmers must use loops ethically and responsibly

CONCLUSION

Loops are one of the most powerful and widely used tools in programming. From the apps on your phone to the systems that process your school results, loops are quietly working to make everything run smoothly and efficiently.

As a JSS 2 student in Nigeria, mastering loops gives you a strong foundation for everything else in computer science — from building simple programs to eventually creating your own apps, games, or websites. The world needs more Nigerian tech talent, and understanding concepts like loops is the first step in that exciting journey.

Keep practising, keep asking questions, and never stop exploring the world of programming. The next great Nigerian tech innovator could be you!


FREQUENTLY ASKED QUESTIONS (FAQ)

Q1: What is a loop in programming in simple terms? A loop is a way of telling a computer to repeat the same action multiple times without the programmer having to write the same instruction over and over again. It is like telling someone, "Keep doing this until you are done."

Q2: What is the difference between a for loop and a while loop? A for loop is used when you know exactly how many times you want to repeat an action. A while loop is used when you want to keep repeating an action for as long as a certain condition remains true — even if you do not know in advance how many times that will be.

Q3: Is this topic in the Nigerian NERDC curriculum for JSS 2? Yes. Loops in programming fall under the Computer Studies and Basic Technology strand of the NERDC Junior Secondary School curriculum. It is part of the introductory programming concepts taught at the JSS 2 level to prepare students for more advanced topics in senior secondary school.

Q4: What programming language can a JSS 2 student use to practise loops? Beginners can start with visual or pseudocode-based practice, then progress to beginner-friendly languages like Scratch (which uses visual loops), Python, or Blockly. These languages are simple, free, and widely recommended for young learners.

Q5: What happens if a loop never stops? If a loop never stops, it becomes an infinite loop. This can cause a program to freeze, the computer to slow down, or the application to crash entirely. It is a common beginner mistake and can be fixed by ensuring the loop has a proper exit condition.

Q6: How are loops used in everyday Nigerian life? Loops are used in many systems Nigerians interact with daily — including JAMB's CBT exam platform, telecom recharge systems (MTN, Airtel, Glo), bank ATMs, mobile games, supermarket billing systems, and government payroll processing software. Wherever repetitive data processing happens, a loop is almost certainly involved.


This article was written in alignment with the Nigerian NERDC Junior Secondary School curriculum for Computer Studies. It is intended for educational purposes and is suitable for classroom use, home study, and educational blog publication.

I am Echofu George Adah, the Pioneer of Digital Tech Note and the Founder and Chief Executive Officer of GeoWeb Technologies Limited, an information technology company committed to providing cutting-edge ICT solutions.