{"id":3596,"date":"2022-05-20T11:11:27","date_gmt":"2022-05-20T11:11:27","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=3596"},"modified":"2022-05-20T11:11:27","modified_gmt":"2022-05-20T11:11:27","slug":"dart-switch-case-statement","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/05\/20\/dart-switch-case-statement\/","title":{"rendered":"Dart Switch Case Statement"},"content":{"rendered":"\n<p>Dart Switch case statement is used to avoid the long chain of the if-else statement. It is the simplified form of nested if-else statement. The value of the variable compares with the multiple cases, and if a match is found, then it executes a block of statement associated with that particular case.<\/p>\n\n\n\n<p>The assigned value is compared with each case until the match is found. Once the match found, it identifies the block of code to be executed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dart Switch Case Statement Flowchart<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/tutorial\/dart\/images\/dart-switch-case-statement.png\" alt=\"Dart Switch Case Statement\"\/><\/figure>\n\n\n\n<p>The syntax is given below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>switch( expression )  \n{  \n    case value-1:{  \n  \n            \/\/statement(s)  \n            Block-1;  \n                                         }  \n                                            break;  \n    case value-2:{             \n                                                          \/\/statement(s)  \n            Block-2;  \n                                          }  \n                                           break;  \n    case value-N:{             \n                                                          \/\/statement(s)  \n            Block-N;  \n                                          }  \n                                           break;  \n    default:    {  \n            \/\/statement(s);  \n                                      }  \n}  <\/code><\/pre>\n\n\n\n<p>Here, the expression can be integer expression or character expression. The value 1, 2, n represents the case labels and they are used to identify each case particularly. Each label must be ended with the colon(:).<\/p>\n\n\n\n<p>The labels must be unique because same name label will create the problem while running the program.<\/p>\n\n\n\n<p>A block is associated with the case label. Block is nothing but a group of multiple statements for a particular case.<\/p>\n\n\n\n<p>Once the switch expression is evaluated, the expression value is compared with all cases which we have defined inside the switch case. Suppose the value of the expression is 2, then compared with each case until it found the label 2 in the program.<\/p>\n\n\n\n<p>The&nbsp;<strong>break statement<\/strong>&nbsp;is essential to use at the end of each case. If we do not put the break statement, then even the specific case is found, it will execute all the cases until the program end is reached. The&nbsp;<strong>break<\/strong>&nbsp;keyword is used to declare the break statement.<\/p>\n\n\n\n<p>Sometimes the value of the expression is not matched with any of the cases; then the default case will be executed. It is optional to write in the program.<\/p>\n\n\n\n<p>Let&#8217;s understand the following example.<a href=\"https:\/\/www.javatpoint.com\/dart-switch-case-statement#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-switch-case-statement#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-switch-case-statement#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main() {  \n        int n = 3;  \n        switch (n) {  \n            case 1:  \n                print(\"Value is 1\");  \n                break;  \n            case 2:  \n                print(\"Value is 2\");  \n                break;  \n            case 3:  \n                print(\"Value is 3\");  \n                break;  \n            case 4:  \n                print(\"Value is 4\");  \n                break;  \n            default:  \n                print(\"Out of range\");  \n                break;  \n        }  \n    }  <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Value is 3\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above program, we have initialized the variable&nbsp;<strong>n<\/strong>&nbsp;with value 3. We constructed the switch case with the expression, which is used to compare the each case with the variable n. Since the value is 3 then it will execute the case-label 3. If found successfully case-label 3, and printed the result on the screen.<\/p>\n\n\n\n<p>Let&#8217;s have a look at another scenario.<\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><a href=\"https:\/\/www.javatpoint.com\/dart-switch-case-statement#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-switch-case-statement#\"><\/a><a href=\"https:\/\/www.javatpoint.com\/dart-switch-case-statement#\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void main()  \n {  \n \/\/ declaring a interger variable   \nint Roll_num =  90014;  \n   \n\/\/ Evalaute the test-expression to find the match  \n  switch (Roll_num) {  \n  case 90009:  \n    print(\"My name is Joseph\");  \n    break;  \n  case 90010:  \n    print(\"My name is Peter\");  \n    break;  \n  case 090011:  \n    print(\"My name is Devansh\");  \n    break;  \n  \n\/\/ default block  \n  default:  \n    print(\"Roll number is not found\");  \n}  \n}   <\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Roll number is not found\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211;<\/strong><\/p>\n\n\n\n<p>In the above program, we have initialized the variable&nbsp;<strong>Roll_num<\/strong>&nbsp;with the value of 90014. The switch test-expression checked all cases which are declared inside the switch statement. The test-expression did not found the match in cases; then it printed the default case statement.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Benefit of Switch case<\/h2>\n\n\n\n<p>As we discussed above, the switch case is a simplified form of if nested if-else statement. The problem with the nested if-else, it creates complexity in the program when the multiple paths increase. The switch case reduces the complexity of the program. It enhances the readability of the program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dart Switch case statement is used to avoid the long chain of the if-else statement. It is the simplified form of nested if-else statement. The value of the variable compares with the multiple cases, and if a match is found, then it executes a block of statement associated with that particular case. The assigned value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[916],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3596"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=3596"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/3596\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=3596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=3596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=3596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}