Skip to main content

Switch - Case / Select - Case

Switch - Case / Select - Case


switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

The syntax for a switch statement in C# is as:

switch(expression) 
{     
case constant-expression1  :        
      statement(s);        
          break;     
case constant-expression2  :     
case constant-expression3  :        
        statement(s);        
              break;         
    /* you can have any number of case statements */     
default : /* Optional */     
statement(s);  
}  

The following rules apply to a switch statement −

  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, then it will raise a compile time error.

  • switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true.

This is how it works:

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break keyword terminates the switch and the flow of control jumps to the next line
  • default case executes when none of the above cases is true.
using System;    
  namespace DecisionMaking 
       {     
  class Program {        
        static void Main(string[] args) {           
                 /* local variable definition */           
                     char grade = 'B';                      
      switch (grade) {              
                  case 'A':                 
                     Console.WriteLine("Excellent!");                
                                      break;              
                  case 'B':              
                  case 'C':                 
                     Console.WriteLine("Well done");                 
                                        break;              
                  case 'D':                 
                     Console.WriteLine("You passed");                 
                                       break;              
                  case 'F':                 
                     Console.WriteLine("Better try again");                 
                                       break;                 
                  default:              
                     Console.WriteLine("Invalid grade");                 
                                       break;           
                     }           
          Console.WriteLine("Your grade is  {0}", grade);           
          Console.ReadLine();        
           }     
      }  
  }

Result:  Well done  
         Your grade is B  

Comments

Popular posts from this blog

The 120 year old light bulb that never been turned off.

Light bulb that never been turned off since 1901. ivermore's Centennial Light Bulb The Centennial Light is the world's longest-lasting light bulb, burning since 1901 , and almost never switched off. Due to its longevity, the bulb has been noted by The Guinness Book of World Records , Ripley's Believe It or Not!, and General Electric.  The Centennial Light was originally a 30-watt(or 60-watt) bulb, but is now very dim, emitting about the same light as a 4-watt nightlight. The hand-blown, carbon-filament common light bulb was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s and was invented by Adolphe A. Chaillet. The hand-blown, carbon-filament common light bulb was invented by Adolphe Chaillet, a French engineer who filed a patent for this technology. It was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s; many just like it still exist and can be found functioning. According to Zylpha Bernal Beck, the bulb was...

Robot

Robot is drawn from an old Church Slavonic word, robota, for “servitude,” “forced labor” or “drudgery.” The word, which also has cognates in German, Russian, Polish and Czech, was a product of the central European system of serfdom by which a tenant’s rent was paid for in forced labor or service. The word robot was coined by artist Josef Čapek, the brother of famed Czechoslovakian author Karel Čapek. As a word, robot is a relative newcomer to the English language. It was the brainchild of a brilliant Czech playwright, novelist and journalist named Karel Čapek (1880-1938) who introduced it in his 1920 hit play, R.U.R., or Rossum’s Universal Robots. The robots in this play were not what we would call robots today, and they weren’t made of steel, plastic, and lines of code. Those robots were manufactured as pseudo-organic components out of a substance that acted like protoplasm in a factory, then “assembled” into humanoids. Watch video --> Saudi Arabia grants citizenship to huma...

Cloud computing

Cloud computing Cloud computing is on-demand access, via the internet, to computing resources—applications, servers (physical servers and virtual servers), data storage, development tools, networking capabilities, and more—hosted at a remote data center managed by a cloud services provider (or CSP). The CSP makes these resources available for a monthly subscription fee or bills them according to usage. Simply put, cloud computing is the delivery of computing services—including servers, storage, databases, networking, software, analytics, and intelligence—over the Internet (“the cloud”) to offer faster innovation, flexible resources, and economies of scale. You typically pay only for cloud services you use, helping you lower your operating costs, run your infrastructure more efficiently, and scale as your business needs change. Cloud computing is a big shift from the traditional way businesses think about IT resources. Cloud computing has been credited with increasing competitivenes...