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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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)
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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)
Background image of page 5

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

View Full DocumentRight Arrow Icon
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)
Background image of page 6
7 CptS 121 L4-2 – 9/16/09 Prof. Chris Hundhausen Conditions (cont.) Operator precedence (cont.) When in doubt, parenthesize! -
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 / 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 Right Arrow Icon
Ask a homework question - tutors are online