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

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...

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...

Wireless power transfer

Wireless power transfer Inventor and engineer Nikola Tesla, 'the man who invented the 20th century' theorized about wireless electricity back in the 1890s. He even demonstrated the principle by lighting up glass tubes with wireless power transmission. Nikola Tesla wanted to create the way to supply power without stringing wires. He almost accomplished his goal when his experiment led him to creation of the Tesla coil. It was the first system that could wirelessly transmit electricity. Wireless power transfer (WPT), wireless power transmission, wireless energy transmission (WET), or electromagnetic power transfer is the transmission of electrical energy without wires as a physical link. Wireless power transfer (WPT) is one of the hottest topics being actively studied, and it is being widely commercialized. In particular, there has been a rapid expansion of WPT in mobile phone chargers, stationary charging electric vehicles (EVs), and dynamic charging EVs, also called road-po...