Open In App
Related Articles

C++17 new feature : If Else and Switch Statements with initializers

Improve Article
Save Article
Like Article

In many cases, we need to check the value of something returned by a function and perform conditional operations based on this value. So our code looks something like this

// Some function
return_type foo(params)
// Call function with params and 
// store return in var
auto var = foo(params);
if (var == /* some value */) { 
   //Do Something
} else {
   //Do Something else

While this is alright, notice that there are a few subtleties

  • The variable leaks into the surrounding scope, which is not intended and care must be taken to ensure that this variable doesn’t get mixed up with other variables
  • Since this variable has already been defined, if we were to call another function (say bar()) later in the program we would need to create another variable to store the values returned from bar()
  • Thirdly, if the compiler can know explicitly that the variable is going to be used only in one if-else block then it may be able to better optimize the code.

Notice the general format in all conditional if-else blocks. Firstly there is an optional initial statement which sets up the variable, followed by the if-else block. So the general if-else block resembles as follows


if (condition) {
    // Do Something
} else {
   // Do Something else 

In C++17 the init statement is called an initializer, and we can directly put it into the if-else block as follows

if (init-statement; condition) {
    // Do Something
} else {
    // Do Something else

The scope of the conditioned variable gets limited to the current if-else block. This also allows us to reuse the same named identifier in another conditional block.

if (auto var = foo(); condition) {
// Another if-else block
if (auto var = bar(); condition) {
else {

Likewise the switch case block has been updated. We can now put an initial expression inside the switch parenthesis. After the initial statement we need to specify which variable is being used to check the cases

switch (initial-statement; variable) {
    // cases

A complete program

// Program to demonstrate init if-else
// feature introduced in C++17
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
  // Set up rand function to be used
  // later in program
  // Before C++17
  int i = 2;
  if ( i % 2 == 0)
    cout << i << " is even number" << endl;
  // After C++17
  // if(init-statement; condition)
  if (int i = 4; i % 2 == 0  )
    cout << i << " is even number" << endl;
  // Switch statement
  // switch(init;variable)
  switch (int i = rand() % 100; i) {
         cout << "i = " << i << endl; break;

Output (Machine Dependent)

2 is even number
4 is even number
i = 5

Note: To compile these programs we need latest versions of compilers. As of writing this article this feature has been completely implemented in clang 5 and gcc 7 onwards. To compile the programs we also need to specify the -std=c++17 flag

g++-7 program_file.cpp -std=c++17


clang++ program_file.cpp -std=c++17

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 : 27 Jan, 2018
Like Article
Save Article
Similar Reads