• We just launched and are currently in beta. Join us as we build and grow the community.

If Statements

bastun

Cyber Awareness Educator
Divine
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
75
Likes
134
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
The If statements is one of the conditional Logic statements, that allows the programmer to specify which condition evaluates to true or false. Without the conditional statement, a program could not meet the condition requirement and possibly returns a different result.
Sample Syntax
If condition Then [ statements ] [ Else [ else statements ] ]

The if statement is a decision statement, used to check a condition and if the condition is true then the statements following Then are executed. The else clause is optional, one or more statements that are to be executed.

This time, let’s create a new project and name it as “userLogin” and click “OK”.
userlogin.png


Then add two labels, two textbox and a button. Change the text property of the first label into “Username” and the other one as “Password”, change also the Name property of the Textbox1 into “txtuser” and the Textbox2 as “txtpass”, and lastly for the button change the Name Property into “bntlogin” and the Text property as “Log in”. Next, arrange all the objects that look like as shown below.

login.png


This time, we’re going to add functionality to our application applying the if statements. To do this double click a button, and add the following code:

  1. Dim

    user As

    String
  2. Dim

    pass As

    String

  3. user =

    txtuser.

    Text
  4. pass =

    txtpass.

    Text

  5. If

    user =

    "Joken"

    And

    pass =

    "12345"

    Then
  6. MsgBox

    (

    "Welcome Joken"

    )
  7. Else
  8. MsgBox

    (

    "Access is denied!"

    )
  9. End

    If

The code above if statements, we have declared there a two variables the “user” and “pass” as a string data type. Next, it fills the two variables with values based on user inputs using the two textboxes. Then using if statements, it checks user inputs if user is equal to “Joken” AND pass is equal to “12345”, then it displayed “Welcome Joken” otherwise “Access denied” will be displayed. And we also use AND operator to check if both conditions are true, then the results is true.
Next, if you want to log in more than one user account, nested if statements are also available in visual basic. Here’s the following example that’s illustrates the nested if statements.

  1. Dim

    user As

    String
  2. Dim

    pass As

    String

  3. user =

    txtuser.

    Text
  4. pass =

    txtpass.

    Text

  5. If

    user =

    "Joken"

    And

    pass =

    "12345"

    Then
  6. MsgBox

    (

    "Welcome Joken!"

    )
  7. Else
  8. If

    user =

    "Diego"

    And

    pass =

    "54321"

    Then
  9. MsgBox

    (

    "Welcome Diego"

    )
  10. Else
  11. If

    user =

    "Pablo"

    And

    pass =

    "picaso"

    Then
  12. MsgBox

    (

    "Welcome Pablo"

    )
  13. Else
  14. MsgBox

    (

    "Access is denied!"

    )
  15. End

    If

  16. End

    If

  17. End

    If

This time, you can now run your program by pressing “F5”.

Book traversal links for If Statements

  • ‹ For each Loop
  • Up
  • ListBox Adding and Removing an Item in VB.NET ›

 

439,009

316,008

316,017

Top