December 03, 2020
SAMPLE SOURCE CODE FOR SWITCH STATEMENT
// This program demonstrates the use of a switch statement.
// The program simply tells the user what character they entered.
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
switch (choice)
{
case 'A': cout << "You entered A.\n";
break;
case 'B': cout << "You entered B.\n";
break;
case 'C': cout << "You entered C.\n";
break;
default: cout << "You did not enter A, B, or C!\n";
}
return 0;
}
// This program demonstrates how a switch statement
// works if there are no break statements.
#include <iostream>
using namespace std;
int main()
{
char choice;
cout << "Enter A, B, or C: ";
cin >> choice;
// The following switch statement is missing its break statements!
switch (choice)
{
case 'A': cout << "You entered A.\n";
case 'B': cout << "You entered B.\n";
case 'C': cout << "You entered C.\n";
default : cout << "You did not enter A, B, or C!\n";
}
return 0;
}
// This program is carefully constructed to use the "fall through"
// feature of the switch statement.
#include <iostream>
using namespace std;
int main()
{
int modelNum;
// Display available models and get the user's choice
cout << "Our TVs come in three models:\n";
cout << "The 100, 200, and 300. Which do you want? ";
cin >> modelNum;
// Display the features of the selected model
cout << "That model has the following features:\n";
switch (modelNum)
{
case 300: cout << " Picture-in-a-picture\n";
case 200: cout << " Stereo sound\n";
case 100: cout << " Remote control\n";
break;
default : cout << "You can only choose the 100, 200, or 300.\n ";
}
return 0;
}
// The switch statement in this program uses the "fall through" feature
// to catch both uppercase and lowercase letters entered by the user.
#include <iostream>
using namespace std;
int main()
{
char feedGrade;
// Get the desired grade of feed
cout << "Our dog food is available in three grades:\n";
cout << "A, B, and C. Which do you want pricing for? ";
cin >> feedGrade;
// Find and display the price
switch (feedGrade)
{
case 'a':
case 'A': cout << "30 cents per pound.\n";
break;
case 'b':
case 'B': cout << "20 cents per pound.\n";
break;
case 'c':
case 'C': cout << "15 cents per pound.\n";
break;
default : cout << "That is an invalid choice.\n";
}
return 0;
}
// This program demonstrates the ++ and -- operators.
#include <iostream>
using namespace std;
int main()
{
int num = 4; // num starts out with 4
// Display the value in num
cout << "The variable num is " << num << endl;
cout << "I will now increment num.\n\n";
// Use postfix ++ to increment num
num++;
cout << "Now the variable num is " << num << endl;
cout << "I will increment num again.\n\n";
// Use prefix ++ to increment num
++num;
cout << "Now the variable num is " << num << endl;
cout << "I will now decrement num.\n\n";
// Use postfix -- to decrement num
num--;
cout << "Now the variable num is " << num << endl;
cout << "I will decrement num again.\n\n";
// Use prefix -- to increment num
--num;
cout << "Now the variable num is " << num << endl;
return 0;
}
SAMPLE SOURCE CODE FOR WHILE STATEMENT
// This program demonstrates a simple while loop.
#include <iostream>
using namespace std;
int main()
{
int number = 1;
while (number <= 5)
{
cout << "Hello ";
number++;
}
cout << "\nThat's all!\n";
return 0;
}
// This program uses a while loop to display the numbers 1-5
// and their squares.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int num = 1;
cout << "Number Square\n";
cout << "--------------\n";
while (num <= 5)
{
cout << setw(4) << num << setw(7) << (num * num) << endl;
num++; // Increment counter
}
return 0;
}
// This program displays integer numbers and their squares, beginning
// with one and ending with whatever number the user requests.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int num, // Counter telling what number to square
lastNum; // The final integer value to be squared
// Get and validate the last number in the table
cout << "This program will display a table of integer\n"
<< "numbers and their squares, starting with 1.\n"
<< "What should the last number be?\n"
<< "Enter an integer between 2 and 10: ";
cin >> lastNum;
while ((lastNum < 2) || (lastNum > 10))
{
cout << "Please enter an integer between 2 and 10: ";
cin >> lastNum;
}
// Display the table
cout << "\nNumber Square\n";
cout << "--------------\n";
num = 1; // Set the counter to the starting value
while (num <= lastNum)
{
cout << setw(4) << num << setw(7) << (num * num) << endl;
num++; // Increment the counter
}
return 0;
}
SAMPLE SOURCE CODE FOR DO-WHILE STATEMENT
// This program averages 3 test scores. It uses a do-while loop
// that allows the code to repeat as many times as the user wishes.
#include <iostream>
using namespace std;
int main()
{
int score1, score2, score3; // Three test scores
double average; // Average test score
char again; // Loop again? Y or N
do
{ // Get three test scores
cout << "\nEnter 3 scores and I will average them: ";
cin >> score1 >> score2 >> score3;
// Calculate and display the average
average = (score1 + score2 + score3) / 3.0;
cout << "The average is " << average << ".\n";
// Does the user want to average another set?
cout << "Do you want to average another set? (Y/N) ";
cin >> again;
} while ((again == 'Y') || (again == 'y'));
return 0;
}
SAMPLE SOURCE CODE FOR FOR STATEMENT
// This program uses a for loop to display the numbers 1-5
// and their squares.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int num;
cout << "Number Square\n";
cout << "--------------\n";
for (num = 1; num <= 5; num++)
cout << setw(4) << num << setw(7) << (num * num) << endl;
return 0;
}
// This program takes daily sales figures over a period of time
// and calculates their total.It then uses this total to compute
// the average daily sales.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int numDays; // Number of days
double dailySales, // The sales amount for a single day
totalSales = 0.0, // Accumulator, initialized with 0
averageSales; // The average daily sales amount
// Get the number of days
cout << "For how many days do you have sales figures? ";
cin >> numDays;
// Get the sales for each day and accumulate a total
for (int day = 1; day <= numDays; day++) // day is the counter
{
cout << "Enter the sales for day " << day << ": ";
cin >> dailySales;
totalSales += dailySales; // Accumulate the running total
}
// Compute the average daily sales
averageSales = totalSales / numDays;
// Display the total sales and average daily sales
cout << fixed << showpoint << setprecision(2);
cout << "\nTotal sales: Php" << setw(8) << totalSales;
cout << "\nAverage daily sales: Php" << setw(8) << averageSales
<< endl;
return 0;
}