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 1): for Loop

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

Aim

To demonstrate PHP's for loop — the counter-controlled, entry-checked iteration construct — by printing the numbers 1 to 5.

Theory

The for loop packs the three pieces of counter management into one header:

for (initialization; condition; increment) { body }
  • Initialization runs exactly once, before anything else ($i = 1).
  • Condition is tested before every iteration — making for an entry-controlled loop; if it is false on the very first test, the body never runs (zero-iteration case).
  • Increment runs after each body execution, then the condition is re-tested.

Advanced notes:

  • All three header sections are optional — for (;;) is a deliberate infinite loop, exited with break.
  • Each section may hold multiple comma-separated expressions: for ($i = 0, $j = 10; $i < $j; $i++, $j--).
  • $i++ (post-increment) and ++$i (pre-increment) are equivalent in the header because the expression value is discarded.
  • Off-by-one errors are the dominant bug class: $i <= 5 runs 5 times, $i < 5 runs 4 — always reason about the boundary.
  • for is ideal when the number of iterations is known in advance; when it depends on data (file lines, DB rows), while fits better; for arrays, foreach is idiomatic.
  • break exits the loop entirely, continue jumps to the increment step of the next iteration.

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 p05a_for.php in C:\xampp\htdocs\wbplab.
  3. Execute via http://localhost/wbplab/p05a_for.php or php p05a_for.php.
  4. Modify the header — start at 10, count down with $i--, or step by 2 with $i += 2 — and verify your prediction of the output each time.

Explanation of the Code

  • echo "FOR LOOP:\n"; prints the heading; \n is a newline in the double-quoted string (use <br> when rendering to HTML).
  • The header for ($i = 1; $i <= 5; $i++) initialises the counter to 1, keeps looping while it is at most 5, and bumps it by one after every pass.
  • The body echo $i . " "; concatenates the current counter with a space using the . operator, producing the sequence on one line.
  • Execution order for each pass is: condition → body → increment; after $i becomes 6 the condition fails and control leaves the loop.

Expected Output

FOR LOOP:
1 2 3 4 5

The counter values appear on a single line separated by spaces.

🎯 Viva Questions

  1. Why is for called entry-controlled? Its condition is evaluated before every iteration, including the first.
  2. Exact execution order of the header parts? Initialization once; then condition → body → increment, repeated.
  3. How many times does for ($i = 0; $i < 5; $i++) run? Five times, for i = 0..4.
  4. What does for (;;) mean? All sections empty — an infinite loop, usually exited by break.
  5. break vs continue? break leaves the loop; continue skips to the next iteration's increment.
  6. When would you pick while over for? When the iteration count is unknown in advance, e.g. reading rows until a query is exhausted.

CO Mapping

CO1, CO2