
|
Home Blog Me Funbox Photo Gallery Entertainment Scripts Tutorials Programming Fundamentals Programming in C++ Visual BASIC Programming Part 1 Part 2 Part 3 Part 4 Part 5 Horoscope Writing Site Center Links/Blogrolls Living Room Furniture |
If...Then...ElseThere are times when you want the program to respond differently to different events. Say you wanted to write some code that checks to see that the user entered a number. To do this we would use an if statement. The format for an if statement in Visual BASIC is if condition is true then...do something. The condition would use one of these operators:
To check to see if what the user entered is numeric, we use the builtin function IsNumeric. To do this we put IsNumeric then parenthases and then what we want to check. IsNumeric returns a Boolean variable, which means that the answer will either be True or False, which we need to know for the if statement. Let's use the program from the last section and check and see if the numbers entered is numeric.
If you want to do more than one line of code then you will need to come to the next line after the Then (hitting enter) and type all the code there. When you are done, you'd put an End If. Else StatementsElse statements are used when you want want something to happen if the comparison is false. The else statement comes in handy when say you want your program to do something if your name is Jimmy but do something completely different if your name was Sandra. You can never have an else statement by itself, it always needs to accompany an if statement. Else would come after the last line of code to execute if the condition is true, and before the End If. Let's check and see if a number is odd or even. To check if the number entered is an even number, simply mod it by 2. If the result is 0 then the number is even, otherwise it is odd. This code would go inside of a Button, and will work assuming you have the other components named the same thing:
The reason we mod it by 2 is because only even numbers can be divided evenly by 2, so if a number divided by 2 has no remainder, we can conclude that it is even. Remember the first program in this section, where we had two if statements one for each TextBox, well we can combine them. to do this, we would use one If statement, a condition, a conjunction, and another condition. The conjuctions are:
So to rewrite the code, we would do:
Now an else statement can have as many lines of code as you want, you can even put more if statements in there. Say for example you wanted to turn a number grade into a letter grade, to do this we would need a bunch of ElseIf statements.
Case SelectElseIf statements can get pretty messy. Just look at the previous code, for some it might be diffictule to decifer. If you have too many choices then you may want to use Select Case. Personally, I use Select Case if an If statement has three or more possible outcomes. To use Select Case, you would have an object's property or variable as the focus and then each case is a condition.
The Select Case can be used with any variable type. Unlike other languages, the code ends when it hits the next Case statement. After it executes the code, it will go to the End Select and continue with the rest of the program. Using conditions, whether it be an If statement or Select Case is essential to everyday programs. Next Section: Coming Soon |
Copyright © 2004-2008 Sean Noble, All rights reserved
Read our Disclosure Policy


