Chapter 2. Controlling Your Programs

In This Chapter

Setting conditions with the If statement

Zeroing in on the Select Case statement

Reaching a particular value with the For statement

Setting a time limit with the While statement

Specifying tasks with the Do statement

Only the most trivial of programs execute their tasks one at a time, in the exact sequence in which the statements are listed in the program. Life, even in the world of programming, just isn’t that simple. Most programs require more control over the sequence in which statements are executed. For example, you may need to skip over some statements based on the results of a condition test. Or, you may need to create a loop of statements that repeats itself a given number of times or until a certain condition is met.

This chapter describes the VB.NET statements that let you control the flow of execution for your ASP.NET programs.

Using If Statements

The Robert Frost poem that begins, “Two roads diverged in a yellow wood . . .” is an apt description of how the If statement works. The program is rolling along, executing one statement after another, until it comes to an If statement. The If statement represents a fork in the road; a choice must be made about which path to take.

Many programs have to make such either/or decisions as they execute. For example, a program that writes data entered by the user into a database has to make a determination first: Did the user actually enter some data? If so, the program writes the data ...

Get ASP.NET 2.0 All-In-One Desk Reference For Dummies® 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.