cpts121-4-2

# cpts121-4-2 - CptS 121 Fall 09 Lecture 4-2 HK Chapter 4...

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

1 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen CptS 121 Fall ‘09 Lecture 4-2 HK Chapter 4: Selection Structures Lecture Outline I. Control Structures II. Conditions III. if statement

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

View Full Document
2 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Control Structures Recall that algorithms are composed of three different kinds of statements: Sequence : the ability to execute a series of instructions, one after the other. Conditional : the ability to execute an instruction contingent upon some condition. Iteration : the ability to execute one or more instructions repeatedly. We'll now learn about conditionals: the ability to execute some code IF some condition is true.
3 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Conditions Conditional statements rely on a Boolean condition, which evaluates to either true or false In C, the true and false values are actually represented numerically: False: 0 True: any number except 0 (usually 1) Relational operators are used to build Boolean conditions: < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), != (not equal to)

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

View Full Document
4 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Conditions (cont.) Examples Assume x = 3, y = 4, max = 100, min = 0, and ch = 'c' Then what do the following evaluate to? x <= 0 x == y max >= min ch < 'a' max min != 0 max == 99 + 1
5 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Conditions (cont.) Logical operators We can combine relational operators with logical operators to construct general Boolean expressions in C: - AND: (A single '&' is different.) - OR: || (A single '|' is different.) - NOT: ! Examples - Assume that temp = 50 , MAX_TEMP = 90 , precip = 2.0 , num_votes = 20 , votes_needed = 20 , and elected = 0 ; - Then evaluate these: (temp < MAX_TEMP) && (precip > 0)

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

View Full Document
6 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Conditions (cont.) Operator precedence Just like numeric operators (+, -, /, *), logical operators have precedence rules that determine order of evaluation From highest to lowest, the precedences are as follows: [ Most are left-to-right; but not assignment.] function calls (highest) (unary operators) *, /, % +, - <, <=, >=, > ==, != || = (assignment) (lowest)
7 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Conditions (cont.) Operator precedence (cont.) When in doubt, parenthesize! -

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

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

{[ snackBarMessage ]}

### Page1 / 23

cpts121-4-2 - CptS 121 Fall 09 Lecture 4-2 HK Chapter 4...

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

View Full Document
Ask a homework question - tutors are online