By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

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

P: n/a
GY2
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

Nov 8 '06 #1
Share this Question
Share on Google+
25 Replies


P: n/a
Ugly? :-)

I would guess it would be called "Short Circuit Evaluation via Nested
If Then blocks"

Thanks,

Seth Rowe
GY2 wrote:
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
Nov 8 '06 #2

P: n/a
GY2 wrote:
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
>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?
It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.

Nov 8 '06 #3

P: n/a
GY2
Thanks for the 'Nested-Ifs' language.

"Brian Tkatch" <Ma***********@ThePentagon.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
GY2 wrote:
>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
>>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?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.

Nov 8 '06 #4

P: n/a
GY2
Thanks. I like the 'short circuit evaluation' languague.

And btw, it often times doesn't look ugly at all, in fact the indented
structure makes it quite easy to grasp and to explain. It also doesn't run
too ugly either if the successive tests are ordered correctly. In fact it
can be pretty efficient because it minimizes the number of tests needed to
filter down to whatever one is seeking.

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Ugly? :-)

I would guess it would be called "Short Circuit Evaluation via Nested
If Then blocks"

Thanks,

Seth Rowe
GY2 wrote:
>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

Nov 8 '06 #5

P: n/a
The other two posters who replied are being hyper-critical. There is a place
for nested if's. And, contrary to what one of them said, 'Select Case' is
not a general purpose replacement for nested if's.
--
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
"GY2" wrote:
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

Nov 8 '06 #6

P: n/a
GY2
Thanks for the support, David. Yes, obviously Case statments would not work
for the multi-layered filtering I'm trying to document, but not receiving
perfect replies is no reason not to ask the question. It's good to be able
to receive something sent with a sense of humor in the same way. I thought
the one word reply--Ugly--was pretty funny.

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:90**********************************@microsof t.com...
The other two posters who replied are being hyper-critical. There is a
place
for nested if's. And, contrary to what one of them said, 'Select Case' is
not a general purpose replacement for nested if's.
--
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
"GY2" wrote:
>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


Nov 8 '06 #7

P: n/a
In the interest of not confusing GY2 even more... it is never called a
(SELECT) CASE structure. :-) All the tests need to be performed to
determine if [Found it] is reached. I'm not sure there is any term for this
except NESTED IF's. You could write it as a compound IF statement as well.

If [test 1] AndAlso [test 2] AndAlso [test 3] Then
[Found it]
End If

What matters most is what your tests consist of. If there are one or two
simple tests the compound form works fine. If there are a few more or they
are a bit more complicated then the nested format works. But if the tests
get "crazy" move all the comparison code into a method with a name that
identifies the set of tests like IsValidCustomer() and call that method from
within your For Each loop. Any overhead incurred by a method call is
outweighed by the advantage of skipping over the details when reading the
code and that the darn tests now have a name.

Oh and (when possible) place the most restrictive (i.e. unlikely to pass)
tests first. If earlier tests fail others don't have to execute.
"Brian Tkatch" <Ma***********@ThePentagon.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
GY2 wrote:
>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
>>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?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.

Nov 8 '06 #8

P: n/a
David Anton wrote:
The other two posters who replied are being hyper-critical.
Actually, i answered the question. I also added a comment, because it
cannot be stressed enough. In most cases of nested-ifs, a CASE
statement should be used.
There is a place for nested if's.
It is rare. Besides, there are limits on how nested an IF can be. And,
it is *very* hard to follow in real code.
And, contrary to what one of them said, 'Select Case' is not a general purpose replacement for nested if's.
Yes it is. It also makes the code more clear. It is a rare case where
CASE cannot be used.

---

It could be that i'm so senstivie about this right now becaise i'm
fixing someone's code who indeed used nested IFs where CASE would
clearly be the choice. And since there are no comments, it makes
understanding the code twice as hard.

B.

Nov 8 '06 #9

