90 likes | 194 Views
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.
E N D
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.
IF statements This slide show introduces the IF logic structure and describes how flowcharts are used to document if statements Vocabulary: algorithm annotation if else Christine S. Wolfe Ohio University Lancaster 2008-Aug-01
The IF logic structure forces the program to take one of 2 paths based on the logical value of an expression. The syntax of the if statement in ANSI C is if (logical (Boolean) expression) { statement(s) to execute if expression is true } else { statement(s) to execute if expression is false } Click Tip The else clause is optional In pseudocode the if statement is written as if (logical (Boolean) expression) statement(s) to execute if expression is true else statement(s) to execute if expression is false end if The else clause is optional
if logical exp if logical exp statements to execute if expression is true statements to execute if expression is false F T F T statements to execute if expression is false statements to execute if expression is true The diamond symbol is used to flowchart a logical expression. When used to diagram an if condition, there is exactly one inflow and two outflows. One of the outflows is labeled "T" and points to the next instruction to be executed whenever the condition is True. The other outflow is labeled "F" and points to the next instruction to be executed whenever the condition is False. it doesn't matter which outflow is T and which is F as long as they point to the appropriate instructions. Generally, programmers choose the layout that best fits on the paper. Click Tip Typically, the paths link up again after one of the alternates is executed.
x = 1; x = 20; x = 10; if (x > 10) { printf(“It’s True”); } else { printf(“It’s False”); } 10 1 20 X > 10 PRINT “It’s True” T F PRINT “It’s False” It’s True It’s False
Sometimes, you want the program to do something special when a condition is true but, there is nothing special to do when the condition is false. Because the else, or False path is optional, no statements are required along the F path. IF logical exp statements to execute if expression is true T F statements to execute if expression is false
Using an if statement to solve a problem: The customer interest problem. Calculate the interest charged on an invoice. The interest rate is determined by the customer's credit status as determined by the vendor as shown in the chart below: Example 1: Customer Bob has a credit status of A and orders $1000.00 of merchandise. The interest charged on his order is $50.00 (1000.00 * 0.05) Example 2: Customer Alan has a credit status of C and orders $500.00 of merchandise. The interest charged on his order is $60.00 (500.00 * 0.12)
The customer interest problem. The problem can be solved by using a series of IF statements. START GET CreditStatus GET TotalMerchandise if CreditStatus == 'A' CALCULATE Interest = TotalMerchandise * 0.05 end if if CreditStatus == 'B' CALCULATE Interest = TotalMerchandise * 0.10 end if if CreditStatus == 'C' CALCULATE Interest = TotalMerchandise * 0.12 end if PRINT Interest STOP
The customer interest problem. main() GET CreditStatus A B IF CreditStatus == 'B' IF CreditStatus == 'C' GET Total Merchandise N N Y Y IF CreditStatus == 'A' Interest = TotalMerchandise * 0.10 Interest = TotalMerchandise * 0.12 N Y Interest = TotalMerchandise * 0.05 B PRINT Interest A STOP