5 Replies - 742 Views - Last Post: 05 March 2010 - 08:57 AM Rate Topic: -----

#1 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

, vs ; -- comma operator

Posted 04 March 2010 - 06:35 PM

It appears that a comma can seperate some statements. I thought a semi-colon always seperated statements. This code:

int main()
{
    int a = 6;
    double b = 7.7;
    cout << a << endl, b = 8.8, cout << b << endl,
    cin >> a;
}



Is no different from this code:

int main()
{
    int a = 6;
    double b = 7.7;
    cout << a << endl; b = 8.8; cout << b << endl;
    cin >> a;
}



Why does the first code with the commas work?

Is This A Good Question/Topic? 0
  • +

Replies To: , vs ; -- comma operator

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: , vs ; -- comma operator

Posted 04 March 2010 - 06:44 PM

An interesting read I found about C++ commas. [First question]


As for why that works, see this and this
Was This Post Helpful? 4
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: , vs ; -- comma operator

Posted 04 March 2010 - 09:02 PM

Nice links! I am glad to see that you gave the link to the sequence point as well since that is a key to understanding the comma operator and its uses!

So lets break this down:
cout << a << endl, b = 8.8, cout << b << endl,
cout << a;


Lets start by ignoring the line break at the end of the first line there. In C/C++ generally white space has no meaning and the new line character is a white space char. So we can rewrite this as one expression:

cout << a << endl, b = 8.8, cout << b << endl, cout << a;

next we can break this into 4 sub expressions seperated by the comma operator:

exp1, exp2, exp3, exp4;

where

exp1 = cout << a << endl
exp2 = b = 8.8
exp3 = cout << b << endl
exp4 = cout << a

So, first exp1 executes and outputs a and a newline, then exp2 executes setting b to 8.8, then exp3 executes outputting b and a new line, and then exp3 evaluates outputting a again.
Was This Post Helpful? 1
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: , vs ; -- comma operator

Posted 05 March 2010 - 12:44 AM

I find the , operator useful in for() statements such as
   for (i=0, j=10; i<20; i++, j+=2)
    .......


Was This Post Helpful? 0
  • +
  • -

#5 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: , vs ; -- comma operator

Posted 05 March 2010 - 08:30 AM

I have used the comma operator many times in for loops and variable declarations but it never occurred to me that you could use it to separate statements. I think I'll stick with my good friend the semi-colon for statement delimiting. Thanks for the information.
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: , vs ; -- comma operator

Posted 05 March 2010 - 08:57 AM

I have also used it in while() statements
while (cout << "\nnumber > 0 ? ", cin >> n, !cin.eof())     
   {
    ...
 
 

it prompts user, reads a number and tests for EOF (if EOF the loop terminates)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1