Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 5 (Part 2): while Loop

Lesson 7 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To demonstrate PHP's while loop — the entry-controlled loop used when the number of iterations is not known in advance — by printing the numbers 1 to 5.

Theory

while (condition) { body } evaluates its condition before each pass. If the condition is false at the first check, the body executes zero times — that zero-iteration behaviour is the defining property of an entry-controlled loop.

Structural contrast with for: a while loop spreads the counter logic across three places — initialisation before the loop ($j = 1), the test in the header ($j <= 5), and the update inside the body ($j++). Because the update is a normal statement, forgetting it is the most common cause of infinite loops: the condition never changes, and the script runs until max_execution_time (default 30 s under Apache) kills it.

Where while genuinely shines:

  • Consuming data of unknown length: while ($row = $result->fetch_assoc()) { ... } when reading MySQL rows, or while (!feof($handle)) when streaming a file.
  • Polling/retry loops that end on an external state change.
  • Sentinel-controlled input processing (loop until a stop value appears).

Any for loop can be rewritten as while and vice versa — the choice is about communicating intent: for says "counted repetition", while says "repeat while a state holds".

Requirements

  • XAMPP/WAMP with PHP 8.x, or PHP CLI
  • Code editor and a browser/terminal

Procedure

  1. Start Apache from the XAMPP Control Panel.
  2. Save the snippet as p05b_while.php in C:\xampp\htdocs\wbplab.
  3. Run http://localhost/wbplab/p05b_while.php or php p05b_while.php.
  4. Comment out $j++; and re-run to witness (and then stop) a runaway loop — this makes the role of the update statement unforgettable.
  5. Change the initial value to $j = 6 and confirm the body never executes.

Explanation of the Code

  • $j = 1; initialises the loop-control variable before the loop — while has no built-in initialisation slot.
  • while ($j <= 5) is the guard: it is checked before every pass, so the loop admits exactly five passes.
  • Inside the body, echo $j . " "; prints the current value followed by a space.
  • $j++; is the progress statement — the line that guarantees the condition eventually turns false. Every correct while loop must contain something that moves the state toward termination.
  • When $j reaches 6, the guard fails and execution continues after the loop.

Expected Output

WHILE LOOP:
1 2 3 4 5

Five numbers on one line; starting with $j = 6 would print only the heading.

🎯 Viva Questions

  1. Minimum number of body executions of a while loop? Zero — the condition is tested before entry.
  2. What causes an infinite while loop? The body never changes the variables used in the condition (e.g. missing $j++).
  3. Where does initialisation live for a while loop? In ordinary statements before the loop — the construct has no initialisation section.
  4. Give a real use case where while beats for. Fetching database rows: while ($row = $result->fetch_assoc()) — the count is unknown.
  5. What limits a runaway PHP script under Apache? The max_execution_time ini setting (30 s by default).
  6. Is while (true) ever legitimate? Yes — event/polling loops that terminate via break on an internal condition.

CO Mapping

CO1, CO2