What is the proper control structure to impose here?

In summary: Oh well.Note that it assumes that the number of cases is small, and that you won't have something like "if condition1 && condition2 && condition3 && condition4" because that would be a long list of cases otherwise.In summary, there are multiple ways to handle situations where non-mutually exclusive conditions are checked multiple times, such as using boolean variables or a switch statement. It is important to add comments to explain the logic and follow the DRY principle when possible.
  • #1
SlurrerOfSpeech
141
11
I often encounter situations in my professional development like

Code:
if ( condition1 ) 
{
    // do this
}
if ( condition2 ) 
{
   // do that
}
if ( !condition1 && !condition2 ) 
{
   // do something else
}

and I handle it in exactly that manner. (Note: this is not an if-elseif-else situation. condition1 and condition2 are not mutually exclusive.)

However, my programming intuition says there's something wrong when it sees that the truthiness of condition1 and condition2 are each checked twice. The alternative

Code:
if ( !condition1 && !condition2 )
{
    // do something else
}
else
{
    if ( condition1 ) 
   {
       // do this
   }
   if ( condition2 ) 
   {
      // do that
    }
}

is even more like spaghetti code. I have yet a third way that gets rid of the redundant conditions by doing

Code:
int condval = 1 * (condition1 ? 1 : 0 ) + 2 * (condition2 ? 1 : 0);
if ( condval & 1 ) 
{
    // do this
}
if ( condval & 2 ) 
{
   // do that
}
if ( !condval  ) 
{
   // do something else
}

Yes, I've actually used the above thingy in real life. I feel bad for anyone who has to read my code and figure out what it does.

What is the proper way of handling this?
 
Technology news on Phys.org
  • #2
Not necessarilly better, but you could also do this.
Code:
bool x=c1;
if (x) {
  Do first thing
}
if (c2) {
  Do second thing
}
else if (!x) {
  Do third thing
}
 
  • #3
tAllan said:
Not necessarilly better, but you could also do this.
Code:
bool x=c1;
if (x) {
  Do first thing
}
if (c2) {
  Do second thing
}
else if (!x) {
  Do third thing
}

That is not equivalent. If you have !c1 && c2 then the third thing is executed.
 
  • #4
tAllan said:
Not necessarilly better, but you could also do this.
Code:
bool x=c1;
if (x) {
  Do first thing
}
if (c2) {
  Do second thing
}
else if (!x) {
  Do third thing
}

SlurrerOfSpeech said:
That is not equivalent. If you have !c1 && c2 then the third thing is executed.
I don't think so, if I understand what you're saying here.
If c1 is false (which means that x is false), the first if statement doesn't execute. If c2 is true, the second if statement executes, independent of whether x is true or false, but the third doesn't.

The third if clause executes if c2 is false and x is false.

The first two if statements both execute when x is true and c2 is true.

In case it's not completely obvious, the else if clause is associated with the second if clause.

In cases where the logic is contorted like this, due to conditions that are not mutually exclusive, it's really essential to add comments that explain why the logic is as it is.

It might be simpler to do this:
C:
if (c1 && c2) {
   // Handle case where both conditions are true
}
else (if c1) {
   // Handle case where only c1 is true
}
else if (c2) {
   // Handle case where only c2 is true
}
else {
   // Handle case where neither c1 nor c2 is true
}
 
  • Like
Likes jtbell and Ibix
  • #5
Mark44 said:
In cases where the logic is contorted like this, due to conditions that are not mutually exclusive, it's really essential to add comments that explain why the logic is as it is.
PF needs two like buttons for this.
 
  • #6
Mark44 said:
In case it's not completely obvious, the else if clause is associated with the second if clause.

Ah, I see now! Thanks.
 
  • #7
Ibix said:
PF needs two like buttons for this.

I added mine to help you out. :cool:
 
  • #8
I like this scheme:

if( condition1 ){
do one;
skip3=true;
}
if( condition2 ){
do two;
skip3=true;
}
if( ! skip3 ){
do three;
}
 
  • #9
FactChecker said:
I like this scheme:

if( condition1 ){
do one;
skip3=true;
}
if( condition2 ){
do two;
skip3=true;
}
if( ! skip3 ){
do three;
}

^ I'm stealing this from you.
 
  • #10
SlurrerOfSpeech said:
I have yet a third way that gets rid of the redundant conditions by doing
A slight adjustment:
C:
int condval = (condition1 ? 1 : 0 ) + (condition2 ? 2 : 0);
switch (condval) {
  case 3: // condition1 AND condition2
  ...
     break;
  case 2: // condition2 AND (not condition1)
  ...
    break;
  case 1: // condition1 AND (not condition2)
  ...
    break;
  case 0: // (not condition1) AND (not condition2)
  ...
    break;
  default: // How the hell did you get here?
  ...
    break;
    };
 
  • #11
One thing to consider is adherence to the DRY principle.
 
  • #12
SlurrerOfSpeech said:
^ I'm stealing this from you.
I was sloppy and forgot to initialize skip3 to false at the top.
 

What is the proper control structure to impose here?

The proper control structure to impose in a scientific experiment will depend on the specific research question and variables being studied. However, some common control structures include randomized controlled trials, matched pairs design, and factorial design.

How do I determine which control structure to use?

To determine the appropriate control structure for your experiment, you should consider factors such as the type of data you are collecting, the number of variables involved, and the level of control needed to minimize bias and other confounding factors. Consulting with other scientists or conducting a literature review can also provide insights into the most commonly used control structures in your field of study.

What is the purpose of a control group?

A control group serves as a comparison for the experimental group in a study. By keeping all variables constant for the control group, any changes observed in the experimental group can be attributed to the independent variable being studied. This helps to minimize the influence of confounding factors and increases the validity of the results.

Can I have multiple control groups in an experiment?

Yes, it is possible to have multiple control groups in an experiment. This can be useful when studying multiple treatments or interventions and comparing them to a common control group. However, it is important to ensure that the control groups are well-matched and that any differences between them are due to the independent variable being studied.

What are the potential limitations of using a control structure?

One potential limitation of using a control structure is that it may not accurately reflect real-world conditions. This can limit the generalizability of the results to a broader population. Additionally, controlling for certain variables may be difficult or impossible, which can introduce bias into the study. It is important for scientists to carefully consider these limitations when designing their experiments and interpreting the results.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
2
Replies
54
Views
4K
Replies
22
Views
902
Back
Top