Conditional Operators

Submitted by: 

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.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.