Decisions

Decisions control the execution flow of a script or one of its sections with one or more conditional statements.

decision making statements

if statement

An If statement consists of a boolean expression followed by one or more statements. If the condition is said to be True, the statements under If condition(s) are Executed. If the Condition is said to be False, the statements after the If loop are executed.

Syntax

The syntax of an If statement in VBScript is:

If(boolean_expression) Then

   Statement 1

.....

.....

   Statement n

End If

Flow Diagram

Example

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

Dim a : a = 20

Dim b : b = 10

If a > b Then

   Document.write "a is Greater than b"

End If

</script>

</body>

</html>

When the above code is executed, it produces the following result:

  • a is Greater than b

if…else statement

An If statement consists of a boolean expression followed by one or more statements. If the condition is said to be True, the statements under If condition(s) are Executed. If the Condition is said to be False, the statements under Else Part would be executed.

Syntax

The syntax of an if statement in VBScript is:

If(boolean_expression) Then

   Statement 1

.....

.....

   Statement n Else

   Statement 1

.....  ....

   Statement n

End If

Flow Diagram

Example :

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

Dim a : a = 5 Dim b : b = 25

 

If a > b Then

   Document.write "a is Greater" Else 

   Document.write "b is Greater"

End If

 

</script>

</body>

</html>

When the above code is executed, it produces the following result:

  • b is Greater

if…elseif…else statement

An If statement followed by one or more ElseIf Statements that consists of boolean expressions and then followed by a default else statement, which executes when all the condition becomes false.

Syntax

The syntax of an If-ElseIf-Else statement in VBScript is:

If(boolean_expression) Then

   Statement 1

.....

.....

   Statement n

ElseIf (boolean_expression) Then

   Statement 1

.....  ....

   Statement n

ElseIf (boolean_expression) Then

   Statement 1

.....  ....

   Statement n Else

   Statement 1

.....  ....

   Statement n

End If

Flow Diagram

Example :

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

Dim a   a = -5

 

If a > 0 Then

   Document.write "a is a POSITIVE Number" ElseIf a < 0 Then

   Document.write "a is a NEGATIVE Number"

Else

   Document.write "a is EQUAL than ZERO"

End If

</script>

</body>

</html>

When the above code is executed, it produces the following result:

a is a NEGATIVE Number

nested if statements

An If or ElseIf statement inside another If or ElseIf statement(s). The Inner If statements are executed based on the Outermost If statements. This enables VBScript to handle complex conditions with ease.

Syntax

The syntax of a Nested if statement in VBScript is:

If(boolean_expression) Then

   Statement 1

.....

.....

   Statement n

   If(boolean_expression) Then       Statement 1

.....

.....

  Statement n

   ElseIf (boolean_expression) Then       Statement 1

.....  ....

      Statement n

   Else

   Statement 1

.....  ....

   Statement n

   End If

Else

   Statement 1

.....  ....

   Statement n

End If

Example

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript"> Dim a a = 23

 

If a > 0 Then

   Document.write "The Number is a POSITIVE Number"

   If a = 1 Then

      Document.write "The Number is Neither Prime NOR Composite"       Elseif a = 2 Then

      Document.write "The Number is the Only Even Prime Number"       Elseif a = 3 Then

      Document.write "The Number is the Least Odd Prime Number"       Else

      Document.write "The Number is NOT 0,1,2 or 3"   

   End If

ElseIf  a < 0 Then

   Document.write "The Number is a NEGATIVE Number"

Else

   Document.write "The Number is ZERO"

End If

</script>

</body>

</html>

When the above code is executed, it produces the following results:

  • The Number is a POSITIVE Number The Number is NOT 0,1,2 or 3

switch statement

When a User want to execute a group of statements depending upon a value of an Expression, then Switch Case is used. Each value is called a Case, and the variable being switched ON based on each case. Case Else statement is executed if test expression doesn't match any of the Case specified by the user.

Case Else is an optional statement within Select Case, however, it is a good programming practice to always have a Case Else statement.

Syntax

The syntax of a Switch Statement in VBScript is:

Select Case expression    Case expressionlist1       statement1       statement2

      ....       ....

      statement1n    Case expressionlist2       statement1       statement2

      ....

      ....

   Case expressionlistn

      statement1       statement2

      ....

      ....      Case Else       elsestatement1       elsestatement2

      ....

      ....

End Select

Example

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

Dim MyVar

MyVar = 1

Select case MyVar    case 1

Document.write "The Number is the Least Composite Number"    case 2

Document.write "The Number is the only Even Prime Number"    case 3

Document.write "The Number is the Least Odd Prime Number"    case else

Document.write "Unknown Number"

End select

</script>

</body>

</html>

In the above example, the value of MyVar is 1. Hence, Case 1 would be executed.

The Number is the Least Composite Number