Java Nesting selection structures

Associate
Joined
28 Feb 2008
Posts
20
Hi all,

For a piece of college work i have been as to nest a selection structure within another selction structure. Ive been serching google with no avail. if anyone could shed any light on this it would be amazing :)

Cheers

Dave
 
A "selection" structure is a fancy name for an "if" statement which often confuses people so a nested selection structure would be a set of nested "if" statements.
 
I'm guessing an interation control structure would be refering to a "for" or "foreach" loop (a foreach loop is also known as an enhanced for loop in Java). Again these sounds like terms that someone might ask me about in a job interview, no one in the real world refers to these constructs by these names, well at least anyone I know of.
 
Hey guys being a bit ofa noob here doing this java script cource work.
I know its a big ask but if i wanted to have a for loop with a 'if' selection nested withing it how would they be linked together? bit cheecky i know but if anyone one has five min to write me a v simple layout id be so happy :)

Dave
 
in psuedo code:

for each object in the array...
if the object.property is equal to "blah"
do this
else
do that

any simple java book should give you an example of this, I'm not writing it with the correct syntax for you :p
 
Basically, when you have a loop (any kind; for or while), everything inside that loop is executed on each iteration, so the if clause will be evaluated each time it iterates over the loop.

For example:
Code:
for (int i = 0; i < 5; i++)
{
    if (something)
    {
        // Do some stuff.
    }
}

…is equivalent to this:

Code:
// First iteration.
if (something)
{
    // Do some stuff.
}

// Second iteration.
if (something)
{
    // Do some stuff.
}

// Third iteration.
if (something)
{
    // Do some stuff.
}

// Fourth iteration.
if (something)
{
    // Do some stuff.
}

// Fifth iteration.
if (something)
{
    // Do some stuff.
}
 
Hey guys being a bit ofa noob here doing this java script cource work.
I know its a big ask but if i wanted to have a for loop with a 'if' selection nested withing it how would they be linked together? bit cheecky i know but if anyone one has five min to write me a v simple layout id be so happy :)

Dave

Code:
for (initialiser; test expression; post statement) {
  if (something) {
    // Do something 
  } 
}
 
Last edited:

Being pedantic, its actually equivalent to this:

Code:
int i = 0 ;
// First iteration.
if (something)
{
    // Do some stuff.
}

i++ ;
// Second iteration.
if (something)
{
    // Do some stuff.
}

i++ ;
// Third iteration.
if (something)
{
    // Do some stuff.
}

i++ ;
// Fourth iteration.
if (something)
{
    // Do some stuff.
}

i++ ;
// Fifth iteration.
if (something)
{
    // Do some stuff.
}

Im a bit OCD about things like that....
 
Well it just clears it up as you said Java in the thread title, the example people have posted have been Java.
 
Back
Top Bottom