Table of contents
1.
Introduction
2.
If-Then Statement
3.
Select Case
3.1.
1. Declaration of Variables
3.2.
2. Initialization of Variables
3.3.
3. Addition of Select Case Structure
3.4.
4. Result
4.
Mod Operator
5.
Prime Number Checker
5.1.
1. Declaration of Variables
5.2.
2. Initialization of Variables
5.3.
3. Starting of Next Loop
5.4.
4. Calculating the number of Divisors
5.5.
5. Closing the loop
5.6.
6. Displaying the MsgBox
6.
Logical Operators
6.1.
Logical AND Operator
6.2.
Logical OR Operator
6.3.
Logical NOT Operator
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

If Then Statement

Author Prachi Singh
0 upvote

Introduction

If-Then statement needs the execution of specific lines of code when a particular condition is met.

If-Then Statement

Add the following lines of code and place the command button on the worksheet.

Dim marks As Integer, result As String
marks = Range("A1").Value
If marks >= 40 Then result = "pass"
Range("B1").Value = result

Explanation: If marks are greater than or equal to 40, the result will be passed.

Result:

Select Case

One can use the Select Case structure instead of multiple if-then statements.

Add the following lines of code and place the command button on the worksheet.

1. Declaration of Variables

Dim marks As Integer, result As String

2. Initialization of Variables

marks = Range("A1").Value

3. Addition of Select Case Structure

Select Case marks
    Case Is >= 80
        result = "very good"
    Case Is >= 70
        result = "good"
    Case Is >= 60
        result = "sufficient"
    Case Else
        result = "insufficient"
End Select

4. Result

Range("B1").Value = result

Mod Operator

The remainder of a division is the resultant of the mod operator.

Add the following lines of code and place the command button on the worksheet

MsgBox 6 Mod 2

Result:

Prime Number Checker

This is used to find whether the given number is prime or not.

Add the following lines of code and place the command button on the worksheet.

1. Declaration of Variables

Dim divisor As Integer, num As Long, i As Long

2. Initialization of Variables

divisor = 0
num = InputBox("Enter a number")

3. Starting of Next Loop

For i = 1 To num

4. Calculating the number of Divisors

If num Mod i = 0 Then
    divisor = divisor + 1
End If

5. Closing the loop

Next i

6. Displaying the MsgBox

If divisor = 2 Then
    MsgBox number & "  a prime number"
Else
    MsgBox number & "  not a prime number"
End If

Result:

Logical Operators

The most commonly used logical operators are AND, OR, NOT.

Logical AND Operator

Add the following lines of code and place the command button on the worksheet.

Dim marks1 As Integer, marks2 As Integer, result As String
marks1 = Range("A1").Value
marks2 = Range("B1").Value
If marks1 >= 50 And marks2 > 1 Then
    result = "pass"
Else
    result = "fail"
End If
Range("C1").Value = result

Result:

Logical OR Operator

Add the following lines of code and place the command button on the worksheet.

Dim marks1 As Integer, marks2 As Integer, result As String
marks1 = Range("A1").Value
marks2 = Range("B1").Value
If marks1 >= 50 Or  marks2 > 1 Then
    result = "pass"
Else
    result = "fail"
End If
Range("C1").Value = result

Result:

Logical NOT Operator

Add the following lines of code and place the command button on the worksheet

Dim marks1 As Integer, marks2 As Integer, result As String
marks1 = Range("A1").Value
marks2 = Range("B1").Value
If marks1 >= 50 And Not marks2 = 1 Then
    result = "pass"
Else
    result = "fail"
End If
Range("C1").Value = result

Result:

FAQs

1. What is the use of the If-Then statement?

If-Then statement needs the execution of specific lines of code when a particular condition is met.

2. What is the resultant of the Mod Operator?

The remainder of a division is the resultant of the mod operator.

3. Name different logical operators.

The three different logical operators are AND, OR, NOT.

4. What is the use of the Prime Checker?

This is used to find whether the given number is prime or not.

5. Which is the better option for using multiple if-then statements?

Select Case Structure

Key Takeaways

Congratulations on finishing the blog!! After reading this blog, you will grasp the concept of the If-Then statement.

If you are preparing yourself for the top tech companies, don't worry. Coding Ninjas has your back. Visit this link for a well-defined and structured material that will help you provide access to knowledge in every domain.

Live masterclass