Making a Program Choose Between Two Possibilities: The If...Then Statement
In this lesson, you will learn to use the If...Then statement to run code based on conditions.
Programs need to do different things in response to different conditions. For example, you might want your program to check what day of the week it is, and then do something different depending on the day. The If...Then statement allows you to evaluate a condition and to then run different sections of code based on the results of that condition.
The following example demonstrates how the If...Then statement works.
If My.Computer.Clock.LocalTime.DayOfWeek = DayOfWeek.Monday Then
MsgBox("Today is Monday!")
End If
When this code is run, the condition (the part between If and Then) is evaluated. If the condition is true, the next line of code is run and a message box is displayed; if it is false, the code skips to the End If line. In other words, the code states "If today is Monday, then display the message."
Try It!
To use the If...Then statement
On the File menu, choose New Project.
In the New Project dialog box, in the Templates pane, click Windows Application.
In the Name box, type IfThen and then click OK.
A new Windows Forms project opens.
Double-click the form to open the Code Editor.
In the Form1_Load event handler, type the following code.
If My.Computer.Clock.LocalTime.DayOfWeek = DayOfWeek.Saturday Or _ My.Computer.Clock.LocalTime.DayOfWeek = DayOfWeek.Sunday Then MsgBox("Happy Weekend!") End If
Press F5 to run your program.
If today is Saturday or Sunday, a message box appears telling you Happy Weekend! Otherwise, no message box appears.
On the Debug menu, choose Stop Debugging to end the program. Keep this project open. You will add to it in the next procedure, "To use the Else clause".
You may have noticed in the above example that the If...Then statement used the Or operator to evaluate multiple conditions ("if it is Saturday Or if it is Sunday"). You can use the Or and And operators to evaluate as many conditions as you want in a single If...Then statement.
The Else Clause
You have seen how to use the If...Then statement to run code if a condition is true—but what if you want to run one set of code if a condition is true, and another if it is false? In this case, you can use the Else clause. The Else clause allows you to specify a block of code that will be run if the condition is false. The following example demonstrates how the Else clause works.
If My.Computer.Clock.LocalTime.DayOfWeek = DayOfWeek.Friday Then
MsgBox("Today is Friday!")
Else
MsgBox("It isn't Friday yet!")
End If
In this example, the expression is evaluated; if it is true, then the next line of code is run, and the first message box is displayed. If it is false, then the code skips to the Else clause, and the line following Else is run, displaying the second message box.
Try It!
This procedure begins where "To use the If...Then statement" ended. If you have not completed "To use the If...Then statement," you must do so before continuing.
To use the Else clause
Change the code in the If...Then statement to the following.
If My.Computer.Clock.LocalTime.DayOfWeek = DayOfWeek.Saturday Or _ My.Computer.Clock.LocalTime.DayOfWeek = DayOfWeek.Sunday Then MsgBox("Happy Weekend!") Else MsgBox("Happy Weekday! Don't work too hard!") End If
Press F5 to run your program. Your program will now display a message box stating whether it is a weekend or a weekday, with appropriate content.
Note
You can change the day of the week by double-clicking the time on the Windows taskbar, if you want to test the execution of both code blocks. (The taskbar is the bar that contains the Windows Start button; by default, it is at the bottom of the desktop, and the time appears in the right corner.)
Next Steps
In this lesson, you learned how to use the If...Then statement together with the Else clause to make your program selectively run blocks of code based on conditions at run time. For your next lesson, you can choose to either explore how to select code to run by reading Closer Look: Using Select Case to Decide Between Multiple Choices, or, you can continue with the next lesson, What To Do When Something Goes Wrong: Handling Errors.
See Also
Tasks
Making a Program Repeat Actions: Looping with the For...Next Loop
Comparisons: Using Expressions to Compare Values