Sign in to follow this  
xyuri

C++ shorthand "if then" issue

Recommended Posts

xyuri    166
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;
}

Share this post


Link to post
Share on other sites
xyuri    166
Quote:
Original post by Dave
The only thing i can suggest is putting then c==0 inside brackets, ie (c==0).

Dave
That didnt help any ;)

Share this post


Link to post
Share on other sites
DrEvil    1148
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 this post


Link to post
Share on other sites
nmi    978
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 this post


Link to post
Share on other sites
Gorax    202
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 this post


Link to post
Share on other sites
DrEvil    1148
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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by nmi
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.


Quoted for emphasis.

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

Quote:
Original post by DrEvil
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.


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 this post


Link to post
Share on other sites
ProPuke    466
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 this post


Link to post
Share on other sites
Talonius    643
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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by DrEvil
Damn, 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 this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by Fruny
Quote:
Original post by DrEvil
Damn, 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 this post


Link to post
Share on other sites
Photonman    174
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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by Zahlman
Interesting. 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 this post


Link to post
Share on other sites
JohnBSmall    881
Quote:
Original post by Fruny
cout << "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 this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by JohnBSmall
Wouldn't that need brackets around the conditional? I thought the ?: operator had very low precedence...


And you thought correctly. Rating++;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this