In C, every statement of the program is terminated by semicolon (;). In C++ it is applicable and in addition C++ allows us to terminate a statement using comma operator after satisfying the following rules.
- The variable declaration statements should be terminated by semicolon.
- The statements followed by declaration statements like clrscr(), cin, and cout can be terminated by comma operator.
- C++ permits declaration of variables at any point in program, but such declaration is not allowed in between the statements terminated by comma operator. The initialization of previously declared variables can be done.
- The last statement of the program must be terminated by semicolon.
Consider the following programs.
4.31 Write a program to use comma operator in place of semi-colon.
Explanation: In function main(), the first two statements are terminated by comma operator and the last statement is terminated by semicolon.
All the above points are noticed in the Turbo C++ compiler. The reader is advised to follow his/her own observations.
4.32 Write a program to declare an integer, initialize it and display it. Terminate the statements using comma operator.
Explanation: In the above program, the first and last statements are terminated by semicolon. Comma operator terminates the statements in between these two statements.