1. Ordered Structure: --> the so-called ordered structure, as its name implies, is executed in order. All the code in C # is executed in the ascending order.
2. Branch Structure: --> ternary expression? :, If... else..., switch statement. The so-called Condition Statement.
3. Loop Structure: --> while, do... while..., for, foreach statement. The so-called loop statement
Condition Statement:
If statement
If (...)
Rules for pairing else if: else always matches the nearest if, unless other options are indicated by the cross-sign.
Copy codeThe Code is as follows: int a = 7;
If (a> 0)
{
Console. WriteLine ("this is dog ");
}
Else
{
Console. WriteLine ("this is cat ");
}
// Or
If (a> 0)
{
//...
}
Else if (a = 0)
{
//...
}
Else
{
//...
}
Switch statement.
Int a = 4; when the value of a is equal to the value after case, the case statement is executed. If none of the case statements match, the default statement is executed.
Copy codeThe Code is as follows: switch ()
{
Case 1:
Return 1;
Break;
Case 2:
Return 2;
Break;
Default:
Return 3;
Break;
}
A conditional statement contains a goto statement, which is rarely used.
Jump to the specified flag line: the following code when I enter a, the program will go to the third case and go to case "Mark2", then specifically jump to the Code in the second case statement execution.
Copy codeThe Code is as follows: static void Main (string [] args)
{
String mark = Console. ReadLine ();
Int cons = 20;
Switch (mark)
{
Case "Mark1 ":
Goto Mark1;
Case "Mark2 ":
Cons + = 20;
Break;
Case "":
Goto case "Mark2 ";
Default:
Console. WriteLine ("the node you entered is not found ");
Return;
}
Mark1:
// Console. WriteLine ("Jump to Mark1 ");
// Return;
Console. WriteLine (cons );
}
Loop statement:
While, do... while..., for, foreach statement
While (condition) {}, while is very similar to the if statement. When both conditions are met, the code is executed quickly, and the difference between the two is that while will repeatedly execute the code body when the condition is set, the if statement is executed only once.
Do {statements to be executed cyclically} while (condition); basically the same as while. Only do while will first execute a loop body in the judgment condition.
For loop, the for loop can be said to be the most used loop in C.
For (condition initialization; loop condition; condition change)
Let's do an operation that accumulates to 100.
Copy codeThe Code is as follows: int sum = 0;
For (int I = 0; I <= 100; I ++)
{
Sum + = I;
}
Foreach loop statement: a loop statement that is automatically traversed. The following is a string array. We need to traverse it. First, each bit in the array is of the string type.
So we first define a string s, and the variable s belongs to the arrt array, so the in arrt program will be automatically cyclically completed. Variable type before in foreach
Be sure to be consistent with the type of the retrieved array or anything. Otherwise, an error is reported.
Copy codeThe Code is as follows: string [] arrt = new string [] {"a", "B", "c "};
Foreach (string s in arrt)
{
Console. WriteLine (s );
}
Conditions and loops are relatively simple ....
Two keywords are involved: continue break
Then let us talk about the difference between them.
The difference between the continue break statement and the loop statement is that the break exits the loop.
When the loop statement is executed to break, the loop statement will jump out. Continue only ends the current loop and enters the next loop.
Let's take a look at the following two loop statements. The first output is 0 1 2, and the second output is skipped 3.
Copy codeThe Code is as follows: for (int a = 0; a <= 100; a ++)
{
If (a = 3)
Break;
Console. WriteLine ();
}
For (int a = 0; a <= 100; a ++)
{
If (a = 3)
Continue;
Console. WriteLine ();
}