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:
- Comma as an operator
- Comma as a separator
- Comma operator in place of a semicolon
Contact Us