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

Page 2 / 2
  RSS

Robo Pi
(@robo-pi)
Robotics Engineer Moderator
Joined: 10 months ago
Posts: 1590
2019-07-23 5:21 am  
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)
Active Member
Joined: 3 months ago
Posts: 14
2020-01-10 7:05 am  

@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 3 months ago by jrbdeveloper

The unexamined life is not worth living - Socrates


ReplyQuote
Pugwash
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-01-10 12:09 pm  

@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.

SteveC - Topple Rudd Poltman


ReplyQuote
jrbdeveloper
(@jrbdeveloper)
Active Member
Joined: 3 months ago
Posts: 14
2020-01-10 3:16 pm  

@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
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-01-10 3:36 pm  

@jrbdeveloper

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

SteveC - Topple Rudd Poltman


ReplyQuote
jrbdeveloper
(@jrbdeveloper)
Active Member
Joined: 3 months ago
Posts: 14
2020-01-10 3:58 pm  

@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
(@pugwash)
Prominent Member
Joined: 10 months ago
Posts: 969
2020-01-10 4:07 pm  

@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.

 

SteveC - Topple Rudd Poltman


ReplyQuote
Page 2 / 2

Please Login or Register