1. If statement of the branch statement
1, the Process Control statement is the core part of the program, for any programming language is very important, is the control program execution flow of the basic statement. If a language lacks process control, it lacks control over the flow of the program, and it cannot be called a computer language.
2, C # language provides a rich, flexible control of the flow of statements, mainly divided into branch statements, iteration statements, jump statements three categories.
The branch statement is the IF statement and the switch statement, and the logical path code can be determined according to the actual situation.
if ( judge conditional expression) {// expression evaluates to True when executing } else{ // Execution When the expression result is false }
3, the input of the number to judge
> 10 tips greater than 10
< 10 tips less than 10
= 10 hint equals 10
4. New statement
int Parse (Console.ReadLine ()); // An integer that accepts user input
The procedure is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_5._1_ Branch Statement _if_ statement {classProgram {Static voidMain (string[] args) { //determining the relationship between a stored value and 10 in a variable aConsole.WriteLine ("Please enter a value to determine its relationship to 10:"); intA =int. Parse (Console.ReadLine ()); //Int. Parse is used to convert a screen input statement to an integer type if(A <Ten) {Console.WriteLine ("a less than ten"); } if(A = =Ten) {Console.WriteLine ("a equals ten"); } if(A >Ten) {Console.WriteLine ("a greater than ten"); }/*else//If one of the preceding if statements is not executed, then the ELSE statement is executed///If one of the IF statements is executed, then the ELSE statement is not executed { Console.WriteLine ("cannot be judged"); }*/Console.readkey (); } }}
Operation Result:
2. Swirch Statement of branch statement
Instance:
1, input 1 display as Monday, and so on
The procedure is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceThe switch statement for the _5._2 branch statement {classProgram {Static voidMain (string[] args) { //Input 1 show Monday, and so onConsole.WriteLine ("Please enter a number in 1-7."); intWeek =int. Parse (Console.ReadLine ()); //if (week = = 1) Console.WriteLine ("Monday"); //if (week = = 2) Console.WriteLine ("Tuesday"); //if (week = = 3) Console.WriteLine ("Wednesday"); //if (week = = 4) Console.WriteLine ("Thursday"); //if (week = = 5) Console.WriteLine ("Friday"); //if (week = = 6) Console.WriteLine ("Saturday"); //if (week = = 7) Console.WriteLine ("Sunday"); Switch(week) { Case 1: Console.WriteLine ("Monday"); Break; Case 2: Console.WriteLine ("Tuesday"); Break; Case 3: Console.WriteLine ("Wednesday"); Break; Case 4: Console.WriteLine ("Thursday"); Break; Case 5: Console.WriteLine ("Friday"); Break; Case 6: Console.WriteLine ("Saturday"); Break; Case 7: Console.WriteLine ("Week seven"); Break; default: Console.WriteLine ("your input is incorrect"); Break; } console.readkey (); } }}
Operation Result:
2. Determine the number of days per month in 2015
The procedure is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceThe switch statement for the _5._2 branch statement {classProgram {Static voidMain (string[] args) { //determine the number of days per month in 2015//31 Days: 1, 3, 5, 7, 8, 10,//30 Days: 4, 6, 9, one//28 Days: 2Console.WriteLine ("Please enter the month number"); intmonth =int. Parse (Console.ReadLine ()); Switch(month) { Case 2: Console.WriteLine ("There are 28 days this month"); Break; Case 4: Case 6: Case 9: Case One: Console.WriteLine ("There are 30 days this month"); Break; default: Console.WriteLine ("There are 31 days this month"); Break; } console.readkey (); } }}
Operation Result:
1. Structure:
Switch (expression) { case constant expression: conditional statement; Case constant expression: conditional statement; Case constant expression: conditional statement; Case constant expression: conditional statement; ...... default : conditional statement;}
C # "Five" Process Control (1)