Before looking MCQ questions and answers on php do while loop, we must know what is do while loop and before that must know the meaning of Loop. This is a very important concept in PHP, every developer must have deep knowledge about php do while loop. Most of the questions ask about do while loop interview.
What is Loop in PHP?
In PHP, a loop is a programming construct that allows us to execute a block of code repeatedly. Loops are essential for automating repetitive tasks or iterating over collections of data. Using Loop we can execute the same code several times. Each loop has its own purpose and usage. There are multiple loops in PHP, but now we will see PHP do while loop.
Flowchart of PHP Do While Loop
When to use loops in PHP?
Loop in PHP is used to execute a statement or a block of statements (code), multiple times until and unless a specific condition is true. This helps the user to save both time and effort of writing the same code multiple times.
How many types of loop in php?
There are 4 loops in PHP.
- For
- Foreach
- While
- Do while
But now we will discuss about Do while loop only.
What is a do while loop?
In do while loop code executes once, and repeat the same code depends on a specified condition in while. Let’s understand the PHP do while loop concept with an example.
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
Output of the above code is 0 (zero). Value of $i is 0, but in while loop we have given condition as $i > 0, value of $i is 0 means while(0 > 0), condition is false, even getting output is 0. Now question in your mind is that, condition is not true even how we got output as 0.
Let me explain you, in do while loop, output print at least once even condition is not true. Output will print first then will check condition in while loop.
Now we take another example for better understanding.
<?php
$i = 1;
do {
echo $i."<br>";
$i++;
} while ($i < 6);
?>
Output of above code like as below.
1
2
3
4
5
Here the output is 1 to 5, because value of $i is 1 and in while condition is $i < 6, means 1 < 6, that’s why output is 1 to 5.
Hope you understand now, in do while loop there is a guaranteed result at least once.
What does break do in a do-while loop?
break is a keyword that is used in do while loop. break stops the execution of loop when a condition is true. Let’s see an example for a better understanding of break.
<?php
$i = 1;
do {
if ($i == 3) break;
echo $i."<br>";
$i++;
} while ($i < 6);
?>
Output of the above code is as below.
1
2
When value of $i will be 3, execution will stop.
What does continue do in a do-while loop?
Working of continue keyword is exactly inverse of break keyword. continue keyword not stop execution of loop. Let’s understand with example.
<?php
$i = 0;
do {
$i++;
if ($i == 3) continue;
echo $i."<br>";
} while ($i < 6);
?>
Output of the above code will like as below.
1
2
4
5
6
In above output we can see that, value 3 has not printed there, because if ($i == 3) continue; means when value of $i is 3, that loop will not execute, but a further loop has been executed.
Hope you understood do while loop. Do while loop is a very important concept for the PHP Developer, so in an interview there are questions on do while loop. So we can see now important do while loop mcq questions.
Do while loop mcq Questions and Answers
What is the purpose of a do-while loop in PHP?
- To execute a block of code while a condition is true, and then check the condition
- To execute a block of code only once
- To execute a block of code while a condition is true
- To execute a block of code at least once, and then check the condition
Answer: 4) To execute a block of code at least once, and then check the condition
Which keyword is used to start a do-while loop in PHP?
- do
- while
- for
- loop
Answer 1) do
In a do-while loop, where is the condition evaluated?
- At the beginning of the loop
- At the end of the loop
- Before executing the loop body
- After executing the loop body
Answer 2) At the end of the loop
What happens if the condition in a do-while loop is initially false?
- The loop runs indefinitely
- The loop executes once and then terminates
- The loop does not execute at all
- The loop throws an error
Answer 2) The loop executes once and then terminates
Which symbol is used to terminate the statement block in a do-while loop?
- Semicolon (;)
- Colon (:)
- Curly braces ({ })
- Parentheses (() )
Answer 3) Curly braces ({ })
What is the main advantage of using a do-while loop over a while loop?
- A do-while loop is always more efficient
- A do-while loop can handle multiple conditions
- A do-while loop guarantees the execution of the loop body at least once
- There is no advantage, they are equivalent in functionality
Answer 3) A do-while loop guarantees the execution of the loop body at least once
Which of the following statements about the do-while loop is true?
- It cannot contain break or continue statements
- It can only iterate over arrays
- The loop condition must be enclosed in parentheses
- It is suitable for situations where the loop body should execute at least once
Answer 4) It is suitable for situations where the loop body should execute at least once
What happens if the condition in a do-while loop is always true?
- The loop runs indefinitely
- The loop executes once and then terminates
- The loop does not execute at all
- The loop throws an error
Answer 1) The loop runs indefinitely
You don’t know these facts about Super Global Variables
Which of the following is NOT a valid way to terminate a do-while loop?
- Using a break statement
- Using a return statement
- Using the continue statement
- Using the exit() function
Answer 2) Using a return statement
What will be the output of the following PHP code snippet?
$num = 3;
do {
echo $num * $num;
$num++;
} while ($num <= 5);
- 9 16 25
- 9 12 15
- 3 6 9 12 15
- 3 4 5
Answer: 1) 9 16 25
What is the difference between while and do while in PHP?
Feature | while loop | do while loop |
Syntax | while (condition) { code block } | do { code block } while (condition); |
Execution | Condition is evaluated before entering the loop | Code block is executed once before checking the condition |
Use case | Typically used when the loop may not need to execute at all | Suitable for situations where the loop body should execute at least once |
Example | $i = 0; while ($i < 5) { echo $i; $i++; } | $i=0; do { echo $i; $i++; }while($i < 5); |
Follow Us on Telegram
Follow Quiz Lancer Telegram Channel to solve daily interview questions. We share important interview questions which are asked in the interview. Most of the students get good values from this channel and got selected as a developer.
Which loop is better (faster) while or do-while?
The difference between while and do while loops based on execution speed is that a do while loop runs faster than a while loop. The do-while is faster because it runs the first iteration without checking the loop condition. In contrast, the while loop checks the condition always.While loop takes time for check condition, in do while loop there is no need to check condition first. So as per this things do while loop is faster than while loop.