Name: _____________________ Class: Comp 217
SSN/ID:   _____________________ Section & Group: ____________
Additional Control Structures


Highlights of this lab:

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.


Back to Highlights

A Switch Statement

The switch statement is a selection statement that can be used instead of a series of if-then-else statements. Alternative statements are listed with a switch label in front of each. A switch label is either a case label or the word default. A case label is the word case followed by a constant expression. An integral expression is called a switch expression and is used to match one of the values on the case labels. The statement associated with the value that is matched is the statement that is executed. Execution then continues sequentially from the matched label until the end of the switch statement is encountered or a break statement is encountered.

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-while loop

The syntax template for the do-while statement is this:
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

For Loops

The for statement is a looping structure designed specifically to simplify the implementation of count-controlled loops (i.e., a count-controlled while loop can be replaced by a for loop.). The loop-control variable, the beginning value, the ending value, and the incrementation are explicitly part of the for loop heading itself.

The syntax template of a for statement is:

for (InitStatement; Expression1; Expression2)
	Statement
Usually, 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++;
}

Back to Highlights


Lab Exercise — Additional Control Structures

Exercise 1: (This exercise will be marked in the lab.)

Exercise 2: (This exercise will be marked in the lab.)

Exercise 3: (This exercise will be marked in the lab.)

Back to Highlights

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>).


Ricky J. Sethi <rickys at sethi.org>
Last modified: Wed Aug 17 14:43:59 PDT 2005