Facebook
From Arslan, 2 Weeks ago, written in C#.
Embed
Download Paste or View Raw
Hits: 120
  1. /*
  2. // Variables
  3. int age = 12;
  4. int wealth;
  5. wealth = 120;
  6.  
  7. // Input / Output
  8. Console.Write("Enter Your name: ");
  9. string name = Console.ReadLine();
  10. Console.Write("Enter Amount of Cash: ");
  11. int money = Convert.ToInt32(Console.ReadLine());
  12.  
  13. // Conditions and string Templating
  14. if (money > 10) {
  15.     Console.WriteLine($"{name} is allowed in the party.");
  16. }else {
  17.     Console.WriteLine($"{name} is not allowed in the party.");
  18. }
  19. */
  20.  
  21. // Arrays [predefined number of items] and Lists [unlimited number of items]
  22. // Arrays:
  23. /*
  24. string[] provinces = new string[5] {"Sindh", "Punjab", "Khyber Pakhtun Khuwa", "Balochistan", "Gilgit"};
  25.  
  26. Console.WriteLine(provinces[0]);
  27. Console.WriteLine(provinces[4]);
  28. Console.WriteLine();
  29.  
  30. provinces[0] = "Gilgit";
  31. provinces[4] = "Sindh";
  32.  
  33. for(int i = 0; i < 5; i++) {
  34.     Console.Write($"{i}.{provinces[i]}, ");
  35. }
  36. Console.WriteLine();
  37. for(int i = 0; i < provinces.Length; i++) {
  38.     Console.Write($"{i}.{provinces[i]}, ");
  39. }
  40. Console.WriteLine();
  41. foreach(string province in provinces) {
  42.     Console.Write($"{province}, ");
  43. }
  44. Console.WriteLine();
  45. */
  46.  
  47. // Lists:
  48. /*
  49. List<string> friends = new List<string>() {"Azir"};
  50.  
  51. Console.Write("to add a friend type f, to view all friends type v, press any other key to exit: ");
  52.  
  53. string command = Console.ReadLine();
  54.  
  55. if(command == "v" || command == "V") {
  56.     Console.WriteLine("Viewing Friends");
  57. }else if(command == "f" || command == "F") {
  58.     Console.Write("Enter Friend Name: ");
  59.     friends.Add(Console.ReadLine());
  60. }else {
  61.     Console.WriteLine("Exiting...");
  62. }
  63. */
  64.  
  65.  
  66. // Function: A block of code
  67. /*
  68. void CreateFriends() {
  69.     Console.WriteLine("I am your friend");
  70.     Console.WriteLine("Please Talk To Me!");
  71.     Console.Write("What is Your Name: ");
  72.     string name = Console.ReadLine();
  73.     Console.WriteLine($"Good to meet you {name}");
  74. }
  75.  
  76. int multiply(int number1, int number2) {
  77.     return number1 * number2;
  78. }
  79.  
  80. int luckyNumber() {
  81.     return 7;
  82. }
  83.  
  84. string increaseBy2(int num) {
  85.  
  86.     Console.WriteLine($"{num} + 2 = {num + 2}");
  87.  
  88.     return "Azir";
  89. }
  90.  
  91. Console.WriteLine(increaseBy2(10));
  92. */
  93.  
  94.  
  95. // Loops: For repeating commands, foreach, while
  96. // Reusing with list example
  97. /*
  98. List<string> friends = new List<string>() {"Azir"};
  99. bool shouldContinue = true;
  100. while(shouldContinue) {
  101.     Console.Write("to add a friend type f, to view all friends type v, press any other key to exit: ");
  102.  
  103.     string command = Console.ReadLine();
  104.  
  105.     if(command == "v" || command == "V") {
  106.         Console.Write("Friends: ");
  107.         foreach(string friendName in friends) {
  108.             Console.Write($"{friendName}, ");
  109.         }
  110.         Console.Write("n");
  111.     }else if(command == "f" || command == "F") {
  112.         Console.Write("Enter Friend Name: ");
  113.         friends.Add(Console.ReadLine());
  114.     }else {
  115.         Console.WriteLine("Exiting...");
  116.         shouldContinue = false;
  117.     }
  118. }
  119. */
  120.  
  121. // Loops: for
  122. /*
  123. for(int i = 0; i < 10; i++) {
  124.      Console.WriteLine($"{i} -> Hi");
  125. }
  126. */