Notifications
Clear all

I need help with what I think is 1 line of code

22 Posts
5 Users
1 Likes
5,201 Views
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @recycled-roadkill

I'm in an unfortunate area where I know some stuff but often too advanced for basic stuff and too basic for more advanced stuff.

I feel like I've been in that status for decades. ? 

I think it's always going to be like this.   The more we learn the more we want to do and the more we realize we don't know.   I'm currently working with an old program I wrote quite a few years ago.  Over a decade ago actually.  Strangely I was able to catch up where I left off really quickly and even realized that I had learned quite a bit in the interim.  I'm way better at programming now than I was back then.  Yet at the same time, I'm nowhere near where I would like to be.

So it seems to be a never-ending cycle.   I'm always behind where I would like to be.

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
jrbdeveloper
(@jrbdeveloper)
Member
Joined: 4 years ago
Posts: 12
 

@pugwash

to re-write the switch statement as an if else you would want to write it as

switch(val){
  case G:
case UNKNOWN_G:
    go();
break;
case R:
case UNKNOWN_R:
stop();
break;
default:
break;
}
if(val == G || val == UNKNOWN_G) {
go();
} else if (val == R || val == UNKNOWN_R) {
stop();
}

I hadn't seen an answer that closes the loop on this so I thought I'd jump in. Im new to the forums and eager 🙂

This post was modified 4 years ago by jrbdeveloper

The unexamined life is not worth living - Socrates


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@jrbdeveloper

Nice solution.

I wasn't sure whether the following code would compile, let alone run without some serious runtime errors.

switch(val){
  case G:
case UNKNOWN_G:
    go();
break;
case R:
case UNKNOWN_R:
stop();
break;
default:
break;
}

Sometimes it is difficult to see inside the head of whoever wrote a piece of code.


   
ReplyQuote
jrbdeveloper
(@jrbdeveloper)
Member
Joined: 4 years ago
Posts: 12
 

@pugwash

Yep I hear ya. The switch statement does look like it would execute properly though. Having an empty case above one that isn't just means the one thats empty will fall through in an OR fashion to the case below. Similar to the if statement I replied with. The syntax looks correct, though the last case (default) isn't necessary. Default is like a final else in an if/else if statement. Anywho... thanks for letting me jump in on the thread. I wasn't sure by reading if you were looking to close the loop on this or not.

The unexamined life is not worth living - Socrates


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@jrbdeveloper

Is there any way of evaluating an expression in switch..case, or am I stuck with absolute values?


   
ReplyQuote
jrbdeveloper
(@jrbdeveloper)
Member
Joined: 4 years ago
Posts: 12
 

@pugwash

Honestly I think that might be a misuse of the "switch" statement. Probably anything more complex than an evaluation for truth or simple comparison would warrant the use of an "if/else if" statement. Also a lot of what goes into your decision is about readability. Some might find that the "switch" as it is in your example is easier to read than the "if/else if" I responded with and vice versa. In the two examples I'd go with whats easier to read.

The unexamined life is not worth living - Socrates


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@jrbdeveloper

Some might find that the "switch" as it is in your example

I most certainly did NOT advocate using switch case and it was not MY example. The example quoted, I seriously consider as very UGLY code and that is why liked your "if" conditional.

 


   
jrbdeveloper reacted
ReplyQuote
Page 2 / 2