If: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
Line 10: Line 10:


===Syntax===
===Syntax===
  IF [NOT] <conditional expression> [{AND|OR}...] THEN [<statement>][ELSE <statement>]
  IF [NOT] <[[conditional expression]]> [{[[AND]]|OR}...] THEN [<statement>][[[ELSE]] <statement>]


[[file:if.png|800px]]
[[file:if.png|800px]]

Revision as of 14:41, 5 May 2014

The If statement evaluates a relational expression. If the expression is true, it follows the THEN path and executes one or more specified statements or transfers control to another line, as indicated in the instructions. If it is false, it follows the ELSE path, which may specify another statement, transfer control to another line, or initiate another IF statement.

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

Syntax

IF [NOT] <conditional expression> [{AND|OR}...] THEN [<statement>][[[ELSE]] <statement>]

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.

Below is the general syntax used in a multi-line if statement:

IF condition THEN
   [body]
END IF

The optional body between IF condition THEN and END IF is a group of zero or more statements. At least one statement in the body is encouraged for clarity.

Although it is not required to indent the body of a multi-line IF statement, it is highly recommended to indent it for readability.

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 end of 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

Complex Conditions

Often in programs, the execution of a statement depends on several different conditions. For example, you may want REQUIRE the user to enter a name, a date of birth, and a social security number before saving a record.

00010 dim name$*40, dob$*8, ssn$*9
00020 get_data:!
00030 input name$, dob$, ssn$
00040 if trim$(name$)="" or trim$(dob$)="" or trim$(ssn$)="" then
00050    print "Enter ALL THREE fields before saving"
00060    goto get_data$
00070 end if

To see more information on operations see Operations.

Nested if statements

Sometimes you may need to embed if statements into other if statements. These embedded if statements are formerly known as nested if statements. Below is an example:

00020    PRINT "Enter employee type"
00030    PRINT "1 - Manager"
00040    PRINT "2 - Hourly"
00050    PRINT "3 - Commission"
00060    INPUT PAYCODE
00070    IF PAYCODE = 1 THEN ! Manager
00080       PRINT "Manager Selected."
00090       PRINT "Enter Weekly Salary "
00100       INPUT SALARY
00110       if salary => 0 then
00120          PRINT "Manager's pay is " & STR$(SALARY)
00130       else
00140          print "Can't have a negative salary"
00150       end if
00160    ELSE IF PAYCODE = 2 THEN ! Hourly Worker
00170       PRINT "Hourly worker Selected."
00180       PRINT "Enter the hourly salary "
00190       INPUT WAGE
00200       PRINT "Enter the total hours worked "
00210       INPUT HOURS
00220       if hours < 0 then
00230          print "Can't have negative hours"
00240       else if HOURS <= 40 THEN
00250          LET SALARY = HOURS * WAGE
00260       ELSE
00270          LET SALARY = 40.0 * WAGE + ( HOURS - 40 ) * WAGE * 1.5
00280       END IF
00290       PRINT "Hourly worker's pay is " & STR$(SALARY)
00300    ELSE IF PAYCODE = 3 THEN ! Commission Worker
00310       PRINT "Commission Worker Selected."
00320       PRINT "Enter gross weekly sales "
00330       INPUT SALES
00340       if sales > 0 then
00350          LET SALARY = SALES * .057 + 250
00360          PRINT "Commission worker's pay is " & STR$(SALARY)
00370       else
00380          print "Can't have negative sales"
00390       end if
00400    END IF

If there are several levels of indentation, each level should be indented the same additional amount of space.