Current location: Hot Scripts Forums » Programming Languages » C/C++ » C++ Tip : What is Comma-Separated Expressions?


C++ Tip : What is Comma-Separated Expressions?

Reply
  #1 (permalink)  
Old 06-22-10, 01:09 PM
Learnbyexamples Learnbyexamples is offline
Disabled
 
Join Date: Jun 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
C++ Tip : What is Comma-Separated Expressions?

Comma-separated expressions were inherited from C. It’s likely that you use such expressions in for- and while-loops rather often. Yet, the language rules in this regard are far from being intuitive. First, let’s see what a comma separated expression is.
An expression may consist of one or more sub-expressions separated by commas. For example:
Code:
if(++x, --y, cin.good()) /*three expressions*/
The if condition contains three expressions separated by commas. C++ ensures that each of the expressions is evaluated and its side effects take place. However, the value of an entire comma-separated expression is only the result of the rightmost expression. Therefore, the if condition above evaluates as true only if cin.good() returns true. Here’s another example of a comma expression:

Code:
int j=10;  
int i=0;  
while( ++i, --j)  
{  
 /*..repeat as long as j is not 0*/  
}
Reply With Quote
  #2 (permalink)  
Old 11-09-11, 03:14 AM
Ken Peterson Ken Peterson is offline
New Member
 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
The C++ standard guarantees that each of the expressions shall be evaluated and its side effects shall take place. However, the value of an entire comma-separated expression is only the result of the rightmost expression. Thus, the if condition evaluates as true only if cin.good() returns true. Comma separated expressions are used in loops.
Reply With Quote
Reply

Bookmarks


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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Create dropdown menu from comma separated list of numbers maya77 PHP 22 03-20-11 11:35 AM
[SOLVED] Have one tab separated file with one product/line and want to update an sql Oskare100 PHP 5 03-13-08 01:09 PM
comma separated email list problems.... Alith7 PHP 16 10-16-06 11:24 AM
Mastering Regular Expressions in PHP rorycanyon PHP 0 10-24-05 07:27 AM


All times are GMT -5. The time now is 01:15 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.