If, Then, Else, End If

Before we make the program, I am going to break down the syntax of If's. They are quite easy when it comes down to it. You always tell the program that you are going to start a new if by going to a new line and typing "If"...that wasnt to difficult. If's basically check to see if a certain condition is true and then executes a block of code.

ElseIfs check to see if the above condition is true and then test the same thing with another condition. Else simply states that if the above isnt true, then do this. This is how an if statement would be set up:

If object.value is {True, False, or a mathmatical expression}

Then What the program does if the above is true Else What the program does if the above is not true End if You must always end an if or your program will result in errors.

Ifs are also neat in the fact that they can be nested, meaning ifs within ifs. Just dont forget to close any ifs you open.

Alrighty, now lets apply it. Open a new Visual Basic project, standard EXE. Set the form name to frmIf and the caption to "Im learning IF's." Add the following elements: Labels > lblAdd Command Buttons > cmdExit; cmdTest Text Boxes > txtAnswer Set the command button captions to Exit and Is it Right?.

Set the label caption to 2 + 2 = and the Text property to the text box to nothing. Double click cmdTest and open up an If. Now, we want to check to see if the text in txtanswer is equal to 4. If it is, then display a message box that tells the user they are right, else if it is not, display a message box that tells the user they are wrong. Dont forget to end the if.

Your code should look like this:

If txtanswer.Text = 4 Then MsgBox "You're right" Else MsgBox "You're wrong" End if

Simple when it comes down to it. I will be writing a more complicated version with more in depth descriptions and tings to do at a later date.

For now, try and make a program with a label that asks someone their age and if the number is greater than 17, a message or label says that they are an adult and if they arent, the label says that they arent.

Good luck and happy programming.