Conditional execution, If ... Then

To this point, the instructions in our programs have been executed sequentially. The computer executes the first line in a program, then the second, the third, etc. in order until it reaches the end.

The following event handler is from a program that illustrates conditional execution, in which the computer does not necessarily execute all of the instructions in order.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   txtOut.Clear()
   txtOut.AppendText("Roses are red." & vbCrLf)
   txtOut.AppendText("Violets are blue." & vbCrLf)
   If txtLS.Text = "long" Then       'add extra lines
      txtOut.AppendText("Trees are green." & vbCrLf)
      txtOut.AppendText("Mice are grey." & vbCrLf)
   End If
   txtOut.AppendText("Sugar is sweet." & vbCrLf)
   txtOut.AppendText("And so are you.")
End Sub

When you run this program, it only displays the middle lines of the poem if you type "long" into the TextBox named txtLS. If you type anything but "long", the computer skips the lines between If...Then and End If, so the middle lines are not displayed.

The conditional statement in this example has two parts, If...Then and End If. Its syntax is:
If <Boolean expression> Then

   one or more statements that
   are executed if the Boolean
   expression is true.

End If
When it comes to an If...Then statement, the computer first evaluates the Boolean expression. If it is false it skips the instructions between the If...Then and End If statements. In our example, that would mean the middle lines of the poem would not be displayed.

If the value of the Boolean expression is true, the instructions up to the End If statement are executed. Either way, the computer resumes sequential execution when it gets beyond the End If.

This is illustrated in the flowchart to the right. Note that the flowchart is written in terms of the problem and in English, not in Basic.

If you look back at the listing of the sub-program at the top of this note, you will see that the statements between If...Then and End If are indented. This is not necessary. Indenting those statements does not change their meaning, but it makes he program easier for a person to read and understand. Programmers call this sort of indenting pretty printing, and you should get in the habit of doing it.

Finally, it is legal to put If statements inside of If statements. This is called nesting the If statements. Nesting works in a logical, consistent manner, but it can be confusing to someone trying to read your program (and might make your brain explode). If you do nest If statements, be sure to pretty print the listing and thoroughly test your program.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.