Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

When to use Nested IF?

Posted on 1999-06-23
2
Medium Priority
?
162 Views
Last Modified: 2010-05-02
When would I nest my If's as opposed to making seperate algorithms?

if a = 2 then
   msgbox "It equals 2."
else
   if a = 3 then
        msgbox "It equals 3."
   else
        if a = 4 then
             msgbox "It equals 4."
        end if
    end if
end if

as opposed to

if a = 2 then
     msgbox "it equals 2."
end if

if a = 3 then
     msgbox "it equals 3."
end if

if a = 4 then
     msgbox "it equals 4."
end if

0
Comment
Question by:rb982996
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Expert Comment

by:ture
ID: 1519314
rb982996,

If you use a single If structure or a Select Case clause, only the first 'matching' code will run. If you use several If's, one or many codes can run. Look at this code:


Dim a
a = 3


'SELECT CASE STRUCTURE

Select Case a
  Case Is < 10           'This will run
    MsgBox "less than 10"
  Case Is > 100
    MsgBox "more than 100"
  Case Int(a)            'But not this one
    MsgBox "integer"    
  Case Else
    MsgBox "whatever"
End Select


'IF...ELSEIF...ELSE...ENDIF STRUCTURE

If a < 10 Then           'This will run
  MsgBox "less than 10"
ElseIf a > 100 Then
  MsgBox "more than 100"
ElseIf a = Int(a) Then   'But not this one
  MsgBox "integer"
Else
  MsgBox "Whatever"
End If


'SEVERAL CONSECUTIVE IFS

If a < 10 Then           'This will run
  MsgBox "less than 10"
End If

If a > 100 Then
  MsgBox "more than 100"
End If

If a = Int(a) Then       '...and this also
  MsgBox "integer"
End If


I hope I haven't messed this up completely...

Ture Magnusson
Karlstad, Sweden
0
 
LVL 2

Accepted Solution

by:
dhodge earned 150 total points
ID: 1519315
If your code changed the value of 'a' in the first branch then the subsequent branches could be called.  

eg.

if a = 2 then
   msgbox "It equals 2."
   ...
   a = 3
endif

if a = 3 then
   msgbox "It equals 3."
end if

This should be considered.  You may or may not want that to happen.  Speedwise there is not much between a case and a well structured set of if's.  

However the great saving comes in ease of reading and maintainability, most people (me included) would prefer to see a select case, its much neater and clearer.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question