Home
Username:
Password:
Microsoft Visual Basic 2005 Tutorials

Conditional Programming / Case Structure Alternative




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

We have seen how you can use If statements to test condition and make decisions. Whenever you want to test a single variable for multiple values, you could also use a structure known as the Case Structure. This Case Structure provides a flexible and very powerful solution. Code that you've already written in an if structure could also be written as a Case Structure. Often the Case Structure is easier to understand. Here's an example of the Case Structure where we have the variable quantity integer and as you can see, each case is similar to an IF statement except instead of using the word if, we use the word case. And the first example is read case is greater than a thousand. Immediately after that statement you would place any statements that you would want to be performed if indeed the quantity is greater than one thousand. Below that is another case of 500 to 999. Here you would place statements if the quantity is between 500 and 999. Now, make note that this is an inclusive range. That is, this includes the number of 500 and includes the number 999. Dropping to the third case where it has written case is zero to 499, here you would perform whatever statements you want executed, if any, if the quantity is between zero and 499. Again, this is including the number zero and including the 499. Now, one nice benefit of the Case Structure is that you can use a clause called case else. This is an optional clause, just like the else is optional in the if structure, but the case else basically says that if none of the above are true, then this gives you one last opportunity to catch an error or display an error message or something out of the ordinary has happened. So the structure of select case quantity integer could also be written as an IF statement. The user would not have any idea what structure is being used, nor would they care. Either method, if statements or the Case Structure, is perfectly valid and a lot of this depends on the programmer's way of thinking and just their organizational approach to the problem. One last note; the select Case Structure does end with the word and select, similar to the If statements that end with the words and if. By the way, there is no limit to the number of statements that can follow a case statement. Also, in the Case Structure, the word is is a required word. Although the case else clause is optional, generally you will want to include it in the select case statements. The statements you code beneath the case else execute only if none of the other case conditions have been met. This clause provides checking for any invalid or unforeseen values of the expression being tested. If the case else clause is omitted and none of the case conditions are true, then the program continue execution at the statement following the and select. Also, if more than one case value is matched by the expression, then only the statement in the first case that's matched will execute. The other will be ignored.

Tutorial Information

Course: Microsoft Visual Basic 2005
Author: Arthur Lee
SKU: 33940
ISBN: 1-935320-10-6
Release Date: 2008-11-19
Duration: 7.5 hrs / 97 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available