What do you call nested If...Then search loops?

  • Thread starter Thread starter GY2
  • Start date Start date
Again, I think you're assuming that the if then structure will only
test one variable. In this case you could rewrite it using a Select
Case statement. But with multiple variables if thens are clearly better
suited for this. Take the following adaptation of the OP's structure:

dim a, b, c as integer

a = 5
b = 7
c = 7

if a > 4 then
if b > 6 then
if c > 6 then
msgbox("found it")
end if
end if
end if

How exactly would you rewrite this with a select case?

Thanks,

Seth Rowe


Brian said:
David said:
Please re-read the original post. The OP example shows 3 independent tests,
not 3 possible values of an expression.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


I did re-read it before i replied:

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next

In this case there's only one "Found it". The assumption is that the
elses will feed the other cases. And that is perfect for a CASE
statement.

B.
 
For what you have shown, I would suggest "andalso", i.e.,

if test1 andalso test2 andalso test3 then

end if
 
Well, the code has already been written in VB 6 years ago. All I'm trying do
to is document it and find out what the name of this kind of structure is.
Thanks, though. It's an interesting discussion.

Dennis said:
For what you have shown, I would suggest "andalso", i.e.,

if test1 andalso test2 andalso test3 then

end if

--
Dennis in Houston


GY2 said:
I writing some documentation and I want to describe a common code
structure
which is used to step through all the items in a collection (e.g. each
file
in a subdirectory) while applying more and more restrictive filters so
that
only the desired items can fall all the way through. This method is so
obvious and common it must have a name. What is it or at least, what is
the
best (short) way to describe it?

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next
 
If you are using VB 2005, you could rewrite this as

For Each [file or whatever] In [some collection]
if not [test 1] then Continue For
if not [test 2] then Continue For
...
[Found it]
Next

This has the benefit of eliminating the confusion that results when adding
an additional test. If you don't have VB 2005, I would rewrite as

For Each [file or whatever] In [some collection]
if passesfilters(file) then [Found it]
Next

Private function PassesFilters(file as object) as boolean
if not [test 1] then return false
if not [test 2] then return false
if not [test 3] then return false
return true
end function

Again, this eliminates the issue of screwing up the nesting when you change
the number or nature of the tests.

Mike.
 
rowe_newsgroups said:
Again, I think you're assuming that the if then structure will only
test one variable. In this case you could rewrite it using a Select
Case statement. But with multiple variables if thens are clearly better
suited for this. Take the following adaptation of the OP's structure:

dim a, b, c as integer

a = 5
b = 7
c = 7

if a > 4 then
if b > 6 then
if c > 6 then
msgbox("found it")
end if
end if
end if

How exactly would you rewrite this with a select case?

Thanks,

Seth Rowe


Brian said:
David said:
Please re-read the original post. The OP example shows 3 independent tests,
not 3 possible values of an expression.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


:


David Anton wrote:
We'll have to agree to disagree then. I find 'Select Case' more appropriate
where you care about many possible values of an expression.

Yes, which is the case in the OPs given example.

B.


I did re-read it before i replied:

For Each [file or whatever] In [some collection]
If [test 1] Then
If [test 2] Then
If [test 3] Then
[Found it]
End If
End If
End If
Next

In this case there's only one "Found it". The assumption is that the
elses will feed the other cases. And that is perfect for a CASE
statement.

B.
Again, I think you're assuming that the if then structure will only
test one variable. In this case you could rewrite it using a Select

Yes, i was assuming one variable. That is the usual case.

B.
 
Back
Top