Loops in Php: book back answers

19 solved questions from the end of this chapter, Class 12 Computer Application, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.

← Notes and quiz for Loops in Php

Choose the correct answer

  1. Which of the following is NOT a type of loop statement in PHP?

    • a) for
    • b) if ... else
    • c) while
    • d) do … while
    Try it, then show the answer

    b) if ... else

    The chapter lists for, while, do … while and foreach as loop types. if ... else is not listed as a loop.

    From the book: “There are several types of loops in PHP. They are.”

  2. What type of loop is “for loop” in PHP?

    • a) Entry-Check Loop
    • b) Exit-Check Loop
    • c) Counter Loop
    • d) Iteration Loop
    Try it, then show the answer

    a) Entry-Check Loop

    The chapter identifies the for loop as an entry-check loop.

    From the book: “‘for loop’ is called as the entry-check loop.”

  3. What is the syntax for for loop in PHP?

    • a) for(initialization; condition; increment) { // code}
    • b) foreach(initialization; condition; decrement) { // code}
    • c) while(condition)
    • d) do{...}while(condition)
    Try it, then show the answer

    a) for(initialization; condition; increment) { // code}

    The chapter gives the for-loop syntax with initialization, condition and increment/decrement inside the parentheses, followed by a code block.

    From the book: “for (initialization; condition; increment/decrement) { //code to be executed; }”

  4. What are the three parts of the for loop syntax in PHP?

    • a) initialization, condition, increment
    • b) initialization, code block, condition
    • c) code block, condition, increment
    • d) condition, initialization, code block
    Try it, then show the answer

    a) initialization, condition, increment

    The for-loop syntax contains initialization, condition and increment/decrement as its three parts.

    From the book: “for (initialization; condition; increment/decrement)”

  5. When is the ‘initialization’ part of a for loop executed?

    • a) Before each iteration
    • b) After each iteration
    • c) Only once at the beginning of the loop
    • d) Only once at the end of the loop
    Try it, then show the answer

    c) Only once at the beginning of the loop

    Initialization happens once, at the start of the loop.

    From the book: “The initialization part is executed only once at the beginning of the loop.”

  6. What is the purpose of the ‘increment’ part of a for loop?

    • a) To initialize variables
    • b) To update variables
    • c) To check the condition
    • d) To execute the code block
    Try it, then show the answer

    b) To update variables

    The increment/decrement part updates variables after each iteration.

    From the book: “It is used to update variables.”

  7. What type of loop is “while loop” in PHP?

    • a) Entry-Check Loop
    • b) Exit-Check Loop
    • c) Counter Loop
    • d) Iteration Loop
    Try it, then show the answer

    a) Entry-Check Loop

    The chapter identifies the while loop as an entry-check loop.

    From the book: “‘while loop’ is called as the entry-check loop.”

  8. What type of loop is “do...while loop” in PHP?

    • a) Entry-Check Loop
    • b) Exit-Check Loop
    • c) Counter Loop
    • d) Iteration Loop
    Try it, then show the answer

    b) Exit-Check Loop

    The chapter identifies do … while as an exit-check loop.

    From the book: “‘do … while loop’ is called as the exit-check loop.”

  9. Which looping structure should be used to iterate over elements of an array in PHP?

    • a) for loop
    • b) while loop
    • c) do...while loop
    • d) foreach loop
    Try it, then show the answer

    d) foreach loop

    The foreach loop is used to iterate over array elements.

    From the book: “The foreach loop is used to iterate over elements of an array.”

