Chapter 3- Data Types and Operators Boolean Expressions Page 2 3 4

Introduction

The If Statement Page 2 3 4 5 6 7 8 9
Arithmetic Operations Page 2 3 4 5 6 Boolean Operators and Nested If Statements Page 2 3 4 5 6 7
Events and Sequential Processing Page 2 3 4 5 More Examples Page 2 3 4 5 6 7 8 9 10 11 12
Datatypes and Conversions Page 2 3 4 5 6 7 Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10
Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Exercises Page 2 3 4 5 6 7 8
Chapter 4- Selection Statements Review Questions
Introduction

4-3: If Statements

Introduction

The boolean expressions we have seen in the previous section yield values that are of boolean data type. When you use a boolean expression you must do something with the value resulting from the expression. In Exercise 3-1 you assigned the value to a variable that had been declared as boolean data type. Assigning the value resulting from an expression to a variable (i.e. storing the value in the computers memory) is one thing you might do with the result of an expression.

However, it is equally common that the value from a boolean expression is directly used in what is called an If statement to make a decision about which statements to execute next. We'll examine that now.

If Statement Syntax

If statements are used to select whether or not a set of statements are executed. The simplest kind of If statement, called a block If statement, is

other statements in subprogram
...
If (booleanValue) Then
statement1
statement2
...
End If
more statements
...

The booleanValue is often the result of a boolean expression, but may also be a boolean variable (which of course simply represents a boolean value). The statements must be on a new line following the Then keyword. An example, drawn from everyday experience might be

enter shop
look around
find item
If (itemPrice < cashInPocket) Then
buy item
End If
leave shop

This example has some statements before the If statement that are executed in sequence. The selection is determined by the result of the boolean expression
itemPrice < cashInPocket
If it is true the statement buy item is executed. If the boolean expression is false the statement is not executed. Thus the statement buy item might or might not be executed depending on the value of the boolean expression, but in either case the last statement leave shop will be executed.