cpts121-5-1

# cpts121-5-1 - CptS 121 Fall 09 Lecture 5-1 HK Chapter 4:...

This preview shows pages 1–5. Sign up to view the full content.

1 CptS 121 L5-1 – 9/21/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 5-1 HK Chapter 4: Selection Structures (cont.) Lecture Outline I. Nested if statements II. Another example

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 CptS 121 L5-1 – 9/21/09 Prof. Chris Hundhausen Nested if statements Consider the following scenario: A high school baseball team awards merit points to players based on their offensive performance. A single (encoded 's') is worth 1 point, a double (encoded 'd') is worth 2 points, a triple (encoded 't') is worth 3 points, and a home run (encoded 'h') is worth 4 points. Any at-bat that leads to an out (encoded 'o') worth 0 points. Write a C statement that, given an at-bat character, properly awards points.
3 CptS 121 L5-1 – 9/21/09 Prof. Chris Hundhausen Nested if statements (cont.) We can write a nested if statement to handle this situation: char at_bat; int points; printf("Enter the at-bat code (s,d,t,h,o): "); scanf(" %c",&at_bat); if (at_bat == 's') /* single */ { points = 1; } else if (at_bat == 'd') /* double */ { points = 2; } else if (at_bat == 't') /* triple */ { points = 3; } else if (at_bat == 'h') /* home run */ { points = 4; } else /* out */ { points = 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 CptS 121 L5-1 – 9/21/09 Prof. Chris Hundhausen Nested if statements (cont.) Consider the following updated scenario: A high school baseball team awards merit points to players based on their offensive performance and the class standing ('f' = freshman, 'o' = sophomore, 'j' = junior, and 's' = senior). In particular, freshmen and
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/25/2011 for the course CPTS 121 taught by Professor Hundhaus during the Spring '09 term at Washington State University .

### Page1 / 12

cpts121-5-1 - CptS 121 Fall 09 Lecture 5-1 HK Chapter 4:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online