TN Online TestSamacheer Kalvi practice

Loops in PHP: One Mark Questions, Book Back Answers and Notes

12th Standard · Computer Applications · #7

This chapter covers the four loops in PHP: for, while, do-while and foreach. You learn the syntax of each, which ones are entry-check and which is exit-check, and which loop suits a fixed count, an open-ended condition or an array.

Study this chapter

Asked in previous board exams

9 questions from this chapter came in 7 past public exams. Open any of them to see the answer and explanation.

  1. March 2026
    What is the purpose of the ‘increment’ part of a for loop?
    See answer in the solved paper →
  2. March 2025
    What is the output of the following code ?
    $i = 4;
    do
    {
    echo $i."<br>";
    $i++;
    } while ($i<=3);
    See answer in the solved paper →
  3. March 2025
    Which looping structure should be used to iterate over elements of an array in PHP ?
    See answer in the solved paper →
  4. March 2025
    What type of loop is "while loop" in PHP ?
    See answer in the solved paper →
  5. March 2024
    What is the output of the following code ?
    for($i=5; $i>=1; $i--)
    {
    echo $i ."<br>";
    }
    See answer in the solved paper →
  6. March 2023
    Loops that execute the same block of code, a specified number of times is called :
    See answer in the solved paper →
  7. May 2022
    The loop exclusively used for array is ________.
    See answer in the solved paper →
  8. Supplementary 2021
    PHP supports which types of looping techniques :
    See answer in the solved paper →
  9. March 2020
    Most complicated looping structure in PHP is ________.
    See answer in the solved paper →

About Loops in PHP

Medium ~90 min study

Suppose you need to print the numbers 1 to 100. Writing 100 echo statements would be slow, and one typing error would spoil the output. A loop lets you write the statement once and have PHP repeat it as many times as needed. That is the whole idea of this chapter.

Loops bring together three things you already know: variables, conditions and arrays. The for and while loops check their condition before running the body, so they are called entry-check loops. The do-while loop checks the condition after the body, so it is an exit-check loop and always runs at least once. The foreach loop is meant only for arrays and hands you each element, and its key if you ask for it, one at a time.

In the exam you are asked to write the syntax of each loop, to compare while with do-while, and to predict the output of a short loop. Practical questions often ask for a series, such as the sum of the first 10 numbers or a multiplication table, or for printing every element of an array with foreach. Tracing the loop by hand, one iteration at a time, is the surest way to get these right.

What you'll learn

  • Explain what a loop is and why scripts use loops instead of repeated statements.
  • Write for and while loops with correct syntax.
  • Write a do-while loop and explain why its body runs at least once.
  • Compare while and do-while loops in terms of when the condition is checked.
  • Use foreach to print the values, or keys and values, of an array.
  • Write PHP scripts for simple series such as the sum of numbers or a multiplication table.

Before you start

  • Basic PHP syntax and how PHP code is embedded in a page.
  • Declaring variables, data types and arithmetic operators.
  • Conditional statements: if, else and elseif.
  • PHP arrays, both indexed and associative.

Topics covered in this chapter

What a Loop Is A control structure that repeats a block of statements until its condition becomes false or all the elements have been processed.
Entry-Check Loops for and while test the condition before the body runs, so the body may run zero times.
Exit-Check Loop do-while runs the body first and tests the condition afterwards, so the body runs at least once.
for Loop Syntax Initialisation, condition and increment or decrement written together inside one pair of parentheses, separated by semicolons.
foreach for Arrays A loop only for arrays that gives you each value, or each key and value pair, without a counter.
Avoiding Infinite Loops Making sure the body changes the variable in the condition so that the condition eventually becomes false.

Loops in PHP explained

How Loops Work in PHP

What a Loop Does

A loop runs a block of statements again and again. Each run is called an iteration. Before or after every iteration, PHP checks a condition to decide whether to run the block once more or stop. Loops are how a script prints long lists, builds rows of an HTML table from data, or adds up values that change by a fixed step each time.

