CHAPTER 3 - CALC IF STATEMENTS



AD

3.1 If-then-else

You will use If – Then statements often. The general form of the statement is:

If condition Then
true_instructions
Else
false_instructions
End if

The following program will prompt you with an Input Box for a number between 1 and 10. If the number is greater than 5, print Watch late news. Otherwise print Watch morning news. Notice the third line. The InputBox has the form: InputBox (“Prompt text”). The variable number is set equal to the value you type into the InputBox.

Sub Test8()
Dim number As Long
number = InputBox("Enter a number between 1 and 10.")
If number > 5 Then
   MsgBox "Watch late news."
Else
   MsgBox "Watch morning news."
End if
End Sub




3.2 If-then

Sometimes a very short If – Then statement is useful. It takes the form:

If condition Then
true-instructions
End if

The following program will prompt you with an InputBox for a number between 1 and 10. If the number is greater than 5 then the message box prints Watch late news. Otherwise, you go to End Sub.

Sub Test9()
Dim number As Long
number = InputBox ("Enter a number between 1 and 10.")
If number > 5 Then
   MsgBox "Watch late news."
End if
End Sub




3.3 Nested if-then-else

This construct makes use of nested If statements. It takes the general form:

If condition Then
statement
Else
  If condition Then
  statement
   Else
    If condition Then
    statement
     End if
  End if
End if

In the next subroutine an InputBox will ask you to enter a number between 1 and 10. If the number is less than 3 then a message box will say Watch morning news. If the number is equal to 3 then a message box will say Watch noon news. If the number is greater than 3 then a message box will say Watch late news.

Sub Test10()
Dim number As Long
number = InputBox ("Enter a number between 1 and 10.")
If number < 3 Then
MsgBox "Watch morning news."
Else
   If number = 3 Then
   MsgBox "Watch noon news."
   Else
      If number > 3 Then
      MsgBox "Watch late news."
      End if
     End if
   End if
End Sub




3.4 If-elseif-else

The If-ElseIf-Else structure exists because it is more efficient. In our preceding examples of If statements all the statements are read. In the If-ElseIf-Else structure the computer stops reading conditions when a true condition is met.

It takes the general form:

If condition Then
   true_instructions
ElseIf condition Then
   alternate_instructions
Else
   alternate_instructions
End If

This program duplicates the results of the Nested If – Then – Else program.

Sub Test11()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
If number < 3 Then
   MsgBox “Watch morning news.”
ElseIf number = 3 Then
   MsgBox “Watch noon news.”
Else
   MsgBox “Watch late news.”
End If
End Sub


3.5 Select case statements

A Select Case statement does not have If in it, but it acts like a nested If statement.

The Select Case statement is a third way of executing the program above. It is useful for choosing between three or more options.

It takes the following general form:

Select Case test_expression
Case Is test_expression condition
   instruction statement
Case Is test_expression condition
   instruction statement
Case Is test_expression condition
   instruction statement
Case Is test_expression condition
   instruction statement
End Select

The following program repeats the results of the two programs above.

Sub Test12()
Dim number As Long
number = InputBox (“Enter a number between 1 and 10.”)
Select Case number
Case Is < 3
   MsgBox “Watch morning news.”
Case Is = 3
   MsgBox “Watch noon news.”
Case Is > 3
   MsgBox “Watch late news.”
End Select
End Sub


theSurfDragon.com


Calc Navigation

Table of Contents
Ch1-Introduction
Ch2-Loops
Ch3-If Statements
Ch4-Functions
Ch5-Subs & Functions
Ch6-Read & Write
Ch7-Operators
Ch8-Built-in Functions
Ch9-Built-in Examples
Ch10-Debugging
Ch11-Running Subs
Ch12-Sample Programs
Ch13-WS Formulas
Ch14-WS Functions
Ch15-Calc Help Page