Home All Groups Group Topic Archive Search About

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

Author
8 Nov 2006 1:50 PM
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

Author
8 Nov 2006 2:03 PM
rowe_newsgroups
Ugly? :-)

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

Thanks,

Seth Rowe


GY2 wrote:
Show quoteHide quote
> 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
Author
8 Nov 2006 3:35 PM
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.

Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> wrote in message
news:1162994593.135240.169780@h48g2000cwc.googlegroups.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
>
Author
8 Nov 2006 3:22 PM
Brian Tkatch
GY2 wrote:
Show quoteHide quote
> 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.
Author
8 Nov 2006 3:30 PM
GY2
Thanks for the 'Nested-Ifs' language.

Show quoteHide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1162999342.700887.39800@e3g2000cwe.googlegroups.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.
>
Author
8 Nov 2006 4:02 PM
Tom Leylan
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.


Show quoteHide quote
"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> wrote in message
news:1162999342.700887.39800@e3g2000cwe.googlegroups.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.
>
Author
8 Nov 2006 4:44 PM
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.

Show quoteHide quote
"Tom Leylan" <gee@iamtiredofspam.com> wrote in message
news:uMRMO90AHHA.2316@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" <Maxwell_Sm***@ThePentagon.com> wrote in message
> news:1162999342.700887.39800@e3g2000cwe.googlegroups.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.
>>
>
>
Author
8 Nov 2006 3:51 PM
David Anton
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


Show quoteHide quote
"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

>
>
>
Author
8 Nov 2006 3:59 PM
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.

Show quoteHide quote
"David Anton" <DavidAn***@discussions.microsoft.com> wrote in message
news:904B22ED-2624-4A6E-BB42-B421623C15D4@microsoft.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
>>
>>
>>
>>
Author
8 Nov 2006 4:09 PM
Brian Tkatch
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.
Author
8 Nov 2006 4:24 PM
David Anton
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


Show quoteHide quote
"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.
>
>
Author
8 Nov 2006 6:56 PM
Brian Tkatch
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.
Author
8 Nov 2006 7:27 PM
David Anton
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


Show quoteHide quote
"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.
>
>
Author
8 Nov 2006 9:22 PM
GY2
Exactly.

Show quoteHide quote
"David Anton" <DavidAn***@discussions.microsoft.com> wrote in message
news:CB5814BF-A2A5-4DE1-8650-7708590E9B4A@microsoft.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.
>>
>>
Author
9 Nov 2006 12:38 AM
Brian Tkatch
David Anton wrote:
Show quoteHide quote
> 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.
Author
9 Nov 2006 1:13 AM
rowe_newsgroups
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:
Show quoteHide quote
> 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.
Author
9 Nov 2006 5:24 AM
Brian Tkatch
rowe_newsgroups wrote:
Show quoteHide quote
> 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.
Author
8 Nov 2006 5:03 PM
rowe_newsgroups
> 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:
Show quoteHide quote
> 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
> > 
> >
> >
> >
Author
8 Nov 2006 5:18 PM
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.

Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> wrote in message
news:1163005393.909022.147040@e3g2000cwe.googlegroups.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
>> >
>> >
>> >
>> >
>
Author
8 Nov 2006 5:57 PM
Cor Ligthert [MVP]
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

Show quoteHide quote
"GY2" <2muchspam@wherever.com> schreef in bericht
news:%23klOPzzAHHA.4592@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
>
>
>
Author
8 Nov 2006 6:02 PM
GY2
Thanks Cor. Yes, the format in VB can be pretty nice if you're into
'literate programming'.

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:udTeP81AHHA.4292@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" <2muchspam@wherever.com> schreef in bericht
> news:%23klOPzzAHHA.4592@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
>>
>>
>>
>
>
Author
8 Nov 2006 7:18 PM
RobinS
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.



Show quoteHide quote
"GY2" <2muchspam@wherever.com> wrote in message
news:%23klOPzzAHHA.4592@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
>
>
>
Author
9 Nov 2006 12:39 AM
Brian Tkatch
RobinS wrote:
> Just to add another voice to the cacophony,
> sometimes you just HAVE to use nested Ifs.

Yes, sometimes. Most cases not.

B.
Author
9 Nov 2006 2:21 AM
Dennis
For what you have shown, I would suggest "andalso", i.e.,

if test1 andalso test2 andalso test3 then

end if

--
Dennis in Houston


Show quoteHide quote
"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

>
>
>
Author
9 Nov 2006 3:05 AM
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.

Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote in message
news:CE5CB189-C53D-4DF1-A9B7-B7B4D5A4B6AF@microsoft.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
>>
>>
>>
>>
Author
9 Nov 2006 5:09 AM
Michael D. Ober
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.

Show quoteHide quote
"GY2" <2muchspam@wherever.com> wrote in message
news:%23klOPzzAHHA.4592@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
>
>
>