If: Difference between revisions
No edit summary |
No edit summary |
||
Line 94: | Line 94: | ||
00150 end if | 00150 end if | ||
00160 stop | 00160 stop | ||
===Complex Conditions=== | |||
<noinclude> | <noinclude> | ||
[[Category:Control Structures]] | [[Category:Control Structures]] | ||
</noinclude> | </noinclude> |
Revision as of 14:16, 4 February 2012
IF Statement
if condition then statement
Applications use selection statements to choose among alternative courses of action.
For example, suppose that the passing grade on an exam is 60. The BR statement below determines whether the condition grade >= 60 is true or false. If the condition is true, "Passed" is displayed, and the next BR statement in order is performed. If the condition is false, the output statement is ignored, and the next BR statement in order is performed.
00010 print "Enter a grade" 00020 input grade 00030 if grade >= 60 then print "Passed" 00040 stop
Multi-line IF Statements
The same program could be rewritten to put the print "Passed" statement on a separate line with indentation to emphasize that this statement may or may not happen:
00010 print "Enter a grade" 00020 input grade 00030 if grade >= 60 then 00040 print "Passed" 00050 end if 00060 stop
In the above example, end if marks the end of the statements which will be performed if the grade >= 60 condition is true. The indentation of line 40 of this selection statement is optional, but recommended, because it emphasizes the inherent structure of the if statement.
Programmers often run into situations in which more than one statement needs to be executed if a particular condition is true. To illustrate the method of achieving this in BR, we will expand the above example to print multiple statements if the grade entered by the user is more than or equal to 60:
00010 print "Enter a grade" 00020 input grade 00030 if grade >= 60 then 00040 print "Passed" 00050 print "You will not need to re-take the test" 00060 print "Good job" 00070 end if 00080 stop
IF... ELSE Statement
When your programming needs call for two different actions depending on whether or not a condition is true, the IF... ELSE statement should be used.
00010 print "Enter a grade" 00020 input grade 00030 if grade >= 60 then 00040 print "Passed" 00050 else 00060 print "Failed" 00070 end if 00080 stop
IF... ELSE IF Statement
When your programming needs call for many different actions depending on multiple conditions, the IF... ELSE IF statement should be used.
00010 print "Enter a grade" 00020 input grade 00030 if grade >= 90 AND grade <= 100 then 00040 print "Got an A" 00050 else if grade >= 80 AND grade <= 89 then 00060 print "Got a B" 00070 else if grade >= 70 AND grade <= 79 then 00080 print "Got a C" 00090 else if grade >= 60 AND grade <= 69 then 00100 print "Got a D" 00110 else if grade < 60 AND grade > 0 then 00120 print "Failed" 00130 end if 00140 stop
IF... ELSE IF... ELSE Statement
The program above covers all scenarios except two:
1. When the grade entered by the user is negative 2. When the grade entered by the user is greater than 100
In order to catch this problem, you may add an ELSE clause to the program above, resulting in:
00010 print "Enter a grade" 00020 input grade 00030 if grade >= 90 AND grade <= 100 then 00040 print "Got an A" 00050 else if grade >= 80 AND grade <= 89 then 00060 print "Got a B" 00070 else if grade >= 70 AND grade <= 79 then 00080 print "Got a C" 00090 else if grade >= 60 AND grade <= 69 then 00100 print "Got a D" 00110 else if grade < 60 AND grade > 0 then 00120 print "Failed" 00130 else 00140 print "Invalid grade. Must be positive and less than 100" 00150 end if 00160 stop