{"id":1220,"date":"2026-09-03T09:00:00","date_gmt":"2026-09-03T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/"},"modified":"2026-09-03T09:00:00","modified_gmt":"2026-09-03T03:30:00","slug":"apex-conditional-statements-if-switch","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/","title":{"rendered":"Apex Conditional Statements: if, else and switch Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Conditional Statements let your code make decisions. For example, they answer &#8220;Is the fee too low?&#8221; or &#8220;Which level is this course?&#8221;. Then they run different code for each answer. Without them, your code would do the same thing every time, no matter what the data says.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the last two posts, you learned <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-data-types-variables-constants\/\">Apex data types<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-operators-expressions\/\">Apex operators<\/a>. Operators give you true or false results. Now you will use those results to choose what happens next. If you have built decision elements in our <a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-flow-builder-automation-guide\/\">Flow Builder guide<\/a>, the idea will feel familiar.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Apex Conditional Statements?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A conditional statement runs a block of code only when a condition is true. The condition is any expression that gives a Boolean, such as <code>fee &gt; 5000<\/code>. Apex has two main tools for this: the <code>if<\/code> family and the <code>switch<\/code> statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a traffic signal. If the light is green, you drive. Else if it is yellow, you slow down. Else, you stop. Your code works the same way. It checks a condition and picks one path.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Conditional Statements Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Business rules are full of &#8220;if this, then that&#8221;. So almost every class or trigger you write will use Apex Conditional Statements. Here are some real rules from Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stop the save with an error when the fee is below 500.<\/li>\n\n<li>Give a 10% discount when a course is online.<\/li>\n\n<li>Mark fees above 8000 as Advanced, above 4000 as Intermediate, and the rest as Beginner.<\/li>\n\n<li>Assign a senior trainer to Advanced courses.<\/li>\n\n<li>Close sign-ups once the start date has passed.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Each of these is one small decision. Put together, they form the logic of an app.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Conditional Statements Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With Apex Conditional Statements, Apex checks the condition first. If it is true, Apex runs the code inside the curly braces. If it is false, Apex skips that block and moves on, or runs the <code>else<\/code> block if there is one.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1-1024x640.png\" alt=\"Apex Conditional Statements diagram showing how an else if chain checks each condition\" class=\"wp-image-1218\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">An else if chain checks from top to bottom and stops at the first true condition.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With an <code>else if<\/code> chain, Apex checks each condition from top to bottom. As soon as one is true, it runs that block and skips the rest. So the order of your checks matters a lot.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Apex Conditional Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex gives you a few shapes of conditional statements. Each one fits a different kind of decision.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The if Statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest form runs code only when one condition is true. Nothing happens when it is false.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Decimal fee = 300;\n\nif (fee &lt; 500) {\n    System.debug('Fee is too low');\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here the condition <code>fee &lt; 500<\/code> is true, so the message prints. If the fee were 600, Apex would skip the block silently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The if-else Statement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>else<\/code> when you want something to happen in both cases. One of the two blocks always runs, never both.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The else if Chain<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>else if<\/code> when you have more than two choices. Apex walks down the chain and stops at the first true condition. A final <code>else<\/code> catches everything left over.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Nested if Statements<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can put one <code>if<\/code> inside another. This is useful when a second check only makes sense after the first passes. However, deep nesting gets hard to read, so keep it to two levels when you can.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The switch Statement in Apex Conditional Statements<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>switch on<\/code> compares one value against a list of options. Each option starts with <code>when<\/code>. The <code>when else<\/code> block runs if nothing matches. It reads more cleanly than a long <code>else if<\/code> chain when you are checking one value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Example with if, else if and else<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s set a course level from its fee at Bright Path Tutors. Fees above 8000 mean Advanced, and fees above 4000 mean Intermediate. All the rest mean Beginner. We also block fees below 500.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public with sharing class CourseLevelSetter {\n\n    public static String levelFor(Decimal fee) {\n        if (fee == null || fee &lt; 500) {\n            return 'Invalid';\n        } else if (fee &gt; 8000) {\n            return 'Advanced';\n        } else if (fee &gt; 4000) {\n            return 'Intermediate';\n        } else {\n            return 'Beginner';\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first check handles bad input, including a null fee. Next, the chain checks the highest band first. This order is important, because 9000 is also greater than 4000. If we checked 4000 first, a 9000 course would wrongly become Intermediate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now test it with anonymous Apex.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.debug(CourseLevelSetter.levelFor(9500));\nSystem.debug(CourseLevelSetter.levelFor(5200));\nSystem.debug(CourseLevelSetter.levelFor(1500));\nSystem.debug(CourseLevelSetter.levelFor(200));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The log shows one result for each call.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [1]|DEBUG|Advanced\nUSER_DEBUG [2]|DEBUG|Intermediate\nUSER_DEBUG [3]|DEBUG|Beginner\nUSER_DEBUG [4]|DEBUG|Invalid<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Example with switch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now we go the other way. Given a level, we want the trainer team name. This time we check one value against several options, so <code>switch<\/code> is a good fit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String level = 'Intermediate';\nString team;\n\nswitch on level {\n    when 'Beginner' {\n        team = 'Foundation Team';\n    }\n    when 'Intermediate', 'Advanced' {\n        team = 'Senior Team';\n    }\n    when null {\n        team = 'Unassigned';\n    }\n    when else {\n        team = 'Review Needed';\n    }\n}\nSystem.debug('Team: ' + team);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second <code>when<\/code> lists two values separated by a comma, so both share one block. Next, <code>when null<\/code> handles an empty level safely. Finally, <code>when else<\/code> catches any unexpected value, such as a typo. The log prints <code>Team: Senior Team<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What switch Can Check<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">However, <code>switch on<\/code> does not work with every type. The table below lists what it supports.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Type<\/th><th>Example when value<\/th><th>Good for<\/th><\/tr><\/thead><tbody><tr><td>Integer and Long<\/td><td>when 1, 2, 3<\/td><td>Numbered options<\/td><\/tr><tr><td>String<\/td><td>when &#8216;Beginner&#8217;<\/td><td>Picklist values<\/td><\/tr><tr><td>Enum<\/td><td>when RED<\/td><td>Fixed sets you define<\/td><\/tr><tr><td>sObject<\/td><td>when Course__c c<\/td><td>Checking a record&#8217;s object type<\/td><\/tr><tr><td>null<\/td><td>when null<\/td><td>Empty values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So you cannot switch on a Decimal or a Date. For ranges like &#8220;fee above 4000&#8221;, use <code>if<\/code> and <code>else if<\/code> instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">if-else vs switch in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both are Apex Conditional Statements, but they suit different jobs. Picking the right one makes your code shorter and clearer. The table below compares them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>if-else chain<\/th><th>switch on<\/th><\/tr><\/thead><tbody><tr><td>Checks<\/td><td>Any Boolean condition<\/td><td>One value against fixed options<\/td><\/tr><tr><td>Ranges, such as fee &gt; 4000<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Many exact values<\/td><td>Gets long<\/td><td>Clean and short<\/td><\/tr><tr><td>Supported types<\/td><td>Any expression<\/td><td>Integer, Long, String, Enum, sObject<\/td><\/tr><tr><td>Default case<\/td><td>Final else<\/td><td>when else<\/td><\/tr><tr><td>Readability for 5+ options<\/td><td>Lower<\/td><td>Higher<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is simple. Use <code>if-else<\/code> for ranges and mixed conditions, and use <code>switch<\/code> when you compare one value against a list.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2-1024x640.png\" alt=\"Apex Conditional Statements diagram comparing if-else and switch\" class=\"wp-image-1219\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">if-else vs switch in Apex.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Conditional Statements in Triggers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Triggers are where you will use conditions most often. A trigger runs for every record a user saves, so it must decide what to do with each one. Here is a common pattern from Bright Path Tutors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>trigger CourseCheck on Course__c (before insert, before update) {\n    for (Course__c c : Trigger.new) {\n        if (c.Fee__c == null) {\n            c.Fee__c.addError('Please enter a fee.');\n        } else if (c.Fee__c &lt; 500) {\n            c.Fee__c.addError('Fee must be at least 500.');\n        } else if (c.Is_Online__c &amp;&amp; c.Duration_in_Weeks__c == null) {\n            c.Duration_in_Weeks__c = 6;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, the trigger loops over every course in the save. For each one, it checks three conditions in order. A missing fee or a low fee stops the save with a clear error on the field. Otherwise, online courses with no duration get a default of 6 weeks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So a few lines of Apex Conditional Statements enforce three business rules at once. We will learn loops in the next post and triggers in detail later in the series.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ternary vs if-else<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You met the ternary operator in the previous post. It is a one-line shortcut for a simple <code>if-else<\/code> that picks a value. For example, <code>String mode = isOnline ? 'Online' : 'Classroom';<\/code> does the same job as five lines of <code>if-else<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the ternary only picks between two values. It cannot run several statements. So use it for short value choices, and use full Apex Conditional Statements for anything with more steps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Test Every Branch<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every <code>if<\/code>, <code>else if<\/code> and <code>when<\/code> is a separate path through your code. Good tests check each path at least once. So the level example needs four tests. You test an invalid fee, an Advanced fee, an Intermediate fee and a Beginner fee.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Testing every branch also raises your code coverage. As a result, your deployments pass the 75% rule with room to spare.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tips for Clean Apex Conditional Statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Good conditional code is easy to scan. A few habits help a lot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, return early. If an input is invalid, return or throw an error at the top of the method. Then the rest of the code does not need extra nesting. Next, always use curly braces, even for one line. Apex allows a single line without braces, but adding a second line later causes bugs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, give complex conditions a name. Instead of a long expression inside the <code>if<\/code>, store it in a Boolean first, such as <code>Boolean canEnrol = seatsLeft &gt; 0 &amp;&amp; !isClosed;<\/code>. Then write <code>if (canEnrol)<\/code>. The code now reads almost like plain English.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Conditional Statements<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Checking ranges in the wrong order. Always test the highest or most specific case first.<\/li>\n\n<li>Using <code>=<\/code> instead of <code>==<\/code> in a condition. Apex will not compile <code>if (fee = 500)<\/code>, so read the error carefully.<\/li>\n\n<li>Forgetting null checks. A null fee in <code>fee &gt; 500<\/code> throws an error, so check for null first.<\/li>\n\n<li>Leaving out <code>when else<\/code>. An unexpected picklist value then does nothing, silently.<\/li>\n\n<li>Nesting four or five levels deep. Split the logic into small methods instead.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Conditional Statements<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Put the most likely or most important condition first.<\/li>\n\n<li>Use <code>switch<\/code> for picklist values, so new options are easy to add.<\/li>\n\n<li>Always include a final <code>else<\/code> or <code>when else<\/code> for unexpected values.<\/li>\n\n<li>Keep each block short. If it grows, move the work into a method.<\/li>\n\n<li>Store business thresholds, such as 4000 and 8000, in constants.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a method called <code>seatStatus<\/code> that takes seats booked and max seats. Return &#8220;Full&#8221; when booked equals max, &#8220;Almost full&#8221; when 3 or fewer seats remain, and &#8220;Open&#8221; otherwise. Hint: check &#8220;Full&#8221; first, then &#8220;Almost full&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/langCon_apex_if_else.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Conditional (If-Else) Statements<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/langCon_apex_switch.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Developer Guide: Switch Statements<\/a><\/li>\n\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/apex_database\" target=\"_blank\" rel=\"noopener noreferrer\">Trailhead: Apex Basics and Database<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Conditional Statements run code only when a condition is true.<\/li>\n\n<li>An <code>else if<\/code> chain stops at the first true condition, so order matters.<\/li>\n\n<li><code>switch on<\/code> compares one value against fixed options.<\/li>\n\n<li>Switch works with Integer, Long, String, Enum and sObject types.<\/li>\n\n<li>Always handle null and unexpected values with <code>else<\/code> or <code>when else<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Decisions are only half the story. Next, learn how to repeat work in <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-loops-for-while-do-while\/\">Apex loops with for, while and do-while<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Conditional Statements let your code make decisions. For example, they answer &#8220;Is the fee too low?&#8221; or &#8220;Which level is this course?&#8221;. Then&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1217,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,15,7,97],"swapz_series":[],"class_list":["post-1220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-beginner","tag-salesforce","tag-salesforce-developer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Conditional Statements: if, else and switch<\/title>\n<meta name=\"description\" content=\"Apex Conditional Statements explained simply: if, else if, nested checks and switch on, with real Salesforce examples and common mistakes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Conditional Statements: if, else and switch\" \/>\n<meta property=\"og:description\" content=\"Apex Conditional Statements explained simply: if, else if, nested checks and switch on, with real Salesforce examples and common mistakes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-03T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"Apex Conditional Statements: if, else and switch Explained\",\"datePublished\":\"2026-09-03T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/\"},\"wordCount\":1511,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-conditional-statements-if-switch-featured.png\",\"keywords\":[\"Apex\",\"Beginner\",\"Salesforce\",\"Salesforce Developer\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/\",\"name\":\"Apex Conditional Statements: if, else and switch\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-conditional-statements-if-switch-featured.png\",\"datePublished\":\"2026-09-03T03:30:00+00:00\",\"description\":\"Apex Conditional Statements explained simply: if, else if, nested checks and switch on, with real Salesforce examples and common mistakes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-conditional-statements-if-switch-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/apex-conditional-statements-if-switch-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Conditional Statements featured image: title card with an if else chain\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-conditional-statements-if-switch\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Conditional Statements: if, else and switch Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Conditional Statements: if, else and switch","description":"Apex Conditional Statements explained simply: if, else if, nested checks and switch on, with real Salesforce examples and common mistakes.","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:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/","og_locale":"en_US","og_type":"article","og_title":"Apex Conditional Statements: if, else and switch","og_description":"Apex Conditional Statements explained simply: if, else if, nested checks and switch on, with real Salesforce examples and common mistakes.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-03T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"Apex Conditional Statements: if, else and switch Explained","datePublished":"2026-09-03T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/"},"wordCount":1511,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-featured.png","keywords":["Apex","Beginner","Salesforce","Salesforce Developer"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/","name":"Apex Conditional Statements: if, else and switch","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-featured.png","datePublished":"2026-09-03T03:30:00+00:00","description":"Apex Conditional Statements explained simply: if, else if, nested checks and switch on, with real Salesforce examples and common mistakes.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/10\/apex-conditional-statements-if-switch-featured.png","width":2400,"height":1260,"caption":"Apex Conditional Statements featured image: title card with an if else chain"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-conditional-statements-if-switch\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Conditional Statements: if, else and switch Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1220"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1220\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1217"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1220"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}