If

From BR Wiki
Revision as of 13:29, 4 February 2012 by Mikhail.zheleznov (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

IF... ELSE IF Statement

IF... ELSE IF... ELSE Statement