Continue tutorial, this articleArticleThe following describes the statements and keywords used to control the process. In C #, the statements used to control the process include: if, while, dowhile, switch, for, foreach, Goto, break, and continue, most of them are used in our daily development, and this article mainly covers the missing knowledge.
The following is what we will introduce:
1. Loop statements: For and foreach
2. Break, return, and goto in the switch
I. Loop statements: For and foreach
Loop statements are common in all languages, and the following areA For loop I have never usedAndWhen using foreach, I sometimes make a mistake.:
1,A For loop I have never used, The general definition of the For LoopCodeAs follows:
For (INT I = 0; I <count; I ++) {// do something}
Over time, I used to think that a for loop can only contain one parameter. In fact, a for loop can actually contain multiple parameters and make some special judgments, separate multiple parameters with commas (,), as shown in the following code:
For (INT I = 0, j = 10; (I> 5) & (j <5); I ++, j --) {// do something ;}
Of course, the middle part can also be used for logical judgment or, maybe, or more complex judgment, although this writing method is not common (at least I did not use it, or I am too weak ), however, it should be useful in some cases.
2,When using foreach, I sometimes make a mistake.This error is also common, as shown in the following code:
Datarow ROW = new datarow (); foreach (VAR item in datatable. Rows) {item = row ;}
This is a typical case where foreach cannot modify variables in the loop body, such as the row in the above Code. If you follow the above code, you can see the problem at a glance, however, in actual development, sometimes foreach has many other logic code, which is often ignored, so remember.
Ii. Break, return, and goto in the switch
All the friends who have used the switch know that the switch is a solution to replace multiple if statements, and the break keyword must be written under the case clause in the switch to jump out. Otherwise, an error occurs, of course, there are more than one solution. There are two keywords in C # To solve this problem -- Return and goto. The following describes how to use the two statements in the switch:
1,Return, Return is mainly used for return. If return is used in the case clause of the switch to replace break, all code after the switch will not be executed and will jump directly to the end of the method, therefore, you must be cautious when using it.
2,Goto, Goto is a rare keyword, because its use will lead to the entireProgramThe C # syntax also specifies that it does not allow the use of goto to jump from one method to another, so it can be used as little as possible, the usage of goto in a switch has the following special characteristics:
Switch (a) {Case 1: var B = 1; goto default; Case 2: var c = 1; goto default; default: var d = 1; break ;}
You can use the GOTO statement to jump to the default when you need to use the default statement in any situation in the switch, achieving simple code reuse.