Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.

Similar presentations


Presentation on theme: "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture."— Presentation transcript:

1 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture 8

2 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 2Winter Quarter IF If you can keep your head when all about you Are losing theirs and blaming it on you, If you can trust yourself when all men doubt you But make allowance for their doubting too, If you can wait and not be tired by waiting, Or being lied about, don't deal in lies, Or being hated, don't give way to hating, And yet don't look too good, nor talk too wise: THEN Yours is the Earth and everything that's in it, And--which is more--you'll be a Man, my son! --Rudyard Kipling

3 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 3Winter Quarter if / else if / else Selection Structures Selection structures permit alternate actions based on the evaluation of logical expressions. The logical expressions evaluate as either true or false, and the action takes place if and only if the expression is true. When the expression is false, the program may take alternate action(s), or it may evaluate another expression to determine what to do next.

4 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 4Winter Quarter if / else if / else Selection Structures A simple if structure is called a single-selection structure because it either selects or ignores a single action. The if / else structure is called a double-selection structure because it selects between two different actions. Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures.

5 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 5Winter Quarter if / else Selection Structures Syntax for the if selection structure is as follows: if ( this logical expression is true ) statement ; Syntax for the if / else selection structure is as follows: if ( this logical expression is true ) statement ; else statement ;

6 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 6Winter Quarter if / else Selection Structures A very simple program: #include int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a; else c = b; }

7 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 7Winter Quarter if / else Selection Structures The if selection structure is often written as: if ( this logical expression is true ) statement ; And, the if / else selection structure is often written as: if ( this logical expression is true ) statement ; else statement ; no semi-colon!

8 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 8Winter Quarter if / else Selection Structures Often, the earlier example is written this way: #include int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a ; else c = b ; }

9 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 9Winter Quarter if / else if / else Selection Structures Syntax for the if / else if / else selection structure is as follows: if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else statement ;

10 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 10Winter Quarter if / else if / else Selection Structures The actual syntax for the multiple if / else if / else selection structure is as follows: if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else statement ;

11 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 11Winter Quarter if / else if / else Selection Structures if ( this logical expression is true ) { Execute statements in this block ; } else if ( this logical expression is true ) { Execute statements in this block ; } else { Execute statements in this block ; }

12 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 12Winter Quarter Simple Program Using if / else if / else #include int main ( ) { int a, b ; printf ("Enter values for a and b > ") ; scanf ("%d%d", &a, &b ) ; if ( a < b ) printf ("a is less than\n") ; else if ( a == b ) printf (" a is equal to b\n") ; else printf ("a is larger than b\n") ; }

13 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 13Winter Quarter Grading Program Using if / else if / else /* This program associates letter grades with numeric test scores */ #include int main ( ) { int score ; printf ("enter your test score >") ; scanf ("%d", &score) ;

14 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 14Winter Quarter Grading Program Using if / else if / else if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score < 90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ; else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else printf ("Your score of %d is an E\n", score) ; }

15 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 15Winter Quarter A Little Help for Assignment G07 Assignment G07 requires us to make up (generate) a random number between 0 and 10. Often, computer system have routines already available for generating a sequence of pseudo- random numbers. For today, we can use one such utility function: int answer ; answer = rand ( ) % 11 ;

16 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 16Winter Quarter A Little Help for Assignment G07 But if we just use: answer = rand ( ) % 11 ; we'll get the same "random" value each time we run the program. So, how do we get something more "random". We must first "seed" the random number generator: srand ( some_random_number ) ; So where do we get "some_random_number" ?

17 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 17Winter Quarter A Little Help for Assignment G07 On a UNIX system, the time of day (expressed in seconds) is "sort of" a random number: srand ( time (NULL) ) ; Where do these functions "live"? –rand, srand functions are in –time function is in Check C Library resources

18 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 18Winter Quarter A Little Help for Assignment G07 How to control the game? (Use bottom test loop) int nguess, guess, answer ; /* seed random number generator here */ nguess = 0 ; answer = rand ( ) % 11 ; do { nguess++ ; /* game playing logic goes here */ } while ( guess != answer && nguess < 4 ) ;


Download ppt "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture."

Similar presentations


Ads by Google