P: n/a
We'll have to agree to disagree then. I find 'Select Case' more appropriate
where you care about many 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
"Brian Tkatch" wrote:
David Anton wrote:
The other two posters who replied are being hyper-critical.

Actually, i answered the question. I also added a comment, because it
cannot be stressed enough. In most cases of nested-ifs, a CASE
statement should be used.
There is a place for nested if's.

It is rare. Besides, there are limits on how nested an IF can be. And,
it is *very* hard to follow in real code.
And, contrary to what one of them said, 'Select Case' is not a general purpose replacement for nested if's.

Yes it is. It also makes the code more clear. It is a rare case where
CASE cannot be used.

---

It could be that i'm so senstivie about this right now becaise i'm
fixing someone's code who indeed used nested IFs where CASE would
clearly be the choice. And since there are no comments, it makes
understanding the code twice as hard.

B.

Nov 8 '06 #10

P: n/a
GY2
Thanks, Tom, but actually I'm not confused at all. I've been writing code
for 35 years and understand the concepts involved and now it looks as though
I may have answered my own question reasonably well in my original Subject
line. I really just wondered if this type of very common, practical, and
useful algorithm had a name these days.

Of course you're exactly right that a Select Case structure is completely
inadequate for this situation in which >ALL< tests need to be performed in
order to run all the filters.

And you also see that, of course, the way to make this most efficient is to
start with the most restrictive tests and step into the successively less
restrictive filters with each deeper nested If-Then.

"Tom Leylan" <ge*@iamtiredofspam.comwrote in message
news:uM**************@TK2MSFTNGP04.phx.gbl...
In the interest of not confusing GY2 even more... it is never called a
(SELECT) CASE structure. :-) All the tests need to be performed to
determine if [Found it] is reached. I'm not sure there is any term for
this except NESTED IF's. You could write it as a compound IF statement as
well.

If [test 1] AndAlso [test 2] AndAlso [test 3] Then
[Found it]
End If

What matters most is what your tests consist of. If there are one or two
simple tests the compound form works fine. If there are a few more or
they are a bit more complicated then the nested format works. But if the
tests get "crazy" move all the comparison code into a method with a name
that identifies the set of tests like IsValidCustomer() and call that
method from within your For Each loop. Any overhead incurred by a method
call is outweighed by the advantage of skipping over the details when
reading the code and that the darn tests now have a name.

Oh and (when possible) place the most restrictive (i.e. unlikely to pass)
tests first. If earlier tests fail others don't have to execute.
"Brian Tkatch" <Ma***********@ThePentagon.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
>GY2 wrote:
>>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
>>>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?

It is usually called a (SELECT) CASE structure. For those who do not
use CASE, it is called "Nested-IFs", and also "Bad Programming".

B.


Nov 8 '06 #11

P: n/a
The other two posters who replied are being hyper-critical.

Hey be nice! (joking) I did answer the post and put why nested if's are
important (short circuit evaluation).

The reason I put "Ugly" was more to emphasize how nested if's can
easily become giant scary beasts if used incorrectely. In it's pure
form (like in the example) this structure is very easy to read and
maintain. A lot of times programmers (new ones especially) will add way
to much stuff to the if then tests - like multiple exit points, etc. -
and cause the structure to become confusing and unmaintainable. I'm
sure with your experience you know what I'm talking about :-)
Now-a-days you could even replace the pure if-then structures with just
AndAlso statements if you wanted to.

Am I making more sense now?

Thanks,

Seth Rowe
David Anton wrote:
The other two posters who replied are being hyper-critical. There is a place
for nested if's. And, contrary to what one of them said, 'Select Case' is
not a general purpose replacement for nested if's.
--
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
"GY2" wrote:
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


Nov 8 '06 #12

P: n/a
GY2
Sure you're making sense now, as you have all along--it's all good. I'm
working with old VB 6 code that's already a done deal. All I wanted to know
was what the hell to call those kinds of structures.

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>The other two posters who replied are being hyper-critical.

