Comma Operator in Place of a Semicolon

The comma operator in C++ can be also used for terminating statements of the C++ Program. To use a comma operator in place of a semicolon, the following conditions must be satisfied.

  • The variable declaration statements must be terminated with a semicolon.
  • The statements after the declaration statement can be terminated by the comma operator.
  • The last statement of the program must be terminated by a semicolon.

C++




// C++ program to demonstrate
// the use of comma as a
// semicolon
#include <iostream>
using namespace std;
 
int main()
{
 
    // variable declaration
    int num1 = 54;
    int num2 = 34;
    int num3 = 45;
 
    cout << num1 << endl, cout << num2 << endl,
        cout << num3 << endl;
 
    return 0;
}


Output

54
34
45

In the above program all the variable declarations num1, num2 & num3 are ended with semicolons and the last statement of output is ended with a semicolon.



Comma in C++

Comma (,) in C++  can work in three different contexts:

  1. Comma as an operator
  2. Comma as a separator
  3. Comma operator in place of a semicolon

Similar Reads

1. Comma as an operator

A comma operator in C++ is a binary operator. It evaluates the first operand & discards the result, evaluates the second operand & returns the value as a result. It has the lowest precedence among all C++ Operators. It is left-associative & acts as a sequence point....

2. Comma as a separator

...

3. Comma Operator in Place of a Semicolon

...

Contact Us