The for Loop (Entry-Check)

Use for when you know in advance how many times the block must run. Its parentheses hold three parts separated by semicolons: initialisation, condition and increment or decrement, as in for ($i = 1; $i <= 10; $i++). The initialisation runs once. Then PHP checks the condition; if it is true, the body runs, the counter is updated, and the condition is checked again. If the condition is false the very first time, the body never runs.

The while Loop (Entry-Check)

while is also an entry-check loop, but it suits cases where the number of repetitions is not fixed in advance. It checks one condition before each iteration and keeps running the body as long as the condition stays true. The loop itself does not change any variable, so something inside the body must change the value being tested. If nothing does, the condition stays true and the loop never ends.

The do-while Loop (Exit-Check)

do-while puts the body first and the condition after it, do { ... } while (condition);. Because the check happens at the end, the body always runs at least once, even if the condition is false from the start. After each run, PHP tests the condition: true means repeat, false means stop. Note the semicolon after the closing while (condition); this is the one loop where it belongs.

The foreach Loop for Arrays

foreach works only on arrays. It walks through the elements of an indexed or associative array in order, without any counter variable. In foreach ($marks as $value), each element is copied into $value in turn. In foreach ($marks as $key => $value), you get both the key and the value. This makes it the easiest way to print every item of an array on a web page.

Common mistakes to avoid

  • Forgetting to change the loop variable inside a while or do-while body. The condition stays true and the loop never stops. Increment or decrement the counter inside the body.
  • Putting a semicolon right after for (...) or while (...). The loop then repeats an empty statement, and the block in braces runs only once after it. The semicolon belongs after while (...) only in do-while.
  • Mixing up less than and less than or equal to in the condition. A for loop that starts $i at 1 and runs while $i is less than 10 prints 1 to 9, not 1 to 10. Check the last value by hand.
  • Passing a variable that is not an array to foreach. PHP gives a warning and the loop does not run. Make sure the variable holds an array.

Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.

Frequently asked questions

What is the difference between while and do-while in PHP?

while is an entry-check loop: it tests the condition first, so the body may not run at all. do-while is an exit-check loop: it runs the body first and tests the condition afterwards, so the body always runs at least once.

How does a foreach loop work with arrays?

foreach goes through the array from the first element to the last. In foreach ($arr as $value), each element's value is placed in $value in turn. In foreach ($arr as $key => $value), you also get the key, which is useful for associative arrays.

Why does my while loop run forever?

Its condition never becomes false. Usually the counter is not incremented inside the body, or the condition is written the wrong way, for example $i > 0 when $i keeps increasing. Check that the body changes the variable used in the condition.

When should I use a for loop instead of a while loop?

Use for when the number of iterations is known, such as printing 1 to 10. It keeps initialisation, condition and increment on one line. Use while when the loop should stop on a condition whose timing you do not know in advance.

Do I need curly braces around the loop body?

Braces group the statements that must repeat together. For a single statement PHP allows you to leave them out, but only that one statement is repeated. Always using braces avoids errors when you add a second statement later.

What does entry-check loop mean?

An entry-check loop tests its condition before running the body. If the condition is false on the first check, the body is skipped completely. In PHP, for and while are entry-check loops.

Last updated 15 September 2026

More chapters in Computer Applications

View all
1 Multimedia 2 An Introduction to Adobe PageMaker 3 Introduction to Database Management System 4 PHP: Hypertext Preprocessor 5 Functions and Arrays in PHP 6 Conditional Statements in PHP 8 Forms and Files 9 Connecting PHP and MYSQL 10 Introduction to Computer Networks 11 Network Examples and Protocols 12 Domain Name System (DNS) 13 Network Cabling 14 Open Source Concepts 15 E-Commerce 16 Electronic Payment Systems 17 E-Commerce Security Systems 18 Electronic Data Interchange (EDI)