The if statement ( If...Then in VB ) executes embedded statements if a condition evaluates to true.
if ( condition ) {
... statements ...
} else {
... statements ...
}
if condition then
... statements ...
else
... statements ...
end if
if ( condition ) {
... statements ...
} else {
... statements ...
} |
|
C# |
VB |
JScript |
- The condition can be any valid expression that evaluates to true or false.
- The if statement can optionally include an else clause.
- Any valid statement can follow the if or else statement, including further if statements.
NOTE: In C# and JScript, if more than one statement follows a control statement, such as an if or else statement, the statements must be enclosed within braces { }.
An if statement is executed as follows:
- The condition is evaluated.
- If the condition is true, the statements within the if block are executed.
- If the condition is false, and an else part is present, the statements within the else block are executed.
- Thereafter, control is passed to the statement after the if...else block.
The following example demonstrates use of an if...else statement to display a greeting depending on the time of day.
Show me