While loop works same the other programming language. In the while loop, If the expression’s result is true, the loop will run all the statements inside it. If the result is false, however, the loop will end and pass the program control to the statements after it.
Syntax:-
while (expression)
statement
Example:-
<?php
$i=1;
while($i<5){
print $i."<br/>";
$i++;
}
?>
1
2
3
4
2
3
4
Php While loop – Interview Questions
Q 1: What is while loop?
Ans: Executes code while the condition is true.
Q 2: When is the condition checked?
Ans: Before loop execution.
Q 3: Can while loop run zero times?
Ans: Yes.
Q 4: Syntax example?
Ans: while ($i < 5) { }
Q 5: Risk of while loop?
Ans: Infinite loop if condition never becomes false.
C# Dictionary – Objective Questions (MCQs)
Q1. The while loop checks condition ______ executing the loop.
Q2. If the condition is false initially, while executes ______.
Q3. Which keyword skips the current iteration?
Q4. Missing condition update may cause ______.
Q5. while loop is ideal when iterations are ______.