1 / 15

Conditional Execution

Conditional Execution. If statement. So far, we have had sequential execution (everything is executed) Offers little flexibility May want code executed only when certain condition is met Conditional Execution Execute a statement or statements only if a certain condition is met Or not met.

Download Presentation

Conditional Execution

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. Conditional Execution

  2. If statement • So far, we have had sequential execution (everything is executed) • Offers little flexibility • May want code executed only when certain condition is met • Conditional Execution • Execute a statement or statements only if a certain condition is met • Or not met

  3. If statement • Consists of keyword if • Followed by condition in parenthesis • Followed by the body of the if statement • This is what is executed if the condition evaluates to true • Body can consist of multiple statements if they are enclosed with { }

  4. No Condition satisfied? Yes Program statement The if Statement • Format if ( condition ) program statement; or if ( condition ) { program statement(s); }

  5. No number < 0 Yes number = - number; Example • Calculating the absolute value of an integer • printf(“type in your number: “); • scanf(“%i”, &number); • if ( number < 0 ) • number = -number; • printf(“\nThe absolute value is %i\n”, number);

  6. Notes • Only multiply by -1 if the number is less than 0 • The if statement doesn’t end in semi-colon • The body does, but the if statement doesn’t • The if statement begins and ends with brace ( {…} ) • The body has one or more statements within the braces ( {…} ) • Can be considered a compound statement since the body can have multiple statements

  7. If - else • Sometimes have two sets of statements and only want to execute one based on a conditional test • Definitely want to execute one or the other, but not both • If the body of the if statement is skipped (due to the condition evaluating to false) the body of the else statement is executed • Consists of key word if • Followed by condition in parenthesis • Followed by the body of the if • Followed by key word else • Followed by condition in parenthesis • Followed by the body of the else

  8. No Condition satisfied? Program statement 2 Yes Program statement 1 The if-else Statement • Format if ( condition ) program statement 1; else program statement 2; • Or if ( condition ) { program statement(s) 1; } else { program statement(s) 2; }

  9. No “Gator Bait" Yes “Go Gators!” The if-else Statement - Example Are you a gator? answer == ‘y’ • printf(“Are you a gator? (y)es, (n)o “); • scanf(“%c”, &answer); • if ( answer == ‘y’) • printf(“Go Gators!!!\n”); • else • printf(“Then you’re gator bait”);

  10. Example Are you a gator? (y)es, (n)o y Go Gators!!! • printf(“Are you a gator? (y)es, (n)o “); • scanf(“%c”, &answer); • if ( answer == ‘y’) • printf(“Go Gators!!!\n”); • else • printf(“Then you’re gator bait”); Are you a gator? (y)es, (n)o n Then you’re gator bait! Are you a gator? (y)es, (n)o Y Then you’re gator bait!

  11. else if • Seen two statements to facilitate conditional execution • if statement • selectively executes a set of statments based on a conditional test. • if..else • allows us to make one of two choices • if the condition is true, the statements in the body of if is executed, • otherwise the statements in the body of the else part are automatically executed. • Sometimes need to choose between multiple alternatives • if … else if … else if … else if … else

  12. Condition 1 satisfied? No No Condition 2 satisfied? Program statement 2 Yes Yes Program statement 1 The else if Statement • Format if ( condition1 ) program statement 1; else if (condition2) program statement 2; • Flow

  13. Consider assigning letter grades • Have a floating point number between 0 and 100. • Want to assign corresponding letter grade to number grade, x • A – if 90 ≤ x ≤100 • B – if 80 ≤ x < 90 • C – if 70 ≤ x < 80 • D – if 60 ≤ x < 70 • E – x < 60

  14. No X ≥ 90 Yes No X ≥ 80 letGrade = ‘A’ Yes No X ≥ 70 letGrade = ‘B’ Yes No X ≥ 60 letGrade = ‘C’ Yes letGrade = ‘E’ letGrade = ‘D’ An example

  15. Assigning Letter Grades if ( x >= 90 ) letGrade = ‘A’; else if (x >= 80) letGrade = ‘B’; else if (x >= 70) letGrade = ‘C’; else if (x >= 60) letGrade = ‘D’; else letGrade = ‘E’

More Related