Visual Basic.NET 2008 > Programming Fundamentals If...Then Statement in Visual Basic 2008The If. . .Then statement tests an expression, which is known as a condition. If the condition is True, the program executes the statement(s) that follow. The If. . .Then statement can have a single-line or a multiple-line syntax. To execute one statement conditionally, use the single-line syntax as follows:
Conditions are logical expressions that evaluate to a True/False value and they usually contain comparison operators— equals (=), different (<>), less than (<), greater than (>), less than or equal to (<=), and so on — and logical operators: And, Or, Xor, and Not. Here are a few examples of valid conditions:
The parentheses are not really needed in the first sample expression, but they make the code a little easier to read. Sometimes parentheses are mandatory, to specify the order in which the expression's parts will be evaluated, just like math formulae may require parentheses to indicate the precedence of calculations. You can also execute multiple statements by separating them with colons:
Here's an example of a single-line If statement:
You can break this statement into multiple lines by using the multiline syntax of the If statement, which delimits the statements to be executed conditionally with the End If statement, as shown here:
The Month property of the Date type returns the month of the date to which it's applied as a numeric value. Most VB developers prefer the multiple-line syntax of the If statement, even if it contains a single statement. The block of statements between the Then and End If keywords form the body of the conditional statement, and you can have as many statements in the body as needed. Many control properties are Boolean values, which evaluate to a True/False value. Let's say that your interface contains a CheckBox control and you want to set its caption to On or Off depending on whether it's selected at the time. Here's an If statement that changes the caption of the CheckBox:
This statement changes the caption of the CheckBox all right, but when should it be executed? Insert the statement in the CheckBox control's CheckedChanged event handler, which is fired every time the control's check mark is turned on or off, whether because of a user action on the interface or from within your code. The expressions can get quite complicated. The following expression evaluates to True if the date1 variable represents a date earlier than the year 2008 and either one of the score1 and score2 variables exceeds 90:
The parentheses around the last part of the comparison are mandatory, because we want the compiler to perform the following comparison first:
If either variable exceeds 90, the preceding expression evaluates to True and the initial condition is reduced to the following:
The compiler will evaluate the first part of the expression (it will compare two dates) and finally it will combine two Boolean values with the And operator: if both values are True, the entire condition is True; otherwise, it's False. If you didn't use parentheses, the compiler would evaluate the three parts of the expression:
Then it would combine expression1 with expression2 using the And operator, and finally it would combine the result with expression3 using the OR operator. If score2 were less than 90, the entire expression would evaluate to True, regardless of the value of the date1 variable. Table of Contents
|
|||||
W3computing.com Copyright 2011 © All Rights Reserved
|
|||||
| Home | Useful links | Contact us | |||||