# C++ shorthand "if then" issue

## Recommended Posts

why does the following not work ?
c==0?continue:break;
There is nothing behind of before it which is affecting the compilation. This is the error:
Quote:
 xyuri@bsdmachine\$ make g++ -c a1mstrncmp.cpp g++ -c mstrncmp.cpp mstrncmp.cpp: In function int mstrncmp(const char*, const char*, size_t)': mstrncmp.cpp:29: error: expected primary-expression before "continue" mstrncmp.cpp:29: error: expected :' before "continue" mstrncmp.cpp:29: error: expected primary-expression before "continue" mstrncmp.cpp:29: error: expected ;' before "continue" *** Error code 1 Stop in /usr/home/xyuri/code/se93a1.
is it not possible to use break / continue type statements in these things ? Edit: ok, for the sake of supplying sufficient information here is the whole code ... it is a replacement function for the built in one (strncmp) for an assignment ... this doesnt help me with me assignment because as you can see it is already done and working, i'm just tidying up syntax now like i have done for the other 5 functions we needed to write.
//|
//| mstrlen.cpp
//|
//| Author.: Michael Dawson
//| Date...: 10/03/2006
//|
//| Purpose: .
//|

#include "mstr.h"

//
// Purpose: simulate the effect of strncmp
//          function in standard library.
// Pre....: s1 and s2 must be a properly terminated c string.
// Post...: Returned value indicates difference.
//
int mstrncmp(const char *s1, const char *s2, size_t n)
{
int c;

for (int i=0 ; i<n ; i++) {
if (*s1=='\0' || *s2=='\0') break;
c = *s1++ - *s2++;
//if (c == 0) { continue; } else { break; }
c==0?continue:break;
}

return c;
}`

What is 'c' ?

##### Share on other sites
Quote:
 Original post by DrEvilWhat is 'c' ?

Check above, i added the whole file :) thank you for your input.

##### Share on other sites
The only thing i can suggest is putting then c==0 inside brackets, ie (c==0).

Dave

##### Share on other sites
Quote:
 Original post by DaveThe only thing i can suggest is putting then c==0 inside brackets, ie (c==0).Dave
That didnt help any ;)

##### Share on other sites
It appears keywords are not compatible as the operands in the ternary operator. Interesting.

I'd suggest just sticking with the if/then. It's easier to read anyways in most cases.

##### Share on other sites
The operator ?: is an expression, it's not a statement. You can use it to select between one of its subexpressions. You cannot use it to select between different statements.

##### Share on other sites
What's wrong with 'if (c) break;'? You write less, and it does the same thing...

[EDIT]
What nmi said is also true. What you're doing is the same as trying to assign a value to nothing, so obviously it wont work.

##### Share on other sites
Untrue, ?: is not limited to values. You can put statements in there just fine. In this case it appears that for some reason continue and break don't want to work.

Edit: some examples

MyFunc* are void return value functions

bool b = true, c = false;
b ? MyFunc1() : MyFunc2();

// Sometimes people nest them as well, in loop controls and such. It's pretty hideous but it works.
b ? c ? MyFunc1() : MyFunc2() : MyFunc3();

##### Share on other sites
Quote:
 Original post by nmiThe operator ?: is an expression, it's not a statement. You can use it to select between one of its subexpressions. You cannot use it to select between different statements.

Quoted for emphasis.

The conditional operator is not a "shorthand". It serves a significantly different purpose from if statements.

Quote:
 Original post by DrEvilUntrue, ?: is not limited to values. You can put statements in there just fine. In this case it appears that for some reason continue and break don't want to work.

You can put expressions, not statements - a function call is an expression. continue and break simply are not appropriate here, and neither would blocks, while loops, if statements, goto nor any other kind of statement.

b ? MyFunc1() : MyFunc2() is an expression.
b ? MyFunc1() : MyFunc2(); is a statement.

##### Share on other sites
Thanks for the clarification. I was mainly responding to the edit part of Gorax post.

##### Share on other sites
There is, however, one exception:
throw statements are valid rvalues for the conditional operator
(or should be for most compilers anyway; I'm not certain on the official standing on this)

##### Share on other sites
Quote:
 Original post by DrEvil// Sometimes people nest them as well, in loop controls and such. It's pretty hideous but it works.b ? c ? MyFunc1() : MyFunc2() : MyFunc3();

Yea. Code like that would make me kill the individual working with me. At the very least, he would find his IDE refused to load for the next few years... while he was in the same building as I was. :P

I find ? : to be convenient shorthand in a very few situations. The fact that C# 2.0 introduced the ?? operator makes me really irritable...

##### Share on other sites
What's ?? do in C# ?

##### Share on other sites
Quote:
 Original post by DrEvilWhat's ?? do in C# ?

a ?? b is equivalent to (a != null) ? a : b

##### Share on other sites
Damn, they actually deemed that worthy of its own operator? That's wierd. Thanks for the info.

##### Share on other sites
Quote:
 Original post by DrEvilDamn, they actually deemed that worthy of its own operator? That's wierd. Thanks for the info.

I believe GCC allows the use of a ?: b as an extension, to mean a ? a : b, which, I admit isn't quite the same thing (checks for truth rather than for null).

##### Share on other sites
Quote:
Original post by Fruny
Quote:
 Original post by DrEvilDamn, they actually deemed that worthy of its own operator? That's wierd. Thanks for the info.

I believe GCC allows the use of a ?: b as an extension, to mean a ? a : b.

Interesting. Does it evaluate a twice?

##### Share on other sites
Not that I would recommend this for anything but fun, but I experimented with the conditional operator with multiple statements (output and assignment) and I had to do it like this:

((x%2)==0)?(cout<<"foo",y++):(cout<<"bar",z++);

This was in a for loop and the massive, ugly conditional above would test the sentinel and do one thing when it was even and the other when it was odd. Basically, it boils down to putting what you want it to do in parens, separating multiple statements with a comma.

This is very hairy code and I would wanna kill the author if I had to maintain it as part of any serious project, but it will work. Just use the regular if/else convention.

##### Share on other sites
Quote:
 Original post by ZahlmanInteresting. Does it evaluate a twice?

No.

Quote:
 Original post by Photonman((x%2)==0)?(cout<<"foo",y++):(cout<<"bar",z++);

There is very little reason to ever use a conditional expression as a standalone statement.
cout << "Foo is " << (foo.enabled() ? "enabled" : "disabled") << endl; is more appropriate usage in my eyes.

Edit: Fixed precedence. See following post.

[Edited by - Fruny on March 15, 2006 1:54:15 PM]

##### Share on other sites
Quote:
 Original post by Frunycout << "Foo is " << foo.enabled() ? "enabled" : "disabled" << endl; is more appropriate usage in my eyes.

Wouldn't that need brackets around the conditional? I thought the ?: operator had very low precedence...

John B

##### Share on other sites
Quote:
 Original post by JohnBSmallWouldn't that need brackets around the conditional? I thought the ?: operator had very low precedence...

And you thought correctly. Rating++;

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628346
• Total Posts
2982201

• 10
• 9
• 24
• 10
• 9