In the bustling city of Codeville, the Grand Council of Developers often encountered complex scenarios requiring precise control of logic flow. To tackle these challenges, they decided to delve into the power of loops and conditional statements, specifically focusing on while, if, if else, and else if. Join us as we explore how they used these constructs in PHP to solve real-world problems.
Mayor Binary convened the council. “Understanding the basics of conditional statements and loops is crucial. These constructs allow us to control the flow of our programs and make decisions based on various conditions.”
Conditional Statements
Loop Statements
The council decided to write a PHP script to illustrate the use of these constructs by checking numbers within a range for specific properties: prime, odd, even, and multiple of 5.
Prime Number Check Function
The council implemented a function to check if a number is prime:
php
Copy code
<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
return false;
}
}
return true;
}
?>
Using while, if, if else, and else if
Next, they used these constructs to process numbers within a specified range:
php
Copy code
<?php
// Define the range
$start = 1;
$end = 50;
// Initialize the counter
$current = $start;
// Loop through the range
while ($current <= $end) {
// Check if the number is prime
if (isPrime($current)) {
echo “$current is a prime number.<br>”;
} else if ($current % 2 == 0) {
// Check if the number is even
echo “$current is an even number.<br>”;
} else if ($current % 5 == 0) {
// Check if the number is a multiple of 5
echo “$current is a multiple of 5.<br>”;
} else {
// If none of the above, it must be an odd number
echo “$current is an odd number.<br>”;
}
// Increment the counter
$current++;
}
?>
Here’s the complete script combining the prime check function and various conditional statements within a while loop:
php
Copy code
<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
return false;
}
}
return true;
}
// Define the range
$start = 1;
$end = 50;
// Initialize the counter
$current = $start;
// Loop through the range
while ($current <= $end) {
// Check if the number is prime
if (isPrime($current)) {
echo “$current is a prime number.<br>”;
} else if ($current % 2 == 0) {
// Check if the number is even
echo “$current is an even number.<br>”;
} else if ($current % 5 == 0) {
// Check if the number is a multiple of 5
echo “$current is a multiple of 5.<br>”;
} else {
// If none of the above, it must be an odd number
echo “$current is an odd number.<br>”;
}
// Increment the counter
$current++;
}
?>
By mastering the use of while loops and conditional statements (if, if else, else if), the Grand Council of Developers in Codeville effectively implemented logic to categorize numbers based on various properties. These fundamental constructs are essential for controlling the flow of programs and making decisions based on different conditions. Feel free to experiment with the provided script and modify it to suit different scenarios or add additional conditions. If you have any questions or need further assistance, I’m here to help. Happy coding!