CprS 157 Schedule
CprS 157 Class 4
CprS 157 Class 6

### Selection Control Structures Chapter 5 - Class 5

• Flow of Control
• relational operators
logical operators
precedence rules

• Selection Control Statements
• if...then... form of the if statement
if...then...else... form of the if statement
nested if statements
dangling else

• testing the state of an I/O stream
• if (!inFile)
{
cout << "error opening file " << endl;
return 1;
}

• typedef statement
• Boolean datatype

• error examples

•
Identify the errors in each of the following code fragments:
`if ( 0 < x < 100)`
`    cout << "valid" << endl;`
`if (x=7)`
`    amt = x * 2;`
`if (j > 6);`
`    j ++;`
`if (count > 0)`
`    sum = sum / count;`
`    cout << "the sum after dividing by count is " << sum << endl;`
`if (total > 0)`
`    if (total > 1000)`
`        cout << "over 1000 " << endl;`
`else`
`    cout << "a positive value lower than 1000" << endl;`

• Good test cases:
• test each path through program
test each type of input error
test boundary values (large, small)
test for zero or blank input values, negatives
test whether program terminates correctly

notices.cpp

// Notices program
// This program determines (1) a student's average based on three
// test scores and (2) the student's passing/failing status

Program notices.cpp illustrates the use of a conditional statement to direct the flow of control in a program. Pay particular attention to the structure of the nested if clauses.

### Programming Exercises

• Programming Warm-up Exercises (pages 268-270)
• 5.3  (Boolean variables)
5.7  (organizing test cases)
5.10  (slope of a line)
5.12 (sanity check)

Solve these exercises in class to your own satisfaction. Demonstrate your solutions to the instructor or to the class TA. We may save a few particularly instructive examples in the folder "Student Work" in the CprS 157 class folder on the Academic Computing file server.

### Homework For Chapter 5

• Programming Problems 5.3 and 5.4, page 272 (The Date of Easter Sunday)

•
Today's programming project implements an algorithm for calculating the day of Easter for any year between 1900 and 2099. It turns out that a non-intuitive but straightforward numerical calculation will produce an accurate answer in all but four exceptional cases, and we can make a correction in each of those four cases. Write clear and correct solutions to these programming problems, and make sure that your program knows that the day after March 31 is April 1. Drop an electronic copy of your work into the CS157 drop box before the beginning of our next class. Personalize the results of your efforts by using our standard file-naming conventions.