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 3): do-while Loop

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

Aim

To demonstrate PHP's do-while loop — the exit-controlled loop whose body is guaranteed to run at least once — by printing the numbers 1 to 5.

Theory

do { body } while (condition); inverts the usual order: the body executes first, and the condition is evaluated after each pass. Consequences:

  • At-least-once guarantee: even if the condition is false from the start ($k = 99), the body still executes exactly one time. This is the single behavioural difference from while, which would execute zero times.
  • Because the test happens on the way out, do-while is called an exit-controlled (post-tested) loop; while and for are entry-controlled (pre-tested).
  • Syntax trap: the closing while (condition) must be followed by a semicolon — omitting it is a parse error. There is no such semicolon after a normal while block.
  • Canonical use cases are "do, then decide whether to repeat" workflows: re-displaying a menu until the user picks Exit, prompting again until input validates, retrying a network call until it succeeds or attempts run out, and generating a random value until it is unique.
  • Scope note: variables created in the body are function-scoped, not block-scoped — PHP has no block scope, so $k remains visible after the loop with value 6.

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 p05c_do_while.php in C:\xampp\htdocs\wbplab.
  3. Run http://localhost/wbplab/p05c_do_while.php or php p05c_do_while.php.
  4. Key experiment: set $k = 99 and re-run. The loop still prints 99 once — repeat the same experiment with a plain while and note that it prints nothing. This pair of runs is the whole point of the practical.

Explanation of the Code

  • $k = 1; initialises the control variable before the loop.
  • The do block runs immediately — no condition is consulted on entry. It prints $k followed by a space, then increments with $k++.
  • Only then does while ($k <= 5); decide whether to jump back to the top of the block. Passes continue while the test holds.
  • After printing 5, $k becomes 6, the post-test fails, and control drops out; note the terminating semicolon on the while line.

Expected Output

DO WHILE LOOP:
1 2 3 4 5

With $k = 99 the output line becomes just 99 — one guaranteed pass despite a false condition.

🎯 Viva Questions

  1. Guaranteed minimum executions of a do-while body? One — the condition is only checked after the first pass.
  2. Entry-controlled vs exit-controlled? Entry-controlled loops test before the body (while, for); exit-controlled test after (do-while).
  3. What punctuation ends a do-while? A semicolon after while (condition) — omitting it is a syntax error.
  4. Give two real use cases for do-while. Redisplaying a menu until Exit is chosen; re-prompting until user input validates.
  5. Is $k accessible after the loop, and what is its value here? Yes — PHP has no block scope; it ends as 6.
  6. Convert this loop to while — what changes in behaviour? With $k = 1 nothing; with any initial value above 5, while prints nothing while do-while prints once.

CO Mapping

CO1, CO2