Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

4.5 If...Then Selection Statement

A selection statement chooses among alternative courses of action. Suppose that the passing grade on an examination is 60 (out of 100). Then the pseudocode statement

   If student's grade is greater than or equal to 60 then
   Display "Passed"

determines whether the condition "student's grade is greater than or equal to 60" is true or false. If the condition is true, then "Passed" is displayed, and the next pseudocode statement in order is "performed" (remember that pseudocode is not a real programming language). If the condition is false, the display statement is ignored, and the next pseudocode statement in order is "performed."

The preceding pseudocode If statement may be written in Visual Basic as

If studentGrade >= 60 Then
   resultLabel.Text = "Passed" ' display "Passed"
End If

The code corresponds closely to the pseudocode, showing the usefulness of pseudocode as a program-development tool. The statement in the body of the If...Then statement displays the string "Passed" on resultLabel.


The compiler ignores whitespace, such as spaces, tabs and blank lines used for indentation and vertical spacing, unless the whitespace is contained in strings. Some whitespace is required, however, such as the space between variable names and keywords. You can insert extra whitespace characters to enhance program readability.

Single Line If...Then statement

The preceding If...Then selection statement also could be written on a single line as

 If studentGrade >= 60 Then resultLabel.Text = "Passed"

In the multiple-line format, all statements (there can be many) in the If...Then's body execute if the condition is true. In the single-line format, only the statement immediately after the Then keyword executes if the condition is true.

UML Activity Diagram for the If ... Then Selection Statement

Figure 4.2 is an activity diagram for the single-selection If...Then statement. It contains what is perhaps the most important symbol in an activity diagram—the diamond, or decision symbol, which indicates that a decision is to be made. A decision symbol indicates that the workflow will continue along a path determined by the symbol's associated guard conditions, which can be true or false. Each transition arrow emerging from a decision symbol has a guard condition (specified in square brackets above or next to the transition arrow). If a particular guard condition is true, the workflow enters the action state to which that transition arrow points. Exactly one of the guard conditions associated with a decision symbol must be true when a decision is made. In Fig. 4.2, if the grade is greater than or equal to 60, the program displays "Passed" on the screen, then transitions to the final state of this activity. If the grade is less than 60, the program immediately transitions to the final state without displaying a message.

Figure 4.2

Fig. 4.2 If ... Then single-selection statement activity diagram.

  • + Share This
  • 🔖 Save To Your Account