Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 17th, 2005, 3:40 PM   #11
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
if (selection == 1, 2, 3, 4, 5)
This doesn't do what you think it does.
It is not comparing selection to 1,2,3,4, or 5. It is comparing selection to 1, then throwing away that result and returning 5 all the time. This means your if test never fails.
The Dark is offline   Reply With Quote
Old Dec 17th, 2005, 3:59 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The Dark is ever accurate, but often laconic. Quite over and above the fact that expression evaluation is often improperly coded (val == A | B | C ... instead of val == A || val == B ....), the comma operator is little understood. Many don't even know it exists.
Quote:
Originally Posted by MSDN
Comma Operator

The comma operator allows grouping two statements where one is expected.
Syntax

expression :
assignment-expression
expression , assignment-expression

The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.

Consider the expression

e1 , e2

The type and value of the expression are the type and value of e2; the result of evaluating e1 is discarded. The result is an l-value if the right operand is an l-value.

Where the comma has special meaning (for example in actual arguments to functions or aggregate initializers), the comma operator and its operands must be enclosed in parentheses. Therefore, the following function calls are not equivalent:

// Declare functions:
void Func( int, int );
void Func( int );

Func( arg1, arg2 ); // Call Func( int, int )
Func( (arg1, arg2) ); // Call Func( int )

This example illustrates the comma operator:

for ( i = j = 1; i + j < 20; i += i, j-- );

In this example, each operand of the for statements third expression is evaluated independently. The left operand i += i is evaluated first; then the right operand, j, is evaluated.

func_one( x, y + 2, z );
func_two( (x--, y + 2), z );

In the function call to func_one, three arguments, separated by commas, are passed: x, y + 2, and z. In the function call to func_two, parentheses force the compiler to interpret the first comma as the sequential-evaluation operator. This function call passes two arguments to func_two. The first argument is the result of the sequential-evaluation operation (x--, y + 2), which has the value and type of the expression y + 2; the second argument is z.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Dec 21st, 2005, 9:55 PM   #13
D-Ferg27
Programmer
 
D-Ferg27's Avatar
 
Join Date: Oct 2005
Posts: 49
Rep Power: 0 D-Ferg27 is on a distinguished road
yea i was wondering why the test didn't fail when i inserted bad input. But now i know that using the commas is a heck of a lot easier way to idiot proof code. So the usage of commas could be a extremely good thing or a terribly retarded thing.





:banana: ITS PEANUT BUTTER JELLY TIME!!!!!! :banana:
^^^^ Im sorry i just had to do it.
__________________
Dylan Ferguson hence the nickname D-Ferg

Got an IPod?
Use ManagePod
All the cool kids are doing it.
D-Ferg27 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC