Init Statement for if and switch

C++17 adds new features to 'if' and 'switch' statements. See more details in this lesson.

C++17 provides new versions of the if and switch statements:

if (init; condition)

And

switch (init; condition)

In the init section you can specify a new variable, similarly to the init section in for loop. Then check the variable in the condition section. The variable is visible only in if/else scope.

To achieve a similar result, before C++17 you had to write:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy