x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 214

# C++ For Loop

Hi experts,

I have a vector, vectorOfRadios of Radio objects with each object containing one boolean member variable, called IsRadioOn. I loop through each Radio in vectorOfRadios starting at the first Radio and looking ahead to the next radio i.e. vectorOfRadios.at(n+1) and check if the radios are either of the following options and based on those options call different functions:

for (int n = 0; n < vectorOfRadios.size()-1; n++)
{

for (int j = 0; j < size; ++j)
{
int x = 0;
int y = 0;
if ((bFirstIsOn == true) && (bSecondIsOn == true))
{
// A, B, C, D and E are functions that return an integer.
x = A();
y = B();
}
else if  ((bFirstIsOn == true && (bSecondIsOn == false))
{
x = A();
y = D();
}
else if  ((bFirstIsOn == false) && (bSecondIsOn == true))
{
x = B();
y = C();
}
else if  ((bFirstIsOn == false) && (bSecondIsOn == false))
{
x = B();
y = D();
}

// Do other stuff here using variables x and y
// ...
}
}

Is there a more elegant method for doing the above?

Thank you,
j.
0
XPUSR
• 3
2 Solutions

Commented:
if (bFirstIsOn == true)
x = A();
else
x = B();

if (bSecondIsOn == false)
y = D();
else
{
if(bFirstIsOn == true)
y = B();
else
y = C();
}
0

Commented:
or you could try with the conditional iif.

x = (bFirstIsOn) ? A() : B();
y = (!bSecondIsOn) ? D() :;
y = (!bSecondIsOn && bFirstIsOn) ? B() : C();

0

Commented:
Sorry try this:

x = (bFirstIsOn) ? A() : B();
y = (!bSecondIsOn) ? D() : 0;
y = (bSecondIsOn && bFirstIsOn) ? B() : C();

-Brian
0

Commented:
An alternative:

if( bSecondIsOn ) {
if( bFirstIsOn ) {
x = A();
y = B();
}
else {
x = B();
y = C();
}
}
else {
y = D();
}

Both are correct. BrianGeFF719's is more concise but mine is more versatile.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.