By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Conditional expression

P: n/a
mdh
I wonder if someone can help me understand conditional expressions.
Please excuse if this is really silly.

I understand that the "while" ( expression) is true if non-zero. I
wrote this little function, which, however does not compile.
int i=235;

while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);

return 0;

Feb 18 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
mdh wrote:
int i=235;

while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);

return 0;
first try this:

#include <stdio.h>
int main()
{
int i -= 235;
while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);
return 0;
}

You will learn about functions in c later. For now you need to know that
the main function is the entry point to your program. The stdio library
includes the printf function for your program to use.

--
Zack
Feb 18 '07 #2

P: n/a
Zack wrote:
first try this:
mdh, so you're aware the program in my last post will not run either.
What it will give you though is line my line errors that might make
sense depending on your compiler. I just now looked at the actual body
of your code.

--
Zack
Feb 18 '07 #3

P: n/a
"mdh" <md**@comcast.netwrote:
# I wonder if someone can help me understand conditional expressions.
# Please excuse if this is really silly.
#
# I understand that the "while" ( expression) is true if non-zero. I
# wrote this little function, which, however does not compile.
#
#
# int i=235;
#
# while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);

Briefly what you have is
while-statement
predicate
--i
statement
? expr : expr ;

? expr : expr is not a valid expression, therefore that is not
a valid statement.

A valid statement would have a predicate between ) ... ?
Or it would move ?...:... inside the while predicate such as
while (--i) i>0 ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);
or
while (--i ? printf( " i = %d\n", i) : printf ( " i = %d\n", i) ) /*skip*/;

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The little stoner's got a point.
Feb 18 '07 #4

P: n/a
mdh
On Feb 17, 8:20 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.orgwrote:
"mdh" <m...@comcast.netwrote:

# I wonder if someone can help me understand conditional expressions.
# Please excuse if this is really silly.
#
# I understand that the "while" ( expression) is true if non-zero. I
# wrote this little function, which, however does not compile.
#
#
# int i=235;
#
# while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);

Briefly what you have is
while-statement
predicate
--i
statement
? expr : expr ;

? expr : expr is not a valid expression, therefore that is not
a valid statement.

A valid statement would have a predicate between ) ... ?
Or it would move ?...:... inside the while predicate such as
while (--i) i>0 ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);
or
while (--i ? printf( " i = %d\n", i) : printf ( " i = %d\n", i) ) /*skip*/;

--
SM Ryanhttp://www.rawbw.com/~wyrmwif/
The little stoner's got a point.


Aha,,,thanks.

Feb 18 '07 #5

P: n/a
2007-02-18 <11*********************@t69g2000cwt.googlegroups. com>,
mdh wrote:
I wonder if someone can help me understand conditional expressions.
Please excuse if this is really silly.

I understand that the "while" ( expression) is true if non-zero. I
wrote this little function, which, however does not compile.
All expressions are true _for the purpose of being passed to while, if,
?, etc_ if nonzero. while(...) is not, itself, an expression. 'while'
makes a loop.
>

int i=235;

while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);

return 0;
Feb 18 '07 #6

P: n/a
mdh
On Feb 17, 9:06 pm, Random832 <ran...@random.yi.orgwrote:
All expressions are true _for the purpose of being passed to while, if,
?, etc_ if nonzero. while(...) is not, itself, an expression. 'while'
makes a loop.

yes...I see that ...thanks.
Feb 18 '07 #7

P: n/a
Zack said:
mdh wrote:
>int i=235;

while (--i) ? printf( " i = %d\n", i) : printf ( " i = %d\n", i);

return 0;
first try this:

#include <stdio.h>
int main()
The advice you gave, whilst incomplete, was good advice, but I recommend
two changes:

1) when telling people about main, tell them int main(void) rather than
int main()
2) don't put tabs in source code that you post to Usenet, as many
newsreaders will strip them out, destroying your careful indentation in
the eyes of the world. Use spaces instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 18 '07 #8

P: n/a
mdh
On Feb 17, 11:44 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>
The advice you gave, whilst incomplete, .........
The code I was looking for, I realize was this. ( I was trying to
demonstrate ( to me, a novice, at least), how an expression that
returns "true" was really just non-zero)
while (p=i--)

printf( " i should be non-zero: i = %d\n", p);

printf( " i should be zero with this quitting the \"while\" loop: i =
%d\n", p);

return 0;

Feb 18 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.