Tuesday, January 22, 2008

What if?

What if is a simple statement that can be followed by anything. What if the world was flat? What if my hair was black? What if "if" can be used in programming? Well it can! The If........then statement can be used to tell a program to do something if a condition is met or not met. This is the basic format of the If........then statement:

If [condition] then
[do this]
endif

Its better explained like:
If [it is cold outside] then
[put on a sweater]
end if

Between the if........then there should be something there that the computer can work out. An operator like + - / * ^ = > < are the most commonly used. Example of this will be

If x + y = 10 then
label should show 10
end if

Where x represents a dimension you set and y represents a dimension you set. Label represents a "label" you put in the form.( make sure to give the label a unique name) For the above code it would look something like this.

Dim number1 as integer
Dim number2 as integer

'Set variables
number1 = [Val(txtfirstnumber)] <-------we will let this be x

number2 = [Val(txtsecondnumber)]<------we will let this be y

'the if statements

if number1 + number 2 = 10 then
lblanswer.text = "Those two numbers equal 10"
endif

There you have it.

Next week i will go over If.........then..........else!


Reminder: this is for a class and if there is something i left out, we already covered it in class. So if you are just surfing the web and have a Q. about something i left out just post a comment.

No comments: