Learn to program in Visual Basic .NET, the right way

Logic - If Statement

What is Conditional Logic?

Conditional logic is whenever you compare a statement to see if it is True or False.

What is an If Statement?

The If statement in Visual Basic .NET is one method of comparing statements.

Think of If Statements in real life for a moment:

If I study, then I will learn.

This example may sound trivial, but none-the-less it should give you a good idea of how If statements are setup.

ElseIf Statement

If you have a need to compare more than one condition, then you can use one or more ElseIf statements after the original If statement.

Think of an ElseIf Statement in the following context:

If I study, then I will learn. Otherwise, if I don't study, then I may or may not learn.

Else Statement

If you want to execute code if all previous comparison statements failed, then you can use an Else statement after the original If statement and after any of the optional ElseIf statements.

Think of an Else Statement in the following context:

If I am younger than 13 then I am an adolecent. Otherwise, if I am older than 13 but less than 20 then I am a teenager. Otherwise, I am an adult.

Examples

The following demonstrates how to use the multiple variations of the If Statement:

Fiddle: Live Demo

Remarks

It is important to remember that you must have an If statement and that the ElseIf and Else statements are optional, but that the Else statement must come at the very end.