Conditional statements in Visual Basic 6.0 – 8th tutorial

Last Updated on

Conditional statements

Conditional statement mean a set of instructions will be executed when a predefined condition occurs. For example if you want to show pass if student get  33 or more then 33 marks.

Or show failed if student got less then 33 marks. Here you have to write conditional statement. While working on a software application project we have to face many conditions appeared in program logics.

This is beauty of programming that can be used to check and set conditional execution. Let’s get one more example to know what a conditional statement is and how it works.

Imagine if we have a website and users are allowed to post comments to articles. One of users start bad comments and you want to stop it and don’t want him to comment again.

What you have to set in your programming code and how you will define conditions for that type of users. Yes here we will use conditional statement.

We will block the user to get login access and we will set the condition at login if user is blocked then he cant login. We mostly used if then else statement for this purpose.

But in our next lessons we will learn briefly about more conditional statements.

Below we will discuss how to write and use conditional statement using if then else.

 

[AdSense-A]

Structure and parameters

Visual Basic has following types of conditional structures.

  1. if – – – – then
  2. if – – – – then – – -else
  3. Select case structure
READ  Programming fundamentals in Visual Basic 6.0 - 6th turorial

Conditional Structure using IF statements

We will learn through using above example for students marks here.

Start a new EXE project and design interface as below.

Conditional statements form design

Set the properties for controls.

Lable1.caption = Enter marks

Text1.text=0

Command1.caption = Result

Then double click to the command button and add the code as below.

Now enter marks in text box and click the command button to see results in a message box.

Source code details

Conditional statements sourc code example

Dim VarResult As String

—here we define a variable VarResult setting up its data type as String

If Val(Text1.Text) < 33 Then

—then we get value from Text1 and applied conditions to check if marks are less then 33

VarResult = "Student Failed"

— if condition is true then we set value for variable here to Student Failed

Else

— if condition is false

VarResult = "Student Passed"

–if condition is false then we set the variable value to Student Passed

End If

MsgBox VarResult, vbInformation, "Result"

—showing mesg box for resutls

 

[AdSense-B]