If... Else... End If statementAuthor : Pandu Ranga Rao N
E - Mail : pandu2050@gmail.com

In any programming language, we come across the sentence "If something occurs, then do this else do that." This is what being done using if statement in Visual Basic.

There are six forms of If condition in visual basic,

Form - 1


If [[condition]] Then [statement]

Form - 2

If [[condition]] Then [statement] Else [statement]

Form - 3


If [condition] Then
  [statements]
End If

Form - 4


If [condition] Then
  [statements]
Else
  [statements]
End If

Form - 5


If [condition] Then
  [statements]
ElseIf [condition] Then
  [statements]
End If

Form - 6


If [condition] Then
  [statements]
Else
If [condition] Then
  [statements]
End If
End If

Whenever Visual Basic encounters if statement, it will test for the condition given there. If the condition becomes true, then Visual Basic will execute the statements proceeds “Then”. Else it will test for another condition and so on, till the last “Else” is encountered.

The first two if statements are meant to be written in single line only. There should not be more than one statement to be executed for writing these two single lined if statements.

The rest of the If statements are written in multiple lines. A mismatch in the syntax could cause the Visual Basic interpreter to point out the mistake, when you hit the enter key at the end of the line in the Visual Basic IDE. An error message will be flashed out to the developer.

Let us now, look into examples.

For our example to work, choose a text box and a Command Button from Tool Box.

Type the given code in click event of the command button at the code window


If Text1.Text = “PBS” Then MsgBox “Programming Basics...”

And Run the program in the Visual Basic IDE itself by pressing F5.

Next, Type “PBS” in the text box and click on the command button. It will show up a message box with OK button as “Programming Basics !...”. Typing something else will display nothing because of if condition in the click event of command button.

If you want to display "Wrong Cmd !...", when you type something else then you should modify the above code as,


If Text1.Text = “PBS” Then MsgBox “Programming Basics !...” Else Msgbox ”Wrong Cmd !...”

The above code can also be written in multi line, as,


If Text1.Text = “PBS” Then
  MsgBox “Programming Basics !...”
Else
  Msgbox ”Wrong Cmd !...”
End If

Author : Pandu Ranga Rao N
E - Mail : pandu2050@gmail.com
 
 
 
 
 
 
  About Team ProgrammingBasics.In

Contact Team ProgrammingBasics.In

Instructions for submitting your own article
 
 
X

About Team ProgrammingBasics.In

Right now, we are a small but, dedicated team who strived day and night to alter the pretty old programmingbasics.in into a new conceptual website "The ProgrammingBasics Foundation Library".

The articles and topics in the website are the major contributions of freelancers, programmers across the globe. We thank each of them for their valuable contributions. You can also become a contributor by sending some software related articles to us. We will publish it in our website. Instructions for the same are given in the "Instructions for submitting your own article" page.

Contact Us

ICan Technologies,
HIG 44, 23rd Cross, 8th Main,
E-Block, Vijayanagar 3rd Stage
Mysore-570017, Karnataka, India

Website: www.icantech.in
Mobile: +918050239039

Instructions for submitting your own article

Want to become an article contributor to this site ?

If yes, then send us an article written by you on your own words, neatly typed in MS Word document. If we feel that, you have written the article by using some other article then we will reject the article. Every article we will cross verify, before we publish them. The right to reject the article is with us.

If you want to publish your name and email id's, specify the same in the document.

NOTE: You must give your consent in the email for publishing your email id and your name. Otherwise, your name and email-ids will not get published. We respect your privacy.

One article per document, which means, you must not send more than one article in one document. Everything in the sent document will be treated as one article and your article will get rejected without looking in, further.

The articles must and should be related to computer basics, programming concepts and programming languages. We will not entertain any other topics. Please bear this in your mind, before starting an article write-up.

Articles published in ProgrammingBasics.in are the sole property of ICan Technologies, Mysore. You are here in authorizing ICan Technologies, Mysore to take the ownership of the published article. Rejected articles will not be published and you can try to publish the same in other sites. We will inform you the status of your submitted article within 10 working days, from the date of sent mail. If not responded within 10 days, then you are free to try publishing your article on some other websites. We will simply discard them and will not publish them any further. Once we publish the article, you will not be allowed to publish as it is, in some other website. If we find it, then your article will be removed from site and the removal will be informed to you.

Send your articles to programmingbasics.info@gmail.com with "Article for Publishing" in the subject line.

You must create your own images in PSD (Photoshop Document) only, which is to be supported in CS3 or lower versions. Any animated stuffs, must be in fla files (Flash editable file) and not in any other format and must be readable in Falsh 8.0.

You are now in