{"id":75,"date":"2026-05-20T05:07:37","date_gmt":"2026-05-20T05:07:37","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=75"},"modified":"2026-05-22T07:44:54","modified_gmt":"2026-05-22T07:44:54","slug":"if-else-if-else","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=if-else-if-else","title":{"rendered":"if, else if, else"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Conditional statements in C++ are used to make decisions in a program. The <code>if<\/code>, <code>else if<\/code>, and <code>else<\/code> statements allow the program to execute different blocks of code based on conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are if, else if, else Statements?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These statements are used to control the flow of a program.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>if<\/code> checks a condition<\/li>\n\n\n\n<li><code>else if<\/code> checks another condition if the previous one is false<\/li>\n\n\n\n<li><code>else<\/code> runs when all conditions are false<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">They are commonly used in decision-making and logical operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of if Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {<br>    \/\/ code to execute<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of if Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    int age = 18;<br><br>    if (age &gt;= 18) {<br>        cout &lt;&lt; \"You are eligible to vote.\";<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of if else Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {<br>    \/\/ code if condition is true<br>}<br>else {<br>    \/\/ code if condition is false<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of if else Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    int number = 5;<br><br>    if (number % 2 == 0) {<br>        cout &lt;&lt; \"Even Number\";<br>    }<br>    else {<br>        cout &lt;&lt; \"Odd Number\";<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of if else if else Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition1) {<br>    \/\/ code<br>}<br>else if (condition2) {<br>    \/\/ code<br>}<br>else {<br>    \/\/ code<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of if else if else<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    int marks = 75;<br><br>    if (marks &gt;= 80) {<br>        cout &lt;&lt; \"Grade A\";<br>    }<br>    else if (marks &gt;= 60) {<br>        cout &lt;&lt; \"Grade B\";<br>    }<br>    else {<br>        cout &lt;&lt; \"Grade C\";<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How if, else if, else Work<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The <code>if<\/code> condition is checked first<\/li>\n\n\n\n<li>If true, its block executes<\/li>\n\n\n\n<li>If false, the <code>else if<\/code> condition is checked<\/li>\n\n\n\n<li>If all conditions are false, the <code>else<\/code> block runs<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Nested if Statement<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can also place an <code>if<\/code> statement inside another <code>if<\/code> statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br>    int age = 20;<br>    bool hasID = true;<br><br>    if (age &gt;= 18) {<br>        if (hasID) {<br>            cout &lt;&lt; \"Access Granted\";<br>        }<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Why if, else if, else are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These conditional statements are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Help programs make decisions<\/li>\n\n\n\n<li>Control the program flow<\/li>\n\n\n\n<li>Handle different conditions efficiently<\/li>\n\n\n\n<li>Improve logical programming<\/li>\n\n\n\n<li>Are widely used in real-world applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a traffic signal:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If light is green \u2192 Go<\/li>\n\n\n\n<li>Else if light is yellow \u2192 Slow down<\/li>\n\n\n\n<li>Else \u2192 Stop<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is how conditional statements work in programming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>if<\/code>, <code>else if<\/code>, and <code>else<\/code> statements in C++ are essential for decision-making in programs. They allow developers to create logical and interactive applications by executing different code blocks based on conditions.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">C++ Fundamentals (Beginner Level) > Operators and Conditions > if, else if, else<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":14,"template":"","class_list":["post-75","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>if, else if, else - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ if, else if, and else statements for decision making with conditions, syntax, and practical examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"if, else if, else - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ if, else if, and else statements for decision making with conditions, syntax, and practical examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T07:44:54+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=if-else-if-else\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"if, else if, else - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T05:07:37+00:00\",\"dateModified\":\"2026-05-22T07:44:54+00:00\",\"description\":\"Learn C++ if, else if, and else statements for decision making with conditions, syntax, and practical examples.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Fundamentals (Beginner Level) > Operators and Conditions > if, else if, else\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"if, else if, else - Learn C++Language with GiGz.PK","description":"Learn C++ if, else if, and else statements for decision making with conditions, syntax, and practical examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"if, else if, else - Learn C++Language with GiGz.PK","og_description":"Learn C++ if, else if, and else statements for decision making with conditions, syntax, and practical examples.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T07:44:54+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=if-else-if-else","url":"https:\/\/gigz.pk\/","name":"if, else if, else - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T05:07:37+00:00","dateModified":"2026-05-22T07:44:54+00:00","description":"Learn C++ if, else if, and else statements for decision making with conditions, syntax, and practical examples.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"C++ Fundamentals (Beginner Level) > Operators and Conditions > if, else if, else"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}