Explore BrainMass
Share

Explore BrainMass

    Repetition and Switch Structures

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Write a program that uses repetition and switch structures to print the song "The Twelve Days of Christmas." One switch should be used the print the day (i.e., "First", "Second", "Third", etc.). and another switch structure should print the remainder of each verse.

    © BrainMass Inc. brainmass.com October 9, 2019, 9:05 pm ad1c9bdddf
    https://brainmass.com/computer-science/php/i-need-help-with-using-the-switch-functions-i-m-completely-lost-on-how-to-write-this-program-172942

    Solution Preview

    This is the algorithm:

    $i=1;
    While ($i <= 12) {
    Switch ($i) {
    Case 1:
    Do Statement 1
    Case 2:
    Do Statement 2
    Case 3:
    Do Statement 3
    Case 4:
    Do Statement 4
    Case 5:
    Do Statement 5
    Case 6:
    Do Statement 6
    Case 7:
    Do Statement 7
    Case 8:
    Do Statement 8
    Case 9:
    Do Statement 9
    Case 10:
    Do Statement 10
    Case 11:
    Do Statement 11
    Case 12:
    Do Statement 12
    }
    $i = $i + 1;
    }

    The following is written in PHP:

    <?php
    $i=1;
    While ($i <= 12) {
    Switch ($i) {
    Case 1:
    echo nl2br("On the first day of Christmas, n");
    echo nl2br("my true love sent to me n");
    echo nl2br("A partridge in a pear tree. nn");
    break;
    Case 2:
    echo nl2br("On the second day of Christmas, n");
    echo nl2br("my true love sent to me n");
    echo nl2br("Two turtle doves, n");
    echo nl2br("And a partridge in a pear tree. nn ");
    break;
    Case 3:
    echo nl2br("On the third day of Christmas, n");
    echo nl2br("my true love sent to me n");
    echo nl2br("Three French hens, n ");
    echo nl2br("Two turtle doves, n ");
    echo nl2br("And a partridge in a pear tree. nn");
    break;
    Case 4:
    echo nl2br("On the fourth day of Christmas,n");
    echo nl2br("my true love sent to me n");
    echo nl2br("Four calling birds, n");
    echo ...

    Solution Summary

    This solution provides guidelines on how to write a program using repetition and switch structures.

    $2.19