Open In App
Related Articles

How can we use Comma operator in place of curly braces?

Improve Article
Improve
Save Article
Save
Like Article
Like

In C and C++, comma (, ) can be used in two contexts:

  1. Comma as an operator
  2. Comma as a separator

But in this article, we will discuss how a comma can be used as curly braces.

The curly braces are used to define the body of function and scope of control statements. The opening curly brace ({) indicates starting scope and closing curly brace (}) indicates the end of the scope. It is also possible to use a comma operator in control statements to define scope instead of using curly braces.

Comma operator used in place of curly braces in if-else statements:

Example:




// C++ program to show how to use
// comma in place of curly braces
// for if-else statements
  
#include <iostream>
using namespace std;
  
void func(int num)
{
    if (num < 10)
        cout << "It shows how we can use "
             << "comma operator in place of curly braces.\n",
            cout << "Entered number is less than 10\n",
            cout << "end of if block is encountered\n\n";
    else
        cout << "Now we are in else part\n",
            cout << "Entered number is greater than 10\n",
            cout << "End of else is encountered\n\n";
}
  
int main()
{
    int num = 5;
    func(num);
  
    num = 20;
    func(num);
  
    return 0;
}


Output:

It shows how we can use comma operator in place of curly braces.
Entered number is less than 10
end of if block is encountered

Now we are in else part
Entered number is greater than 10
End of else is encountered

Comma operator used in place of curly braces in loops:

Example:




// C++ program to show how to use
// comma in place of curly braces
// for loops
  
#include <iostream>
using namespace std;
  
void func(int num)
{
    for (int i = 0; i < num; i++)
        cout << "It shows how we can use ",
            cout << "comma operator in place of curly braces.\n",
            cout << "Loop traversal number: ",
            cout << i << "\n\n ";
}
  
int main()
{
    int num = 5;
    func(num);
  
    return 0;
}


Output:

It shows how we can use comma operator in place of curly braces.
Loop traversal number: 0

 It shows how we can use comma operator in place of curly braces.
Loop traversal number: 1

 It shows how we can use comma operator in place of curly braces.
Loop traversal number: 2

 It shows how we can use comma operator in place of curly braces.
Loop traversal number: 3

 It shows how we can use comma operator in place of curly braces.
Loop traversal number: 4

Note: The last statement of the block must be terminated by a semicolon.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2019
Like Article
Save Article
Previous
Next
Similar Reads