{"id":4251,"date":"2021-03-23T13:49:21","date_gmt":"2021-03-23T11:49:21","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=4251"},"modified":"2021-05-19T09:51:04","modified_gmt":"2021-05-19T07:51:04","slug":"initializing-statement-for-if-switch-foreach","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/","title":{"rendered":"Initializing statement for if\/switch\/foreach"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There are several statements in C++ whose syntax was modified in recent versions of the standard. I refer here to the if and switch statements that were modified in C++17 to include initializing statements, and the range-based for loop that supports the same as of C++20. Their general form is shown in the following table:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>C++17<\/td><td><code>if(init;<sub><em>opt<\/em><\/sub> condition)<\/code><\/td><\/tr><tr><td>C++17<\/td><td><code>switch(init;<sub><em>opt<\/em><\/sub> condition)<\/code><\/td><\/tr><tr><td>C++20<\/td><td><code>for(init;<sub><em>opt<\/em><\/sub> declaration : initializer)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>init<\/code> part is optional and, if missing, then we have the regular (or traditional) form of the statement. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I am going to show a few examples here, but before I go further, I must make a small comment. There is no such thing as <em>foreach<\/em> in C++. At least, not as a keyword\/statement. There is the concept of a for each statement, but it&#8217;s called <em>range-based for loop<\/em>. I always disliked this very long name, and I prefer to call it, at least in simplified context, a &#8220;foreach&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">if statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start with if statements. The following listing shows snippets before C++17.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">auto val = get_value();\nif(val > 0)\n{    \n   std::cout &lt;&lt; \"processing \" &lt;&lt; val &lt;&lt; '\\n';\n}\nelse \n{\n   std::cout &lt;&lt; \"value \" &lt;&lt; val &lt;&lt; \" is illegal\\n\";\n}\n\nstd::set&lt;int> n{ 1,2,3,5,8 };\nauto pos = n.find(3);\nif (pos != std::end(n))\n{\n   \/* do something *\/\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And this is how these snippets can be simplified with init statements in C++17:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (auto val = get_value(); val > 0)\n{    \n   std::cout &lt;&lt; \"processing \" &lt;&lt; val &lt;&lt; '\\n';\n}\nelse \n{\n   std::cout &lt;&lt; \"value \" &lt;&lt; val &lt;&lt; \" is illegal\\n\";\n}\n\nstd::set&lt;int> n{ 1,2,3,5,8 };\nif (auto pos = n.find(3); pos != std::end(n))\n{    \n   \/* do something *\/ \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A statement of the form <code>if(init; condition) statement;<\/code> is equivalent to the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n   init;\n   if(condition)\n      statement;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, the variable, or variables (because you can define multiple variables of the same type, separated by comma) are declared within a scope that immediately encloses the if statement. This means they are available not only in the if statement but also in all ensuing else-if statements. These may have other init statements, but you cannot redeclare a variable declared in the same if. Here is another example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (auto a = get_value(); a > 0)\n   std::cout &lt;&lt; \"processing A \" &lt;&lt; a &lt;&lt; '\\n';\nelse if(auto b = get_another_value(); b > 0)\n   std::cout &lt;&lt; \"processing B \" &lt;&lt; b &lt;&lt; '\\n';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code is transformed by the compiler into the following form:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n   auto a = get_value();\n   if (a > 0)\n   {\n      std::cout &lt;&lt; \"processing A \" &lt;&lt; a &lt;&lt; '\\n';\n   }\n   else\n   {\n      auto b = get_another_value();\n      if(b > 0)\n      {\n         std::cout &lt;&lt; \"processing B \" &lt;&lt; b &lt;&lt; '\\n';\n      }\n   }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">switch statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The other statement that supports init statements in C++17 is the switch statement. It&#8217;s syntax is very similar to what we have seen before.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before C++17, a switch statement would typically have the following form (this being a simple example):<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">auto option = get_option();\nswitch (option)\n{\ncase 'a': \/* add *\/   break;\ncase 'd': \/* del *\/   break;\ncase 'l': \/* list *\/  break;\ncase 'q': \/* quit *\/  break;\ndefault:  \/* error *\/ break;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In C++17, this statement can be refactored to the following form:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">switch (auto option = get_option(); option)\n{\n   case 'a': \/* add *\/   break;\n   case 'd': \/* del *\/   break;\n   case 'l': \/* list *\/  break;\n   case 'q': \/* quit *\/  break;\n   default:  \/* error *\/ break;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Like in the case of the if statement, the compiler introduces an enclosing scope, transforming the above snippet to the following form:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n   auto option = get_option();\n   switch (option)\n   {\n      case 'a': \/* add *\/   break;\n      case 'd': \/* del *\/   break;\n      case 'l': \/* list *\/  break;\n      case 'q': \/* quit *\/  break;\n      default:  \/* error *\/ break;\n   }\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">range-based for loop statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The latest statement to support initialization statements is the range-based for loops. This change was introduced in C++20 and is very similar to what we have seen so far.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following is a range-based for loop prior to C++20:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> get_values() { return { 1,2,3,5,8 }; }\n\nauto const&amp; values = get_values();\nfor (auto const&amp; v : values)\n   std::cout &lt;&lt; v &lt;&lt; '\\n';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In C++20, we can rewrite this to the following form:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (auto const&amp; values = get_values(); auto const&amp; v : values)\n   std::cout &lt;&lt; v &lt;&lt; '\\n';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Again, the compiler transforms it by adding an enclosing scope:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n   auto const&amp; values = get_values();\n   for (auto const&amp; v : values)\n      std::cout &lt;&lt; v &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Actually, this is just a simplified form, since the compiler is transforming the range-based for loop at the same time. The more correct form is shown below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    auto const&amp; values = get_values();  \/\/ with std::vector\n    auto &amp;&amp;__range = values;\n    auto __begin = values.begin();\n    auto __end = values.end();\n    for ( ; __begin != __end; ++__begin ) {\n        auto const&amp; v = *__begin;\n        std::cout &lt;&lt; v &lt;&lt; '\\n';\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This form with init statement can be handy when you iterate over a range but you also need the index of the elements of the range. Here is an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">for (size_t index = 1; auto const&amp; v : get_values())\n   std::cout &lt;&lt; \"value \" &lt;&lt; index++ &lt;&lt; \": \" &lt;&lt; v &lt;&lt; '\\n';<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Initialization statements for range-based for loops are available in GCC 9, Clang 8, and visual Studio 2019 16.5.<\/p>\n\n\n\n<p class=\"has-light-green-cyan-background-color has-background wp-block-paragraph\">If you want to see how the compiler is transforming your code you can try your snippets at <a href=\"https:\/\/cppinsights.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/cppinsights.io\/<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several statements in C++ whose syntax was modified in recent versions of the standard. I refer here to the if and switch statements that were modified in C++17 to include initializing statements, and the range-based for loop that supports the same as of C++20. Their general form is shown in the following table:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,475,543],"class_list":["post-4251","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-c17","tag-c20"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,c++17,c++20\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Initializing statement for if\/switch\/foreach\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2021-03-23T11:49:21+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2021-05-19T07:51:04+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Initializing statement for if\/switch\/foreach\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#article\",\"name\":\"Initializing statement for if\\\/switch\\\/foreach\",\"headline\":\"Initializing statement for if\\\/switch\\\/foreach\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2021-03-23T13:49:21+02:00\",\"dateModified\":\"2021-05-19T09:51:04+02:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#webpage\"},\"articleSection\":\"C++, C++, C++17, c++20\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#listItem\",\"name\":\"Initializing statement for if\\\/switch\\\/foreach\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#listItem\",\"position\":5,\"name\":\"Initializing statement for if\\\/switch\\\/foreach\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/\",\"name\":\"Initializing statement for if\\\/switch\\\/foreach\",\"description\":\"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/23\\\/initializing-statement-for-if-switch-foreach\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2021-03-23T13:49:21+02:00\",\"dateModified\":\"2021-05-19T09:51:04+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Initializing statement for if\/switch\/foreach","description":"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/","robots":"max-image-preview:large","keywords":"c++,c++17,c++20","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#article","name":"Initializing statement for if\/switch\/foreach","headline":"Initializing statement for if\/switch\/foreach","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2021-03-23T13:49:21+02:00","dateModified":"2021-05-19T09:51:04+02:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#webpage"},"articleSection":"C++, C++, C++17, c++20"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#listItem","name":"Initializing statement for if\/switch\/foreach"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#listItem","position":5,"name":"Initializing statement for if\/switch\/foreach","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/","name":"Initializing statement for if\/switch\/foreach","description":"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2021-03-23T13:49:21+02:00","dateModified":"2021-05-19T09:51:04+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Initializing statement for if\/switch\/foreach","og:description":"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.","og:url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/23\/initializing-statement-for-if-switch-foreach\/","article:published_time":"2021-03-23T11:49:21+00:00","article:modified_time":"2021-05-19T07:51:04+00:00","twitter:card":"summary","twitter:title":"Initializing statement for if\/switch\/foreach","twitter:description":"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops."},"aioseo_meta_data":{"post_id":"4251","title":null,"description":"Learn about the C++17 initialization statements for if and switch as well as the new C++20 initialization statement for range-based for loops.","keywords":[],"keyphrases":{"focus":{"keyphrase":"how to use C++ statements with inits","score":0,"analysis":[]},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"aioseo-article-636966901c060","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":{"businessInfo":{"name":"","urls":{"website":"","aboutPage":"","contactPage":""},"address":{"line1":"","line2":"","zip":"","city":"","state":"","country":""},"contact":{"email":"","phone":"","fax":""},"ids":{"vatID":"","taxID":"","chamberID":""},"payment":{"priceIndication":"","currenciesAccepted":"","methodsAccepted":""},"areaServed":""},"openingHours":{"show":false,"closedLabel":"","open24h":false,"open24hLabel":"","open247":false,"use24hFormat":false,"twoSets":false,"timezone":"","hours":[]}},"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-22 16:15:13","updated":"2025-12-12 07:56:25","seo_analyzer_scan_date":null,"focus_keyword":"how to use C++ statements with inits","additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"There are several statements in C++ whose syntax was modified in recent versions of the standard. I refer here to the if and switch statements that were modified in C++17 to include initializing statements, and the range-based for loop that supports the same as of C++20. Their general form is shown in the following table:","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++17<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">c++20<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">C++17<\/span>","<span class=\"advgb-post-tax-term\">c++20<\/span>"]}},"comment_count":"0","relative_dates":{"created":"Posted 6 years ago","modified":"Updated 5 years ago"},"absolute_dates":{"created":"Posted on March 23, 2021","modified":"Updated on May 19, 2021"},"absolute_dates_time":{"created":"Posted on March 23, 2021 1:49 pm","modified":"Updated on May 19, 2021 9:51 am"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-16z","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=4251"}],"version-history":[{"count":17,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4251\/revisions"}],"predecessor-version":[{"id":4310,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4251\/revisions\/4310"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=4251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=4251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=4251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}