Flow control – For, While and Do-while loops

At the end of this post you will be able to demonstrate how to create and when to apply:

  • A for loop
  • A while loop
  • A do… while loop

What are loops?

Loops will:

  • Continue to perform an action until a certain condition is met
  • Unlike an if/else statement which finish after the next closing brace a condition is checked again until it is met

Why bother with loops?

  • Prevents writing the same instruction more than twice
  • Unlike an if statement it will repeat or iterate until a condition is met
  • Provides cleaner more efficient code

Loops in context

The loop will continue until the condition is met:

loop_flow

The difference between if/else and loops

  • while – loops through a block of code while a specified condition is true
  • dowhile – loops through a block of code once, and then repeats the loop as long as a specified condition is true
  • for – loops through a block of code a specified number of times
  • foreach – loops through a block of code for each element in an array

combo_flow

Introducing the ‘While’ loop

Key facts about the while loop:

The While loop Will iterate while a condition is true

while (condition){    
  code to be executed;
} 

Let’s see a while loop in action

//Declare a variable
$i=1;

//Set and test condition
while($i<=5)
{
  echo "The number is " . $i . "
";
  $i++;
}

Output: The number is 5

Introducing ‘Do… While’

The DO-WHILE is an interesting one…  The way it works is the code will always run once before the condition is tested.  I guess a scenario would be write to a log file once to say the loop has started and then write more actions as the loop progresses.

So what does it look like

//Declare a variable
$i=1;

//Set and test condition

do
{
  $i++;
  echo "The number is " . $i . "
";
}
while ($i<=5);

Key facts about Do… While:

  • The do…while statement will always execute the block of code once
  • It will then check the condition, and repeat the loop while the condition is true.

Task:  Try it yourself by setting the $i variable to ‘5’.  The loop will still run once.

The for loop

The for loop is is different from the while in that can take more parameters – three in total.  It is often considered that a for loop is best used when you know exactly how many iterations you have to complete based on a total given.

The for loop in action

//Set and test condition

for ($i=1; $i<=5; $i++)
{
  echo "The number is " . $i . "
";
}

Output: The number is 5

The foreach loop

Key facts about the foreach loop:

The foreach loop is designed to work with arrays.

The loop iterates through the list and assigns each lit item in the array to a variable or to another array.

//Create indexed array
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;

//Call PHP's inbuilt count array function 
$result = count($a);
$i = 0;

//Construct for loop
//NOTE: < operator as all arrays start with zero which in array terms counts as one
foreach($a as $row)
{
 if($row == 3)
 {
  echo $row;
 }
 else
 {
  echo "This is not three!";
 }
}

Just to spice things up I have used an if/else statement with the loop just to demonstrate how conditional statements can be used in conjunction with loops.

Task: to consolidate your learning build this:

Using the examples we have just explored build a small application with a for or while loop. The loop must loop through a list of items and add a value of £3 delivery charge on to any figure less than £10.

What have we explored?

In lesson post we have:

Demonstrated how to create and when to apply:

  • A for loop
  • A while loop
  • A do… while loop
  • Built a simple application using a loop to consolidate your learning
No Comments

Post a Comment