150 likes | 233 Vues
Learn about if statements, if-else statements, and else if statements for conditional execution in programming to control the flow of code based on specific conditions. Master the basics and examples provided.
E N D
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
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 { }
No Condition satisfied? Yes Program statement The if Statement • Format if ( condition ) program statement; or if ( condition ) { program statement(s); }
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);
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
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
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; }
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”);
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!
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
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
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
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
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’