Part II (2 Marks) Short Answers

  1. What is the purpose of a loop in PHP?

    Try it, then show the answer

    A loop repeatedly executes a set of instructions (a block of code) for a certain number of times or until a certain condition is met.

    A loop repeats a code block either a set number of times or until a condition is met.

    From the book: “A loop executes a set of instructions(a block of code) repeatedly”

  2. Write the syntax for an ‘for’ loop in PHP.

    Try it, then show the answer

    for (initialization; condition; increment/decrement) { //code to be executed; }

    The syntax places initialization, condition and increment/decrement in the for statement, followed by the code block.

    From the book: “for (initialization; condition; increment/decrement) { //code to be executed; }”

  3. How is the ‘condition’ in a while loop evaluated?

    Try it, then show the answer

    The condition is evaluated at the beginning of each iteration. If it is true, the code block is executed; if false, the loop is terminated.

    The while loop checks the condition before each iteration and stops when it is false.

    From the book: “The condition is evaluated at the beginning of each iteration of the loop.”

  4. What is the output of the following code snippet? $i = 1; while ($i <= 5) { echo $i . “<br>”; $i++; }

    Try it, then show the answer

    1<br>2<br>3<br>4<br>5<br>

    The loop starts at 1, prints the current value and increments it until 5. The <br> adds a line break after each value.

    From the book: “To print the numbers from 1 to 5”

Part III (3 Marks) Explain in Brief Answer

  1. Describe the three parts of a for loop in PHP (initialization, condition and increment) and explain their purpose.

    Try it, then show the answer

    Initialization is executed once at the beginning and initializes variables. The condition is checked before each iteration; if true, the code block runs, and if false, the loop ends. The increment/decrement part runs after each iteration to update variables.

    These three parts respectively prepare variables, decide whether to continue, and update variables for the next iteration.

    From the book: “The increment/ decrement part is executed after each iteration of the loop.”

  2. Write a PHP code to print 1 to 10 numbers in ascending order using for loop.

    Try it, then show the answer

    for ($i = 1; $i <= 10; $i++) { echo $i . "<br>"; }

    The loop begins with $i at 1, continues while it is at most 10, and increments $i after each iteration.

    From the book: “To print the numbers from 1 to 5 in ascending order”

  3. Explain the difference between while loop and do … while loop in PHP.

    Try it, then show the answer

    A while loop checks its condition before executing the code block, so it may execute zero times if the condition is initially false. A do … while loop executes the code block first and checks the condition afterward, so it executes at least once.

    The main differences are when the condition is checked and whether the code block is guaranteed to run once.

    From the book: “The do...while loop first executes the code block and then evaluates the condition.”

Part IV (5 Marks) Explain in detail

  1. Explain ‘for’ loop with example.

    Try it, then show the answer

    A loop executes a block of code repeatedly for a certain number of times or until a condition is met. The ‘for loop’ is used to execute a block of code a specific number of times.

    1. Type of loop: The for loop is called an entry-check loop.
    2. Syntax: A for loop has initialization, condition, and increment or decrement parts, followed by the code to be executed.
    3. Initialization: Initialization runs once at the beginning and is used to initialize variables.
    4. Condition: The condition is checked before each iteration. If it is true, the code block runs; if false, the loop terminates.
    5. Increment or decrement: This part runs after each iteration and updates variables.

    Example: To print the numbers from 1 to 5 in ascending order: for ($i = 1; $i<= 5; $i++) echo $i . “<br>”;

    Thus, a for loop repeats a block of code according to its initialization, condition, and update.

    From the book: “The for loop is used to execute a block of code a specific number of times.”

  2. Write a PHP code to print 10 to 20 numbers in ascending order using while loop and do… while loop.

    Try it, then show the answer

    Using while loop: $i = 10; while ($i <= 20) { echo $i . "<br>"; $i++; } Using do…while loop: $i = 10; do { echo $i . "<br>"; $i++; } while ($i <= 20);

    Both loops start at 10, print and increment the value, and continue through 20. The while loop checks before the code block; the do…while loop checks after it.

    From the book: “The do...while loop is similar to the while loop, except that the code block is always executed at least once”

  3. Explain ‘foreach’ loop with examples.

    Try it, then show the answer

    The foreach loop is used to iterate over the elements of an array. It executes a block of code for each element.

    1. Syntax: Use the array followed by as and a value variable. The code block is executed for each array element.
    2. Array values: During each iteration, the value variable represents an element of the array.
    3. Example: The following example prints each element of an array containing the numbers 1 to 5.
    4. Iterating over keys and values: For an associative array, use the key and value variables to access both parts of each element.
    5. How keys and values are assigned: In each iteration, the element's key is assigned to $key and its value to $value.
    6. Uses: The loop is useful for performing actions on every array element, such as printing or modifying elements.

    Example: $array = array(1, 2, 3, 4, 5); foreach ($array as $value) echo $value . “<br>”; This prints 1 2 3 4 5.

    Thus, foreach provides a simple and efficient way to iterate over arrays in PHP.

    From the book: “The foreach loop will iterate over each element of the array and assign the key of the element to the $key variable”

Next chapterForms and Files