Control Program Flow (Decision statement and Looping statements)

 

1. Decision Statements (Branching)
These allow your program to execute different blocks of code based on specific conditions.
 
The if, else, and elseif Statements
 

$age = 20;

if ($age < 13) {

    echo “Child”;

} elseif ($age < 20) {

    echo “Teenager”;

} else {

    echo “Adult”;

}

// Output: Number: 1 Number: 2 Number: 3

 
2. Looping Statements (Iteration)
Loops execute the same block of code repeatedly as long as a certain condition is met.
 
while and do...while
  • while: Checks the condition before executing the code.
  • do...while: Executes the code once first, then checks the condition.

 

$i = 1;

while ($i <= 3) {

    echo “Number: $i “;

    $i++;

}

// Output: Number: 1 Number: 2 Number: 3