PHP: For Loops I

Derek Bridge

Department of Computer Science,
University College Cork

PHP: For Loops I

Aims:

Doing something repeatedly

for-loops

Observations: terminology

In the example,

Observations: how it works

Flowchart

[A flowchart showing a simple for-loop.]

Class exercises

The example is a for-loop where $i counts from 0 to 4 inclusive but stops when $i is 5 (hence the body is executed 5 times)

  1. Write a loop in which $i counts from 0 to 99 inclusive. How many times is the loop body executed?
  2. Write a loop in which $j counts from 0 to 100 inclusive. How many times is the loop body executed?
  3. Suppose variable $num_exams contains the number of exams a student must sit. Write a loop that executes its loop body exactly $num_exams times

Class exercise

Class exercise

Class exercise