| Name: _____________________ | Class: Comp 217 |
| SSN/ID: _____________________ | Section & Group: ____________ |
So far we learned three control structures: the sequence, the If statement, and the While statement. This lab will provide you more control structures. They are
Lab Exercise:NOTE: The Exercises will be marked in the lab.
Here is the syntax template for the Switch statement:
switch ( IntegralOrEnumExpression ) {
SwitchLabel ... Statement
.
.
.
}
IntegralOrEnumExpression is an expression of integral type — char,
short, int, long, bool or of enum type which will be
discussed later.
SwitchLabel
case ConstantExpression: default :In a case label, ConstantExpression is an integral or enum expression whose operands must be literal or named constants.
Now let's look at the following C++ code:
switch (grade)
{
case 'A': cout << "Great work. " << endl;
break;
case 'B': cout << "Good work. " << endl;
break;
case 'C': cout << "Passing work. " << endl;
break;
case 'D':
case 'F': cout << "Unsatisfictory work. " << endl;
cout << "See your instructor." << endl;
break;
default: cout << grade << " is not a legal grade." << endl;
break;
}
grade is the switch expression; the letters (also known as
case values) — such as A, B, C, D, F — beside the statements
make up the case labels.
The value in grade is compared with the value in each case label. When a match is found, the corresponding statement is executed. If the value of the switch expression does not match a value in any case label, the default label is matched by default.
Because execution continues after a match until break is encountered, both 'D' and 'F' send the same message to the screen. What would happen if we forgot to put break after the statement associated with case 'B'? Every time grade contained a B both "Good work!" and "Passing work!" would be printed.
do Statement while (Expression);The do-while statement is a looping statement. It tests the Boolean expression at the end of the loop. A statement (or a sequence of statements) is executed while an expression is true. The do-while statement differs from the while statement in one major respect: the body of the loop is always executed at least once in the do-while statement. For example, the following loop reads marks until a valid mark is entered. This do-while loop is event-controlled.
do {
cout << "Please enter a mark: ";
cin >> mark;
if (mark < 0 || mark > 100)
cout << "Invalid mark. Try again. " << endl << endl;
} while (mark < 0 || mark > 100);
The following C++ code shows a count-controlled do-while statement:
sum = 0;
counter = 0;
do {
cout << "Please enter a mark: ";
cin >> mark;
sum = sum + mark;
counter++;
} while (counter < 10);
cout << "The average of the marks entered is " << sum/counter << endl;
Back to Highlights
The syntax template of a for statement is:
for (InitStatement; Expression1; Expression2) StatementUsually, the InitStatement initializes a loop control variable. Expression2 increments or decrements the loop control variable. Expression1 is equivalent to a while condition.
The following for loop reads 10 marks and sums them up.
sum = 0;
for (counter = 1; counter <= 10; counter++) {
cout << "Please enter a mark: ";
cin >> mark;
sum = sum + mark;
}
The following for loop also reads 10 marks and sums them up.
sum = 0;
for (counter = 10; counter > 0; counter--) {
cout << "Please enter a mark: ";
cin >> mark;
sum = sum + mark;
}
Of course, all the for loops can be written as count-controlled while loops.
For example, we can change the previous program segment to the following
equivalent piece of code:
sum = 0;
counter = 1;
while (counter <= 10) {
cout << "Please enter a mark: ";
cin >> mark;
sum = sum + mark;
counter++;
}
/************************************************************
*
* FileName: case.cpp
* Author: Ricky J. Sethi
* Purpose: Demonstrate how to use Switch Statemenet.
*
**************************************************************/
#include <iostream>
using namespace std;
int main() {
char grade;
cout << "Please enter a character grade (A, B, C, D, or F): ";
cin >> grade;
switch (grade) {
case 'A': cout << "Great work. " << endl;
break;
case 'B': cout << "Good work. " << endl;
break;
case 'C': cout << "Passing work. " << endl;
break;
case 'D':
case 'F': cout << "Unsatisfictory work. " << endl;
cout << "See your instructor." << endl;
break;
default: cout << grade << " is not a legal grade." << endl;
break;
}
return 0;
}
// end program
|
The purpose of the program is to demonstrate how to use switch statement.
/***********************************************************
*
* FileName: loop.cpp
* Author: Ricky J. Sethi
* Purpose: Demonstrate how to use For loop and
* Do-while Statemenet. For loop will
* handle 10 valid marks and printout the
* average. A structure which has a loop
* inside another loop is called a nested loop.
*
**************************************************************/
#include <iostream>
using namespace std;
int main() {
int mark;
int sum;
char grade;
sum = 0;
for (int counter =1; counter <= 10; counter++) {
//write a do-while loop here to obtain a valid mark
//a valid mark means that the mark is greater than or equal to 0
//and less than or equal to 100.
sum = sum + mark;
if (mark >= 90)
grade = 'A';
else if (mark >= 80 && mark < 90)
grade = 'B';
else if (mark >= 70 && mark < 80)
grade = 'C';
else if (mark >= 50 && mark < 70)
grade = 'D';
else
grade = 'F';
cout << "The corresponding character grade is " << grade << endl;
}
cout << endl;
cout << "The average of the ten marks is " << sum/10 << endl;
return 0;
}
// end program
|
Hand In: This lab handout with the answers filled in attached
to a listing of your final exercises
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename> or a2ps <filename>).