1 / 18

Control Statements

Control Statements. Lecture 8. IF.

gloria
Télécharger la présentation

Control Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Statements Lecture 8 Winter Quarter

  2. IF If you can keep your head when all about youAre losing theirs and blaming it on you,If you can trust yourself when all men doubt youBut 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 Winter Quarter

  3. 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. Winter Quarter

  4. 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. Winter Quarter

  5. 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 ; Winter Quarter

  6. if / else Selection Structures • A very simple program: #include <stdio.h> int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a; else c = b; } Winter Quarter

  7. no semi-colon! 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 ; Winter Quarter

  8. if / else Selection Structures • Often, the earlier example is written this way: #include <stdio.h> int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a ; else c = b ; } Winter Quarter

  9. 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 ; Winter Quarter

  10. 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 ; Winter Quarter

  11. 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 ; } Winter Quarter

  12. Simple Program Using if / else if / else #include <stdio.h> 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") ; } Winter Quarter

  13. Grading Program Using if / else if / else /* This program associates letter grades with numeric test scores */ #include <stdio.h> int main ( ) { int score ; printf ("enter your test score >") ; scanf ("%d", &score) ; Winter Quarter

  14. 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) ; } Winter Quarter

  15. 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 ; Winter Quarter

  16. 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" ? Winter Quarter

  17. 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 <stdlib.h> • time function is in <time.h> • Check C Library resources Winter Quarter

  18. 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 ) ; Winter Quarter

More Related