Commands: Conditional

Unlike many programming languages, Visual Basic has been designed to focus on readability and clarity. Many languages are willing to sacrifice these attributes. Visual Basic is designed under the paradigm that the readability of code matters, so commands in Visual Basic tend to spell out the exact context of what is being done.

Literally dozens of commands make up the Visual Basic language, so there isn't nearly enough space here to address all of them. Moreover, many of the more specialized commands are covered later in this book. However, in case you are not familiar with Visual Basic or are relatively new to programming, this book will touch on a few of these. In this chapter, it makes the most sense to review the syntax of conditional statements.

Each of these statements has the ability to literally encapsulate a block of code. Of course the preferred way of embedding code within the scope of a conditional or loop is to call another method. Using a method within your loop or conditional helps keep your structure apparent and simple to view, and allows for code reuse if the same actions need to be accomplished from another location within your code.

Note that the variables declared within the context of a conditional statement (between the If and End If lines) are available only up until the End If statement. After that, these variables go out of scope. The concept of scoping is discussed in more detail later in this chapter.

If Then

The conditional is ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.