Explore BrainMass
Share

Repetition and Switch Structures

This content was STOLEN 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 24, 2018, 10:51 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
See Also This Related BrainMass Solution

Repetitive control structures, Techniques of passing arguments

1. Discuss various counter controlled repetitive control structures.

2. The if...else is suitable for simple program but switch...case is a better construct, which is suitable for more complex problems? Do you agree with this or not? Discuss with suitable examples.

3. We can replace any code that uses a for loop with a while loop. Would not it be simpler to get rid of the for loop and use the while for everything? Do you think this would be a good idea? Why or why not?

4. Write a program using equality and assignment operator with their symbol. Which type of error is generated, using them incorrectly? Discuss the results.

5. Write the example that demonstrates different techniques of passing arguments: by value, by reference, and as pointers. Discuss the three methods.

View Full Posting Details