
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++17 If statement with initializer
C++17 has extended existing if statement’s syntax. Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight. Which in turn avoids variable leaking outside the scope.
Example
Let us suppose we want to check whether given number is even or odd. Before C++17 our code used to look like this −
#include <iostream> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); int random_num = rand(); if (random_num % 2 == 0) { cout << random_num << " is an even number\n"; } else { cout << random_num << " is an odd number\n"; } return 0; }
Output
When you compile and execute the above code it will generate output something like this −
1555814729 is an odd number
In the above example, we can see that variable "random_num" is leaked outside the if-else scope. We can easily avoid this with new "if statement with initializer" syntax.
Below is the syntax of "if statement with initializer" −
if (init; condition) { // Do stuff when Boolean condition is true } else { // Do stuff when Boolean condition is false }
Example
Now let us write same code using this new if statement with initializer −
#include <iostream> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); // C++17 if statement with initializer if (int random_num = rand(); random_num % 2 == 0) { cout << random_num << " is an even number\n"; } else { cout << random_num << " is an odd number\n"; } return 0; }
In above example, scope of variable "random_num" is limited to if-else block. Hence this variable won't be accessible outside this block. Surprisingly it keeps variables scope tight without affecting actual output.
Output
When you compile and execute above code it will generate output something like this −
943513352 is an even number
NOTE − As we are generating random number each time output will vary for each run even on same machine.
- Related Articles
- Object Initializer in C#
- MySQL SELECT IF statement with OR?
- MySQL If statement with multiple conditions?
- Display records with conditions set using if statement in UPDATE statement with MySQL
- Object initializer in JavaScript
- instance initializer block in Java
- The Initializer Block in Java
- Explain ‘simple if’ statement in C language
- Explain if-else statement in C language
- Else and Switch Statements with initializers in C++17
- Explain Nested if-else statement in C language
- Explain else-if ladder statement in C language
- How to implement WHILE LOOP with IF STATEMENT MySQL?
- Java if statement
- IF ELSE statement in a MySQL Statement?
