Decision making is very important for the programmer to control the flow of script or a section of script by using some conditional statements in our script we can let our program decide whether the given condition is true or not and accordingly execute further statements.

Following is the general form of a typical decision making structure found in most of the programming languages.






This image above clearly signifies that for our script to perform decision making we just need to pass the condition to check and then the actions we need it to perform after evaluation of the statement.

here, it shows that when condition is evaluated and it gets false then, some part of code gets executed and if the condition evaluated to true then other part of code is executed.

VBA provides the following types of decision making statements. 

  • if statement
  • if...else statement
  • if...elseif...else statement
  • nested if statements
  • switch statement


1) If  Statement :

An if conditional Statement consists of a boolean expression followed by one or more statements. If the condition is true then statements under if statements are executed while if the condition is said to be false then statements after the if Conditionals are executed.

Syntax :

Below is the Syntax of an If conditional statement :

If(boolean_expression) Then
   Statement 1
   .....
   .....
   Statement n
End If!'



Flow Diagram


Example :

Private Sub if_demo()
   Dim x As Integer
   Dim y As Integer
    
   x = 50
   y = 32
    
   If x > y Then
      MsgBox "X is Greater than Y"
   End If
End Sub

Output :



2) If else  Statements

An if else conditional Statement consists of a boolean expression followed by one or more statements. If the condition is true then statements under if statements are executed while if the condition is said to be false then statements under else are executed.

Syntax :

Below is the Syntax of an If Else conditional statement :

If(boolean_expression) Then
   Statement 1
   .....
   .....
   Statement n
Else
   Statement 1
   .....
   ....
   Statement n
End If


Flow Diagram




Example :

Program to check greatest number among two numbers.

Private Sub if_demo()
   Dim x As Integer
   Dim y As Integer
    
   x = 21
   y = 32
    
   If x > y Then
      MsgBox "X is Greater than Y"
   Else
      Msgbox "Y is Greater than X"
   End If
End Sub

Output :



3) If  Elseif - else Statements

An if  elseif-else conditional Statement consists of a boolean expression under if statement followed by one or more elseif statements and then followed by a default else statement.

If the condition is true then statements under if  are executed while if the condition is said to be false then statements after the elseif Conditionals are executed and if all the conditions are evaluated to false then the default statement is executed.

Note : Else statement is not necessary in any of conditionals while you can use it if you want to add a default statement to your decision making.

Syntax :

Below is the Syntax of an If ElseIf- Else conditional statement :

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 :

Program to check greatest number or equality among two numbers.

Private Sub if_demo()
   Dim x As Integer
   Dim y As Integer
    
   x = 25
   y = 25
    
   If x > y Then
      MsgBox "X is Greater than Y"
   ElseIf y > x Then
      Msgbox "Y is Greater than X"
   Else
      Msgbox "X and Y are EQUAL"
   End If
End Sub

Output :



4) Nested if Statements

An if or ElseIf statements inside another If or ElseIf statement(s). The inner if statements are executed based on the outermost if statements. This enables Script to handle even more complex conditions with ease.


Syntax :

Below is the Syntax of a Nested If conditional statement :

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


Flow Diagram




Example :

Program to check number is positive or negative , if positive then check whether number is prime,composite or even-odd prime.

Private Sub nested_if()
   Dim a As Integer
   a = 32
  
   If a > 0 Then
      MsgBox "The Number is a POSITIVE Number"
      
        If a = 1 Then
           MsgBox "The Number is Neither Prime NOR Composite"
        ElseIf a = 2 Then
           MsgBox "The Number is the Only Even Prime Number"
        ElseIf a = 3 Then
           MsgBox "The Number is the Least Odd Prime Number"
        Else
           MsgBox "The Number is NOT 0,1,2 or 3"
        End If
   ElseIf a < 0 Then
      MsgBox "The Number is a NEGATIVE Number"
   Else
      MsgBox "The Number is ZERO"
   End If
End Sub

Output :

This out is given when first if is encountered and condition is true so,it has gone in nested if that is further statements.



This is the output for nested if condition, all conditions are evaluated and when nothing matched correct it has give the else statement.





5) Switch Statement

When a programmer wants 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 is being switched ON based on each case i.e Statements under any Case are executed if they match correctly.
Case Else statement is executed if the 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 :

Below is the Syntax of a Switch Case conditional statement :

Select Case expression
   Case expressionlist1
      statement1
      statement2
      ....
      ....
      statement1n
   Case expressionlist2
      statement1
      statement2
      ....
      ....
   Case expressionlistn
      statement1
      statement2
      ....
      ....   
   Case Else
      elsestatement1
      elsestatement2
      ....
      ....
End Select



Flow Diagram



Example :

Program to find the type of integer with the help of a function.

Private Sub switch_demo()
   Dim MyVar As Integer
   MyVar = 3
  
   Select Case MyVar
      Case 1
         MsgBox "The Number is the Least Composite Number"
      Case 2
         MsgBox "The Number is the only Even Prime Number"
      Case 3
         MsgBox "The Number is the Least Odd Prime Number"
      Case Else
         MsgBox "Unknown Number"
   End Select
End Sub

Output :







Post a Comment

Previous Post Next Post