Csharp/C Sharp/Language Basics/Goto — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:39, 26 мая 2010
Demonstrate the goto
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the goto.
using System;
public class Use_goto {
public static void Main() {
int i=0, j=0, k=0;
for(i=0; i < 10; i++) {
for(j=0; j < 10; j++ ) {
for(k=0; k < 10; k++) {
Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
if(k == 3) goto stop;
}
}
}
stop:
Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);
}
}
Goto Tester
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class GotoTester
{
public static void Main()
{
int counterVariable = 0;
repeat: // the label
Console.WriteLine(
"counterVariable: {0}",counterVariable);
// increment the counter
counterVariable++;
if (counterVariable < 10)
goto repeat; // the dastardly deed
}
}
the goto statement
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_15.cs illustrates the use of
the goto statement
*/
public class Example4_15
{
public static void Main()
{
int total = 0;
int counter = 0;
myLabel:
counter++;
total += counter;
System.Console.WriteLine("counter = " + counter);
if (counter < 5)
{
System.Console.WriteLine("goto myLabel");
goto myLabel;
}
System.Console.WriteLine("total = " + total);
}
}
Use goto with a switch
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use goto with a switch.
using System;
public class SwitchGoto {
public static void Main() {
for(int i=1; i < 5; i++) {
switch(i) {
case 1:
Console.WriteLine("In case 1");
goto case 3;
case 2:
Console.WriteLine("In case 2");
goto case 1;
case 3:
Console.WriteLine("In case 3");
goto default;
default:
Console.WriteLine("In default");
break;
}
Console.WriteLine();
}
// goto case 1; // Error! Can"t jump into a switch.
}
}