C # syntax ---- program structure (2)

Next, we continue to learn grammar program flow control!

switch-case

For processing multi-value determination condition.

grammar:

switch (value of a variable or expression)

{

   case value1: 1 code to be executed;

   break;

   case value2: 2 code to be executed;

 

   break;

 

   case value3: 3 code to be executed;

 

   break;

   ........

   default: Code 4 to be executed;

   break;

 

 

}

Execution: to switch the program execution, the calculated value of the first bracket or expression, and then holding the value of the primary values ​​of the case, and match, once

          Successfully matched, case corresponding code is executed, encountered break, then out of the current cycle. If each case values ​​do not match, perform default

          The corresponding code.

Exemplified below (in the example section):

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             bool b = true;
14             double salary = 5000;
15             Console.WriteLine ( " Please enter the end of the assessment of John Doe " );
 16              String Level = Console.ReadLine ();
 . 17              Switch (Level)
 18 is              {
 . 19                  Case  " A " : the salary + = 500 ;               
 20 is                      BREAK ;
 21 is                  Case  " B " :
 22 is                      the salary + = 200 is ;
 23 is                      BREAK ;
 24                  Case  " C " :
 25                      BREAK ;
26 is                  Case  " D " :
 27                      the salary - = 200 is ;
 28                      BREAK ;
 29                  Case  " E " :
 30                      the salary - = 500 ;
 31 is                      BREAK ;
 32                  default : Console.WriteLine ( " input error, the program exits " );
 33 is                      B = to false ;
 34 is                      BREAK ;
 35              }
 36              IF (B)
 37 [              {
38 is                  Console.WriteLine ( " John Doe salary next year is 0} { " , the salary);
 39              }           
 40              the Console.ReadKey ();
 41 is          }
 42 is      }
 43 is }

 

Let us consolidate what we learn, following the completion of several exercises:

(1) allows the user to enter the name (Yang, the old Soviet Union, horse, Chiang Kai-shek, cow, tiger, Zhao), last generation of occupation display

. 1  the using the System;
 2  the using the System.Collections.Generic;
 . 3  the using the System.Linq;
 . 4  the using the System.Text;
 . 5  the using System.Threading.Tasks;
 . 6  
. 7  namespace draft
 . 8  {
 . 9      class Program
 10      {
 . 11          static  void the Main ( String [ ] args)
 12 is          {
 13 is              Console.WriteLine ( " enter a name " );
 14              String name =Console.ReadLine ();
 15              Switch (name)
 16              {
 . 17                  Case  " Yang " : Console.WriteLine ( " previous life of opium " );
 18 is                      BREAK ;
 . 19                  Case  " old Soviet Union " :
 20 is                      Console.WriteLine ( " Last generation is Lao Baozi " );
 21                      BREAK ;
 22                  Case  " old horse " :
 23                      Console.WriteLine ( " previous life of the old Soviet Union men first card " );
24                      BREAK ;
 25                  Case  " Laojiang " :
 26 is                      Console.WriteLine ( " previous life pimp " );
 27                      BREAK ;
 28                  Case  " cow " :
 29                      Console.WriteLine ( " previous life cook Cheung " );
 30                      BREAK ;
 31 is                  Case  " tiger " :
 32                      Console.WriteLine ( " previous life big sick cat " );
 33 is                      BREAK ;
34 is                  Case  " Zhao " :
 35                      Console.WriteLine ( " last generation is a widely horse lofty Buddha " );
 36                      BREAK ;
 37 [                  default : Console.WriteLine ( " do not know, it is estimated that cook Cheung " );
 38 is                      BREAK ;
 39              }
 40              the Console.ReadKey ();
 41 is          }
 42 is      }
 43 is }

Guess you like

Origin www.cnblogs.com/LiyuLi/p/12079699.html
Recommended