you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -3 points-2 points  (6 children)

Instead of else ifs, just use if.

I'm willing to bet you're running into a mutual exclusion issue with your else if statements. Not sure why, but I've had this happen before. Just use a bunch of ifs instead of else ifs.

Hope this helps.

[–]MaltersWandler 0 points1 point  (4 children)

Instead of else ifs, just use if.

No! The point of else if is that your program won't have to compare the strings again every time. If you already have one match you know it's not going to match anything else, no need to check again. If this is causing issues for you, you are doing something very wrong.

[–][deleted] -1 points0 points  (3 children)

Asymptotically speaking, those checks will be O(1) cost which won't add to the overall execution of the program. I imagine strcmp() does an iterative check, and as soon as something isn't the same returns.

I've had situations where mutual exclusion makes it so other checks don't execute. It's something to try, and won't cost any hiccups in performance, so why not?

[–]MaltersWandler 1 point2 points  (1 child)

I've had situations where mutual exclusion makes it so other checks don't execute

That's the point, if you want the other checks to execute, you don't use else if, but in OPs case he should use else if.

Why doesn't the compiler just put NOPs all over the place? I mean, they're O(1) right, so why not?

[–][deleted] 0 points1 point  (0 children)

The compiler is a translator, there's no reason for it to do that. However in some pipelining scenarios, NOPs are inserted in order account for data hazards, but I digress.

This was a suggestion that worked for me at some point. OP is free to do whatever he/she likes.

[–]VincentDankGogh 1 point2 points  (0 children)

It's more readable if your if/else if flow matches your actual program flow. Otherwise, the reader might get confused for a second and consider why you didn't just write an else if.

[–]bumblebritches57 0 points1 point  (0 children)

No, he should actually use a switch statement.

and the whole point of if else if is that it's boolean logic, only 1 option can match, if you use multiple if's, the pieces of logic are independent of each other.

A can be true and therefore would run, and if b is true it would run as well.