Advertisement
BaSs_HaXoR

GoTo Function -> Function: (C#) (AWESOME!)

Sep 19th, 2014
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. //SOURCE: http://msdn.microsoft.com/en-us/library/13940fs2.aspx
  2. //HOW TO RETURN TO A CERTAIN SPOT IN CODE, C# 2lines of code :p
  3.  
  4. //func: & goto func;
  5.  
  6.  class SwitchTest
  7.     {
  8.         static void Main()
  9.         {
  10.             Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
  11.             Console.Write("Please enter your selection: ");
  12.             string s = Console.ReadLine();
  13.             int n = int.Parse(s);
  14.             int cost = 0;
  15.             switch (n)
  16.             {
  17.                 case 1:
  18.                     cost += 25;
  19.                     break;
  20.                 case 2:
  21.                     cost += 25;
  22.                     goto case 1;
  23.                 case 3:
  24.                     cost += 50;
  25.                     goto case 1;
  26.                 default:
  27.                     Console.WriteLine("Invalid selection.");
  28.                     break;
  29.             }
  30.             if (cost != 0)
  31.             {
  32.                 Console.WriteLine("Please insert {0} cents.", cost);
  33.             }
  34.             Console.WriteLine("Thank you for your business.");
  35.  
  36.             // Keep the console open in debug mode.
  37.             Console.WriteLine("Press any key to exit.");
  38.             Console.ReadKey();
  39.         }
  40.     }
  41.     /*
  42.     Sample Input:  2
  43.  
  44.     Sample Output:
  45.     Coffee sizes: 1=Small 2=Medium 3=Large
  46.     Please enter your selection: 2
  47.     Please insert 50 cents.
  48.     Thank you for your business.
  49.     */
  50. //The following example demonstrates using goto to break out from nested loops.
  51. ///C#
  52. public class GotoTest1
  53. {
  54.     static void Main()
  55.     {
  56.         int x = 200, y = 4;
  57.         int count = 0;
  58.         string[,] array = new string[x, y];
  59.  
  60.         // Initialize the array:
  61.         for (int i = 0; i < x; i++)
  62.  
  63.             for (int j = 0; j < y; j++)
  64.                 array[i, j] = (++count).ToString();
  65.  
  66.         // Read input:
  67.         Console.Write("Enter the number to search for: ");
  68.  
  69.         // Input a string:
  70.         string myNumber = Console.ReadLine();
  71.  
  72.         // Search:
  73.         for (int i = 0; i < x; i++)
  74.         {
  75.             for (int j = 0; j < y; j++)
  76.             {
  77.                 if (array[i, j].Equals(myNumber))
  78.                 {
  79.                     goto Found;
  80.                 }
  81.             }
  82.         }
  83.  
  84.         Console.WriteLine("The number {0} was not found.", myNumber);
  85.         goto Finish;
  86.  
  87.     Found:
  88.         Console.WriteLine("The number {0} is found.", myNumber);
  89.  
  90.     Finish:
  91.         Console.WriteLine("End of search.");
  92.  
  93.  
  94.         // Keep the console open in debug mode.
  95.         Console.WriteLine("Press any key to exit.");
  96.         Console.ReadKey();
  97.     }
  98. }
  99. /*
  100. Sample Input: 44
  101.  
  102. Sample Output
  103. Enter the number to search for: 44
  104. The number 44 is found.
  105. End of search.
  106. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement