Skip to main content

Conditional Operators


In Visual Basic, we are using Conditional Operators to evaluate a condition that applied to one or two boolean expressions. An expression returning a value if the that expression is true and a different one if the expression is evaluated as false. The logical AND and OR operators both take two operands. Each operand is a boolean expression. It evaluates to either true or false.

The && (logical AND) operator indicates whether both operands are true then it returns true, otherwise it returns false.

Evaluating of Logical AND operators through expressions.

Expression				Results
1 && 1					True or 1
1 && 0 					False or 0
0 && 1					False or 0
0 && 0 					False or 0

Visual Basic Code:

  1. 'declare a variable
  2. Dim username As String
  3. Dim password As String
  4. 'assign value to a variable
  5. username = "joken"
  6. password = "12345"
  7.  
  8. 'test if the two expression match, then it welcome the user
  9. If username = "joken" And password = "12345" Then
  10. 'returns true
  11. MsgBox("Welcome joken!")
  12.  
  13. Else
  14. 'return false
  15. MsgBox("Username and Password did not match!")
  16. End If

The || (logical OR) operator indicates whether either operand is true, it returns true.

Evaluating of Logical OR operators through expressions.

Expression				Results
1 && 1					True or 1
1 && 0 					True or 1
0 && 1					True or 1
0 && 0 					False or 0

Visual Basic Code:

  1. 'declare a string variable
  2. Dim name As String
  3. 'assign value to a string variable
  4. name = "Joken"
  5. 'test if name either of the two expression is true
  6. If name = "Joken" Or name = "joken" Then
  7. 'return true
  8. MsgBox("Welcom Joken!")
  9. Else
  10. 'return false
  11. MsgBox("Your not identified!")
  12. End If

In above example example is applicable in real-world application, especially when you are doing the login- logout system or any other application that need to passed through conditional operations.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.