473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Can we use break statement in conditional operator?

Hi

the scenario is like this:

func()
{
....
....

char *p= (char *)&str[0];
while (p)
*++p=='$' ? break:(*p != ' ' ? return error : )
.....
.....

}

can i use conditional operator like these

Oct 30 '07 #1
5 5859
jayapal wrote:
Hi

the scenario is like this:

func()
{
...
...

char *p= (char *)&str[0];
while (p)
*++p=='$' ? break:(*p != ' ' ? return error : )
No. the second operand in a conditional-expression is required to be an
expression. 'break;' and 'return error;' are statements, not an expressions.
Oct 30 '07 #2
Notice if str[] = "$" your func() will.. 'fail'.
Also, you want while(*p) and not while(p).
Also, if str is of type char * or char[], the cast is not needed.
&str[0] is a char *.

int func(const char * str) {

while(!isspace(*str))
if(*str == '$') break;

return *str == '$';
}

Oct 30 '07 #3
On Oct 30, 2:11 pm, vipvipvipvipvip...@gmail.com wrote:
while(!isspace(*str))
sorry, while(*str && !isspace(*str))

Oct 30 '07 #4
On Oct 30, 2:12 pm, vipvipvipvipvip...@gmail.com wrote:
On Oct 30, 2:11 pm, vipvipvipvipvip...@gmail.com wrote:while(!isspace(*str))

sorry, while(*str && !isspace(*str))
Damn, i don't increment str in the loop.
Sorry for this; it's been a long day

Oct 30 '07 #5
jayapal wrote:
Hi

the scenario is like this:

func()
{
...
...

char *p= (char *)&str[0];
while (p)
*++p=='$' ? break:(*p != ' ' ? return error : )
....
....

}

can i use conditional operator like these
As at least one other person has said, no. And even if you /could/,
you couldn't write what you wrote, because there's nothing after
the second `:`. And even if you could do /that/, what exactly is
this loop supposed to accomplish? It /looks/ like (fx:emulate) it
skips the first character [if any] and then skips spaces and
either returns the error value if it's not at $-or-eos, or
falls through pointing at $-or-eos.

If so, I would have thought:

char *p = str; /* suspect the casting and indexing useless */

if (*p) p += 1;
while (*p == ' ') p += 1;

if (*p == '$') { whatever } else return error;

was more transparent. (I have a stylistic preference away from using
++ when the result isn't consumed; feel free to replace `p += 1` by
`p++` in the above if you have the reverse preference.)

--
Chris "would generally /prefer/ that `break` etc were expressions" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Oct 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
26
by: Chris Potter | last post by:
Hello everyone. I am taking my first course in C and in one of my assignments i need to print out an array that could have anywhere from 0 to 100 positive integers in it (a negative integer is...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
1
by: Jim Heavey | last post by:
I have a couple of questions about break points. I have this one ASPX page which had a breakpoint set on a particular line and I am unable to get rid of it. If I look at the line in which the...
2
by: ichor | last post by:
hi i want to add a conditional break in my vb.net code. like we could in vb6. for example if the counter = 10 then break at that point. kindly let me know if thats possible in vb.net Ichor
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
15
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a...
16
by: lovecreatesbea... | last post by:
Are the following two lines equal? Suppose the expression i++ doesn't overflow. They behave differently in my code. if (!p ? i++ : 0) break; if (!p){ i++; break;} Thank you for your time.
1
by: =?Utf-8?B?YQ==?= | last post by:
Does anyone know if there is a way to force the studio debugging to break at any line of code when any string is initialized with a certain value? In this case I inherited a badly written app and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.