What is the proper control structure to impose here?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Control Structure
Click For Summary

Discussion Overview

The discussion revolves around the proper control structures to use in programming when dealing with multiple conditions that are not mutually exclusive. Participants explore various approaches to structuring conditional statements, focusing on readability, efficiency, and adherence to programming principles.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a method using multiple if statements to handle conditions independently, expressing concern about redundancy in checking conditions.
  • Another participant suggests a variation that uses a boolean variable to simplify the structure but is challenged on its equivalence to the original approach.
  • Several participants discuss the implications of using else-if constructs, noting that they may not capture all scenarios when conditions are not mutually exclusive.
  • A participant proposes a scheme that uses a skip flag to determine whether to execute a third block of code, which is positively received by others.
  • Another participant introduces a switch statement based on a combined condition value, aiming to clarify the logic but acknowledges potential confusion in its use.
  • One participant emphasizes the importance of comments in complex logical structures to aid understanding.
  • Adherence to the DRY (Don't Repeat Yourself) principle is mentioned as a consideration in structuring code.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to structuring conditional statements, with no consensus reached on a single "proper" method. Several competing views and techniques are presented, reflecting the complexity of the problem.

Contextual Notes

Some participants note the importance of clarity and maintainability in code, particularly when conditions are not mutually exclusive, suggesting that additional comments may be necessary to explain logic. There are also mentions of potential oversights, such as variable initialization.

Who May Find This Useful

Programmers and software developers interested in control structures, code readability, and best practices in conditional logic may find this discussion beneficial.

SlurrerOfSpeech
Messages
141
Reaction score
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
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
}
 
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.
 
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   Reactions: jtbell and Ibix
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.
 
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.
 
Ibix said:
PF needs two like buttons for this.

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

if( condition1 ){
do one;
skip3=true;
}
if( condition2 ){
do two;
skip3=true;
}
if( ! skip3 ){
do three;
}
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
11K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K