"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Friday, August 30, 2024

Class 4: Control Structures - Part 1

Class 4: Control Structures - Part 1

Objective:

Learn to use conditional statements (if, else if, else) and the switch statement for decision-making.

1. Introduction to Control Structures

Control structures in PHP are essential for controlling the flow of a script based on conditions. They allow you to make decisions in your code, executing certain blocks of code only when specific conditions are met.


1. Conditional Statements

a) if Statement

The if statement executes a block of code if a specified condition is true.

Syntax:

if (condition) {

    // Code to execute if the condition is true

}

Example:

<?php

    $age = 20;


    if ($age >= 18) {

        echo "You are eligible to vote.";

    }

?>

Explanation:

In this example, if the $age is 18 or more, the message "You are eligible to vote." will be displayed.

b) else Statement

The else statement executes a block of code if the if condition is false.

Syntax:

if (condition) {

    // Code to execute if the condition is true

} else {

    // Code to execute if the condition is false

}

Example:

<?php

    $age = 16;

    if ($age >= 18) {

        echo "You are eligible to vote.";

    } else {

        echo "You are not eligible to vote.";

    }

?>

Explanation:

Here, if the $age is less than 18, the message "You are not eligible to vote." will be displayed.

c) else if Statement

The else if statement allows you to test multiple conditions. If the first condition is false, it checks the next one, and so on.

Syntax:

if (condition1) {

    // Code to execute if condition1 is true

} else if (condition2) {

    // Code to execute if condition1 is false and condition2 is true

} else {

    // Code to execute if both condition1 and condition2 are false

}

Example:

<?php

    $marks = 75;

    if ($marks >= 90) {

        echo "Grade: A";

    } else if ($marks >= 75) {

        echo "Grade: B";

    } else if ($marks >= 60) {

        echo "Grade: C";

    } else {

        echo "Grade: F";

    }

?>

Explanation:

This script checks the value of $marks and assigns a grade based on the range it falls into.

2. The switch Statement

The switch statement is used when you need to compare the same variable against different values. It’s a more organized way of handling multiple else if conditions.

Syntax:

switch (variable) {

    case value1:

        // Code to execute if variable equals value1

        break;

    case value2:

        // Code to execute if variable equals value2

        break;

    // More cases as needed

    default:

        // Code to execute if no cases match

}

Example:

<?php

    $day = "Tuesday";


    switch ($day) {

        case "Monday":

            echo "Start of the workweek!";

            break;

        case "Wednesday":

            echo "Midweek day!";

            break;

        case "Friday":

            echo "Almost weekend!";

            break;

        default:

            echo "Just another day.";

    }

?>

Explanation:

In this example, the script checks the value of $day. If it matches "Monday", "Wednesday", or "Friday", it outputs a specific message. If none of the cases match, it executes the default case and outputs "Just another day."


4. Practical Implementation

Example 1: 

Nested if Statements

<?php

    $temperature = 35;

    if ($temperature > 30) {

        echo "It's hot outside.";

        if ($temperature > 40) {

            echo " Be careful of the heatstroke!";

        }

    } else {

        echo "It's not too hot.";

    }

?>

Explanation:

This example shows how to nest if statements. If the temperature is above 30, it checks further if it's above 40 to give an additional warning.

Example 2: 

Using switch for Menu Selection

<?php

    $choice = 2;

    switch ($choice) {

        case 1:

            echo "You selected Option 1.";

            break;

        case 2:

            echo "You selected Option 2.";

            break;

        case 3:

            echo "You selected Option 3.";

            break;

        default:

            echo "Invalid selection.";

    }

?>

Explanation:

This script checks the value of $choice and outputs a message based on the selected option. If the user selects something outside of 1, 2, or 3, it outputs "Invalid selection."


Outcome

By the end of this class, students will be able to write scripts that effectively use if, else if, else, and switch statements.

They will understand how to control the flow of their code based on different conditions and scenarios. This foundational knowledge is crucial for creating dynamic and responsive PHP applications.

Assignments

1: Simple Grade Calculator

Objective: 

Write a PHP script that takes a student's score as input and assigns a grade based on the following criteria:

A: 90 and above

B: 75 to 89

C: 60 to 74

D: 50 to 59

F: Below 50

Instructions:

Use if, else if, and else statements to determine the grade.

Test the script with different scores to ensure all conditions work correctly.

Expected Output:

Input: 85 → Output: "Grade: B"

Input: 45 → Output: "Grade: F"

Assignment 2: 

Day of the Week Message

Objective: 

Create a PHP script that takes the name of a day as input and outputs a message based on the day.

Monday: "Start your week strong!"

Wednesday: "Midweek blues."

Friday: "Weekend is almost here!"

Other days: "Just another day."

Instructions:

Use a switch statement to handle the different days.

Make sure to include a default case for days that don't match the specific cases.

Expected Output:

Input: "Monday" → Output: "Start your week strong!"

Input: "Sunday" → Output: "Just another day."

Assignment 3: 

Number Range Checker

Objective: 

Write a PHP script that checks if a given number falls within a specific range and outputs an appropriate message.

If the number is between 1 and 10: "The number is in the range 1-10."

If the number is between 11 and 20: "The number is in the range 11-20."

If the number is between 21 and 30: "The number is in the range 21-30."

If the number is outside these ranges: "The number is out of range."

Instructions:

Use if, else if, and else statements to check the range.

Test with various numbers to ensure it covers all cases.

Expected Output:

Input: 15 → Output: "The number is in the range 11-20."

Input: 31 → Output: "The number is out of range."

Assignment 4: 

Simple Calculator Using switch

Objective: 

Develop a simple calculator that performs addition, subtraction, multiplication, or division based on user input.

The script should take two numbers and an operator (+, -, *, /) as input.

Based on the operator, perform the corresponding operation and display the result.

Instructions:

Use a switch statement to handle the different operators.

Ensure that division by zero is handled appropriately with an error message.

Expected Output:

Input: 5, 3, + → Output: "Result: 8"

Input: 10, 0, / → Output: "Error: Division by zero is not allowed."

Assignment 5: 

Age Group Identifier

Objective: 

Write a PHP script that categorizes a person into an age group based on their age:

0-12: "Child"

13-19: "Teenager"

20-59: "Adult"

60 and above: "Senior"

Instructions:

Use if, else if, and else statements to determine the age group.

Provide test cases with different ages to ensure all conditions are covered.

Expected Output:

Input: 5 → Output: "Child"

Input: 65 → Output: "Senior"

These assignments will give students hands-on experience with control structures, allowing them to understand how to direct the flow of their programs based on conditions. Encourage them to test their scripts thoroughly to ensure they handle all possible scenarios.








No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template