This afternoon I encountered a problem where I used the branch clause in the following sentence. I want to jump out of the Branch and the cycle in the branch. I didn't want to use the Skip phrase for a moment. So I found some information on the Internet.
This article Article Think it is not necessary. Add it to favorites.
---------------------------------------------------------
The break statement is used to terminate the recent closed loop or its switch statement. Control the statements passed to the end statement (if any ).
The continue statement passes control to the next iteration of its closed iteration statement.
The GOTO statementProgramControl is passed directly to the mark statement.
A common usage of goto is to pass the control to a specific switch-case label or the default label in the switch statement.
The GOTO statement is also used to jump out of a deep nested loop.
The Return Statement terminates the execution of the method in it and returns the control to the calling method. It can also return an optional value. If the method is void type, you can omit the return statement.
Throw statements are used to send signals that encounter abnormal conditions (Exceptions) during program execution.
Generally, throw statements are used together with try-catch or try-finally statements.
When an exception is thrown, the program looks for the catch statement that handles the exception. You can also use the throw statement to re-raise caught exceptions.
-------------------------------------------------------------
----------
Break example
----------
In this example, the condition statement contains a counter from 1 to 100;
However, the break statement terminates the loop after the count reaches 4. // Statements_break.cs
Using System;
Class Breaktest
{
Static Void Main ()
{
For ( Int I = 1 ; I <= 100 ; I ++ )
{
If (I = 5 )
{
Break ;
}
Console. writeline (I );
}
}
}
Output
1
2
3
4
--------------
Continue example
--------------
In this example, the counter is initially counted from 1 to 10,
However, by using the continue statement with the expression (I <9,
The statement between the end of the continue and the for loop body is skipped. // Statements_continue.cs
Using System;
Class Continuetest
{
Static Void Main ()
{
For ( Int I = 1 ; I <= 10 ; I ++ )
{
If (I < 9 )
{
Continue ;
}
Console. writeline (I );
}
}
}
Output
9
10
----------
Goto Example 1
----------
The following example shows how to use goto in a switch statement.
// Statements_goto_switch.cs
Using System;
Class Switchtest
{
Static Void Main ()
{
Console. writeline ( " Coffee sizes: 1 = small 2 = medium 3 = large " );
Console. Write ( " Please enter your selection: " );
String S = Console. Readline ();
Int N = Int . Parse (s );
Int Cost = 0 ;
Switch (N)
{
Case 1 :
Cost + = 25 ;
Break ;
Case 2 :
Cost + = 25 ;
Goto Case 1 ;
Case 3 :
Cost + = 50 ;
Goto Case 1 ;
Default :
Console. writeline ( " Invalid selection. " );
Break ;
}
If (Cost ! = 0 )
{
Console. writeline ( " Please insert {0} cents. " , Cost );
}
Console. writeline ( " Thank you for your business. " );
}
}
Input
2
Sample output
Coffee sizes: 1 = small 2 = medium 3 = large
Please enter your selection: 2
Please insert 50 cents.
Thank you for your business.
----------
Goto Example 2
----------
The following example demonstrates how to use goto to exit a nested loop.
// Statements_goto.cs
// Nested search Loops
Using System;
Public Class Gototest1
{
Static Void Main ()
{
Int X = 200 , Y = 4 ;
Int Count = 0 ;
String [,] Array = New String [X, y];
//Initialize the array:
For(IntI= 0; I<X; I++)
for ( int j = 0 ; j Y; j + )
array [I, j] = ( ++ count ). tostring ();
// read input:
console. write ( " enter the number to search for: " );
//Input a string:
StringMynumber=Console. Readline ();
// Search:
For ( Int I = 0 ; I < X; I ++ )
{
For ( Int J = 0 ; J < Y; j ++ )
{
If (Array [I, j]. Equals (mynumber ))
{
Goto Found;
}
}
}
Console. writeline ("The number {0} was not found.", Mynumber );
GotoFinish;
Found:
Console. writeline ("The number {0} is found.", Mynumber );
Finish:
Console. writeline ("End of search.");
}
}
Input
44
Sample output
Enter the number to search for: 44
The number 44 is found.
End of search.
----------
Throw example
----------
This example shows how to use the throw statement to raise an exception. // Throw example
Using System;
Public Class Throwtest
{
Static Void Main ()
{
String S = Null ;
If (S = null )
{< br> throw New argumentnullexception ();
}
Console. Write ("The string S is null");//Not executed
}
}
Auto: http://www.cnblogs.com/freeliver54/archive/2007/01/30/634620.html