Hey be nice! (joking) I did answer the post and put why nested if's are
important (short circuit evaluation).

The reason I put "Ugly" was more to emphasize how nested if's can
easily become giant scary beasts if used incorrectely. In it's pure
form (like in the example) this structure is very easy to read and
maintain. A lot of times programmers (new ones especially) will add way
to much stuff to the if then tests - like multiple exit points, etc. -
and cause the structure to become confusing and unmaintainable. I'm
sure with your experience you know what I'm talking about :-)
Now-a-days you could even replace the pure if-then structures with just
AndAlso statements if you wanted to.

Am I making more sense now?

Thanks,

Seth Rowe
David Anton wrote:
>The other two posters who replied are being hyper-critical. There is a
place
for nested if's. And, contrary to what one of them said, 'Select Case'
is
not a general purpose replacement for nested if's.
--
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
"GY2" wrote:
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


Nov 8 '06 #13

P: n/a
GY2,

I always call it a nested If's inside a loop. As simple as you wrote it.

By the way about the comments, I assume that this is the most simple sample.
In any case nobody would use it like your sample of course.

You see that this is very well documentative when you see how ugly this is
mosly in all from C derived languages.

Cor

"GY2" <2m*******@wherever.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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

Nov 8 '06 #14

P: n/a
GY2
Thanks Cor. Yes, the format in VB can be pretty nice if you're into
'literate programming'.

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:ud**************@TK2MSFTNGP02.phx.gbl...
GY2,

I always call it a nested If's inside a loop. As simple as you wrote it.

By the way about the comments, I assume that this is the most simple
sample.
In any case nobody would use it like your sample of course.

You see that this is very well documentative when you see how ugly this is
mosly in all from C derived languages.

Cor

"GY2" <2m*******@wherever.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>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


Nov 8 '06 #15

P: n/a

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.

Nov 8 '06 #16

P: n/a
Just to add another voice to the cacophony,
sometimes you just HAVE to use nested Ifs.

But with VB2005, you can also use the new
OrElse and put all of those together.
Like C#'s usage of Else, it will stop
evaluating options when it hits a false one.

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

Of course, this assumes you don't care which test
it passed.

Robin S.

"GY2" <2m*******@wherever.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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

Nov 8 '06 #17

P: n/a
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
"Brian Tkatch" wrote:
>
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.

Nov 8 '06 #18

P: n/a
GY2
Exactly.

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:CB**********************************@microsof t.com...
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
"Brian Tkatch" wrote:
>>
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.


Nov 8 '06 #19

P: n/a
David Anton wrote:
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
"Brian Tkatch" wrote:

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.

Nov 9 '06 #20

P: n/a

RobinS wrote:
Just to add another voice to the cacophony,
sometimes you just HAVE to use nested Ifs.
Yes, sometimes. Most cases not.

B.

Nov 9 '06 #21

P: n/a
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 Tkatch wrote:
David Anton wrote:
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
"Brian Tkatch" wrote:
>
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.
Nov 9 '06 #22

P: n/a
For what you have shown, I would suggest "andalso", i.e.,

if test1 andalso test2 andalso test3 then

end if

--
Dennis in Houston
"GY2" wrote:
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

Nov 9 '06 #23

P: n/a
GY2
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" <De****@discussions.microsoft.comwrote in message
news:CE**********************************@microsof t.com...
For what you have shown, I would suggest "andalso", i.e.,

if test1 andalso test2 andalso test3 then

end if

--
Dennis in Houston
"GY2" wrote:
>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


Nov 9 '06 #24

P: n/a
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.

"GY2" <2m*******@wherever.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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


Nov 9 '06 #25

P: n/a

rowe_newsgroups wrote:
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 Tkatch wrote:
David Anton wrote:
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
>
>
"Brian Tkatch" wrote:
>

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.

Nov 9 '06 #26

This discussion thread is closed

Replies have been disabled for this discussion.