Download presentation
Presentation is loading. Please wait.
1
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.
2
IF statements This slide show introduces the IF logic structure and describes how flowcharts are used to document if statements Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Vocabulary: algorithm annotation if else
3
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 The IF logic structure forces the program to take one of 2 paths based on the logical value of an expression. Click Tip 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 }
4
if logical exp statements to execute if expression is true statements to execute if expression is false T F 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. if logical exp statements to execute if expression is false statements to execute if expression is true F T Typically, the paths link up again after one of the alternates is executed. Click Tip 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.
5
5 X > 10 T F PRINT “It’s True” PRINT “It’s False” x = 1; if (x > 10) { printf(“It’s True”); } else { printf(“It’s False”); } It’s TrueIt’s False x = 20;x = 10; 12010
6
IF logical exp statements to execute if expression is true statements to execute if expression is false T F 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.
7
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: Credit StatusInterest Rate A5 % B10% C12% 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)
8
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
9
main() IF CreditStatus == 'A' Interest = TotalMerchandise * 0.05 GET CreditStatus GET Total Merchandise PRINT Interest STOP A IF CreditStatus == 'B' Interest = TotalMerchandise * 0.10 IF CreditStatus == 'C' Interest = TotalMerchandise * 0.12 N Y B N Y A N Y B
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.