Making Your 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."
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.
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.
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.
To use the Else clause
Change the code in the If...Then statement to the following.
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.
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.)
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, or, you can continue with the next lesson, .