201Spring08-L9 - CMPSC 201C Spring 2008 Lecture 9 February...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 9 February 4, 2008 The number of branches is not limited to two. One method to allow for multiple branches is the if/else if structure. if (condition 1) { statement(s) } else if (condition 2) { statement(s) } else // default or trailing no if! { statement(s) } Multi-branched Structures Evaluating Multi-way Branches The first condition is tested. If the first condition evaluates to true, then the statements associated with the first condition are executed and control exits the branch structure. If first condition evaluates to false, then the second condition is tested As soon as the statements associated with a true condition are executed, the branch structure is exited. When none of the ifs evaluated to true, the statements associated with the trailing else (when there is one) are executed. Grade Example (cont) if (percent >= 90) { grade = `A'; } else if (percent >= 80) // only executed 1st if was false { grade = `B'; } else if (percent >= 70) // only executed prev. ifs false { grade = `C'; } else if (percent >= 60) // only executed prev. ifs false { grade = `D'; } else // only executed all ifs false--default { grade = `F' } Questions Nesting Ifs Can build ifs (if/else-ifs) inside one another. if (condition 1) { statement(s) if (condition 2) { statement(s) else { statement(s) } } else { statement(s) } } Proper indentation is very important for the person reviewing the program. Example Suppose I had two types of employees. One type was exempt from earning overtime pay, but earned "comp" hours instead. The other type could earn overtime pay (non exempt), but not comp hours. Algorithm: check to see number of hours worked if hours greater than 40 then check status of employee if status is exempt calculate comp hours, otherwise calculate overtime pay if (hoursworked > 40) { if ((status == `E') || (status == `e')) { comphours = hoursworked 40; overpay = 0 ; //ineligible for overtime } else // assuming "double time" { overpay = 2*(hoursworked-40)*rate; comphours = 0; //ineligible for comp hours } gross = 40 * rate + overpay; else { gross = hoursworked * rate; } Another Example Suppose I had two grading scales for students based on whether they were "honor" students. honor regular lettergrade >=950 >= 900 A 880-949 800-899 B 800-879 700-799 C 720-800 600-700 D <720 <600 F if ((status == `R' || (status == `r')) { if (grade >= 900) lettergrade = `A'; else if (grade >= 800) lettergrade = `B'; else if (grade >= 700) lettergrade = `C'; else if (grade >= 600) lettergrade = `D'; else lettergrade = `F'; } else { if (grade >= 950) lettergrade = `A'; else if (grade>=880) lettergrade = `B'; else if (grade>=800) lettergrade = `C'; else if (grade >= 720) lettergrade = `D'; else lettergrade = `F'; } Caution Be careful of nested ifs when you do not use braces. C++ automatically matches an else with the closest if, which may not be what you want. The "fix" is to use the braces or to use an empty statement ( ; ) for the body of the else of the inside if. Questions???? Comparing Floats Because of how floating point numbers are stored in memory, unpredictable results may occur when trying to determine equality. Stick to using greater than or less than operators and use logical operator to determine a range. Example suppose you thought the round-off error to cause a 0.0000003 difference in the value (x == 4.5673675) (x > 4.5673671) && (x < 4.5672679) Comparing C-Strings C-strings cannot be compared using relational operators, but instead the strcmp( ) function (requires cstring library file). Has the general format strcmp(str1, str2) Compares each character by subtracting the ASCII value of str2 character from str1 character from the beginning of the strings until a difference is found. Returns a zero if strings are the same, a negative number is str1 is less than str2, or a positive number if str1 > str2. Caution Remember that C++ considers a 0 to be false and any other integer to be true. Therefore the expression if (strcmp(str1, str2)) cout<<"The words are the same\n"; would produce the wrong effect. if (!strcmp(str1, str2)) cout<<"The words are the same\n"; Questions ...
View Full Document

This note was uploaded on 03/19/2008 for the course CMPSC 201 taught by Professor Susanquick during the Spring '08 term at Pennsylvania State University, University Park.

Ask a homework question - tutors are online