C++ shorthand "if then" issue

Started by
20 comments, last by Fruny 18 years, 1 month ago
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;
}
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Advertisement
What is 'c' ?
Quote:Original post by DrEvil
What is 'c' ?

Check above, i added the whole file :) thank you for your input.
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
The only thing i can suggest is putting then c==0 inside brackets, ie (c==0).

Dave
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 ;)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
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.
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.
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.
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();
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement