Extended if then else structure in Visual Basic 6.0 – 9th tutorial

Extended if then else structure

To chose a block of statements form many options we can use extended if then else structure. We can chose from many options using one condition parameter. If we have a condition again inside a condition we can extend its functionality using Extended if then else structure. For example if we have multiple type of users login some users are just common users and some have access to see reports and some maybe system administrators. I mean users with multiple access levels. To manage and handle situations like this we can use extended if then else statement to extend logical functionalities.

Lets discuss in details how this would be work. At login time we will check if username and password is correct then again we will use if condition inside the if condition  if the user is administrator  then we will assign a special interface for system admins. This is how the extended if then else structure works. Lets now take a look at syntax and how to code for extended logics.

Syntax

If condition = A then

Execute code here

Elseif condition= B then

Execute code here

Elseif condition = C then

Execute code here

Else

If doesn’t match to any condition the execute code her

End if

 

 

By the following code example we will see the extended if then else statement in action.

We will create a project for students grading system to use extended if then else statement. This project will show the student grade using their marks.

Start a new EXE project in Visual Basic 6.0

READ  Variables in Visual Basic 6.0 - 7th tutorial

Design the form as below then set the properties for your controls as below.

Extended if then else structure
Extended if then else structure form

Form1.caption=”Student Grading System using extended if then else strutcure”

Command1.caption=”Show Grade”

Double click on the command button to get in code window and type the code as below:

Extended if then else structure
Extended if then else structure code example

Now run the project to test

Code understanding:

Dim ObtainedMarks As Integer

— variable defined to get marks form an input box

ObtainedMarks = InputBox("enter marks to get Grade")

— variable ObtainedMarks getting value from an input box

Processing variable value to different conditions and showing out put

 

If ObtainedMarks >= 70 Then

MsgBox "Student got A+ grade"

ElseIf ObtainedMarks >= 60 Then

MsgBox "Student got A grade"

ElseIf ObtainedMarks >= 50<strong> Then</strong>

MsgBox "Student got B grade"

<strong> </strong>Else

MsgBox "Student got C grade"

End If

 

Want to read again later ?? Share to save in your social media.