Advertisement
ada1711

Untitled

Jul 21st, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.48 KB | None | 0 0
  1. **Lesson 7. Switch Conditional Instruction**
  2. **Lesson Objective:**
  3. The goal of the lesson is to present the Switch conditional instruction.
  4.  
  5. **Agenda:**
  6. - Questions from the previous lesson and warm-up
  7. - Switch instruction
  8. - Tasks
  9. - Summary
  10.  
  11. **Questions from the previous lesson:**
  12. - What is the syntax of the IF conditional instruction?
  13. - Which instruction can we use to convert a lowercase letter to an uppercase one?
  14. - How to generate a random number?
  15.  
  16. **Warm-up Task: Day of the Week.**
  17. Write a console program that, after reading the user's input of the day number of the week (1-7), displays the name of that day (e.g., "The 3rd day of the week is Wednesday."). Assume that the first day of the week is Monday. Use the if..else if instruction.
  18. Extended version: Add detection of incorrect values (outside 1-7) and display an appropriate message.
  19.  
  20. ```csharp
  21. static void Main(string[] args)
  22. {
  23. Console.Write("Enter the day number of the week (1-7): ");
  24. int day = int.Parse(Console.ReadLine());
  25. if (day == 1)
  26. Console.WriteLine($"{day} day of the week is Monday.");
  27. else if (day == 2)
  28. Console.WriteLine($"{day} day of the week is Tuesday.");
  29. else if (day == 3)
  30. Console.WriteLine($"{day} day of the week is Wednesday.");
  31. else if (day == 4)
  32. Console.WriteLine($"{day} day of the week is Thursday.");
  33. else if (day == 5)
  34. Console.WriteLine($"{day} day of the week is Friday.");
  35. else if (day == 6)
  36. Console.WriteLine($"{day} day of the week is Saturday.");
  37. else if (day == 7)
  38. Console.WriteLine($"{day} day of the week is Sunday.");
  39. else
  40. Console.WriteLine("Invalid day number!");
  41. Console.ReadKey();
  42. }
  43. ```
  44.  
  45. **Switch**
  46. The IF instruction is the basic conditional instruction that we use when we want to check something. However, it is inconvenient when we have many conditions, especially when they are based on comparing the same data. In such situations, we can use the Switch conditional instruction. It is based on selecting the appropriate case for the given values of the checked variable. Depending on the value of the variable in the switch..case, a different part of the program will execute.
  47.  
  48. **Switch command syntax:**
  49. ```csharp
  50. switch (variable)
  51. {
  52. case variable_value:
  53. commands;
  54. break;
  55.  
  56. case another_variable_value:
  57. commands;
  58. break;
  59.  
  60. default: // optional
  61. commands;
  62. break;
  63. }
  64. ```
  65.  
  66. - **switch** - name of the instruction
  67. - **variable** - value from which the expression to be checked in the case will be created
  68. - **case** - case to be executed for a specific variable value - entry into the instruction occurs when the variable value matches the expression (equivalent to if)
  69. - **break** - word ending the instructions
  70. - **default** - (optional) default instruction, which will execute if no previous condition is met (equivalent to else)
  71.  
  72. **Example of using switch:**
  73. ```csharp
  74. static void Main(string[] args)
  75. {
  76. Console.WriteLine("What grade did you get?");
  77. char grade = char.Parse(Console.ReadLine());
  78. switch (grade)
  79. {
  80. case '6':
  81. Console.WriteLine("Excellent!");
  82. break;
  83. case '5':
  84. case '4':
  85. Console.WriteLine("Very good");
  86. break;
  87. case '3':
  88. Console.WriteLine("Not bad");
  89. break;
  90. case '2':
  91. Console.WriteLine("Better improve it!");
  92. break;
  93. case '1':
  94. Console.WriteLine("You won't pass!");
  95. break;
  96. default:
  97. Console.WriteLine("Invalid grade");
  98. break;
  99. }
  100. Console.ReadLine();
  101. }
  102. ```
  103.  
  104. It is worth emphasizing the importance of the break; instruction after case, which ends the execution of the switch and checking subsequent conditions. Omitting it allows us to collectively handle several cases and perform the same action for several cases (as in case '5': case '4' in the example above). Students should note that creating a case for the same values is an error.
  105.  
  106. **Warm-up Task: Continuation with modification.**
  107. For the program converting the day of the week to its name, use the switch conditional instruction.
  108.  
  109. ```csharp
  110. Console.Write("Enter the day number of the week (1-7): ");
  111. int day = int.Parse(Console.ReadLine());
  112.  
  113. switch (day)
  114. {
  115. case 1:
  116. Console.WriteLine($"{day} day of the week is Monday.");
  117. break;
  118. case 2:
  119. Console.WriteLine($"{day} day of the week is Tuesday.");
  120. break;
  121. case 3:
  122. Console.WriteLine($"{day} day of the week is Wednesday.");
  123. break;
  124. case 4:
  125. Console.WriteLine($"{day} day of the week is Thursday.");
  126. break;
  127. case 5:
  128. Console.WriteLine($"{day} day of the week is Friday.");
  129. break;
  130. case 6:
  131. Console.WriteLine($"{day} day of the week is Saturday.");
  132. break;
  133. case 7:
  134. Console.WriteLine($"{day} day of the week is Sunday.");
  135. break;
  136. default:
  137. Console.WriteLine("Invalid day number!");
  138. break;
  139. }
  140. Console.ReadKey();
  141. ```
  142.  
  143. **Task 1: Bestiary**
  144. Create a console application that asks for the name of a monster from your favorite RPG game or mythology and displays a short description of the beast. The description can be made up or taken from the internet (under no circumstances ask students to copy text). Remember to add `using System.Threading;` at the very top to properly use the command to pause the program with `Thread.Sleep(3000);`.
  145.  
  146. It's worth introducing some AI at this point and asking ChatGPT or Copilot to generate such a conversation or descriptions of beasts.
  147. - Microsoft Copilot: [Copilot](https://copilot.microsoft.com/)
  148. - OpenAI ChatGPT: [ChatGPT](https://chat.openai.com/)
  149. - Google Gemini: [Gemini](https://gemini.google.com/)
  150.  
  151. In the end, send sample Bestiaries:
  152. - The Witcher Bestiary: [The Witcher Bestiary](https://gexe.pl/wiedzmin-3/art/5176,bestiariusz)
  153. - Minecraft Mob Bestiary: [Minecraft Mob Bestiary](https://minecraft.fandom.com/wiki/Mob#Hostile_mobs)
  154. - Slavic Mythology: [Slavic Mythology](https://en.wikipedia.org/wiki/Slavic_mythology)
  155. - Greek Mythology: [Greek Mythology](https://en.wikipedia.org/wiki/Category:Creatures_in_Greek_mythology)
  156.  
  157. ```csharp
  158. Console.WriteLine("Welcome Hunter, what monster are you hunting?");
  159. string monster = Console.ReadLine();
  160.  
  161. Console.Clear();
  162.  
  163. Console.WriteLine($"{monster} you say... Let me think...");
  164. Thread.Sleep(3000);
  165. Console.Clear();
  166.  
  167. switch (monster.ToLower())
  168. {
  169. case "gryphon":
  170. Console.WriteLine("A gryphon looks like a combination of a lion (tail and fur) and an eagle (sharp, curved beak, claws, and huge wings).");
  171. break;
  172. case "fiend":
  173. Console.WriteLine("In the eyes of an average resident of the Northern Kingdoms, a fiend is the embodiment of everything evil and devilish, which can be noticed in many well-known proverbs.");
  174. break;
  175. case "troll":
  176. Console.WriteLine("We encounter trolls mainly in the mountains. Lower parts are inhabited by rock trolls, while ice trolls settle among the snowy slopes of Skellige and the Northern Kingdoms.");
  177. break;
  178. default:
  179. Console.WriteLine("Unfortunately, I don't know such a creature.");
  180. break;
  181. }
  182.  
  183. Console.ReadKey();
  184. ```
  185.  
  186. **Task 2: Warm, Cold.**
  187. Create a console application that draws a number from the range of 1 to 10, and the user has to guess it in three attempts. After each response, a hint should be displayed whether the entered value is close (warm) or far (cold) from the sought number.
  188.  
  189. Hint: To easily determine the temperature, use a multi-level scale in your program based on the difference between the sought number and the entered one, e.g., Cold > 4, Cool > 3, Lukewarm > 2, Warm > 1, Very warm == 1. Maybe a method from the Math class can help you with this? ;)
  190.  
  191. ```csharp
  192. int numberToGuess;
  193. int numberOfAttempts = 0;
  194. int difference, enteredNumber;
  195.  
  196. // Create a random number generator and draw a number 1-10
  197. Random randomMachine = new Random();
  198. numberToGuess = randomMachine.Next(1, 11);
  199.  
  200. // First attempt
  201. Console.WriteLine("Enter a number (1-10): ");
  202. enteredNumber = int.Parse(Console.ReadLine());
  203. numberOfAttempts++;
  204. difference = Math.Abs(numberToGuess - enteredNumber);
  205.  
  206. switch (difference)
  207. {
  208. case 0:
  209. Console.WriteLine($"Well done, you guessed the drawn number in {numberOfAttempts} attempt(s)!");
  210. Console.ReadLine();
  211. return;
  212. case 1:
  213. Console.WriteLine("Very warm!");
  214. break;
  215. case 2:
  216. Console.WriteLine("Warm!");
  217. break;
  218. case 3:
  219. Console.WriteLine("Lukewarm!");
  220. break;
  221. case 4:
  222. Console.WriteLine("Cool!");
  223. break;
  224. default:
  225. Console.WriteLine("Cold!");
  226. break;
  227. }
  228.  
  229. // Second attempt
  230. Console.WriteLine("Enter a number (1-10): ");
  231. enteredNumber = int.Parse(Console.ReadLine());
  232. numberOfAttempts++;
  233. difference = Math.Abs(numberToGuess - enteredNumber);
  234.  
  235. switch (difference)
  236. {
  237. case 0
  238.  
  239. :
  240. Console.WriteLine($"Well done, you guessed the drawn number in {numberOfAttempts} attempt(s)!");
  241. Console.ReadLine();
  242. return;
  243. case 1:
  244. Console.WriteLine("Very warm!");
  245. break;
  246. case 2:
  247. Console.WriteLine("Warm!");
  248. break;
  249. case 3:
  250. Console.WriteLine("Lukewarm!");
  251. break;
  252. case 4:
  253. Console.WriteLine("Cool!");
  254. break;
  255. default:
  256. Console.WriteLine("Cold!");
  257. break;
  258. }
  259.  
  260. // Third attempt
  261. Console.WriteLine("Enter a number (1-10): ");
  262. enteredNumber = int.Parse(Console.ReadLine());
  263. numberOfAttempts++;
  264. difference = Math.Abs(numberToGuess - enteredNumber);
  265.  
  266. switch (difference)
  267. {
  268. case 0:
  269. Console.WriteLine($"Well done, you guessed the drawn number in {numberOfAttempts} attempt(s)!");
  270. Console.ReadLine();
  271. return;
  272. case 1:
  273. Console.WriteLine("Very warm!");
  274. break;
  275. case 2:
  276. Console.WriteLine("Warm!");
  277. break;
  278. case 3:
  279. Console.WriteLine("Lukewarm!");
  280. break;
  281. case 4:
  282. Console.WriteLine("Cool!");
  283. break;
  284. default:
  285. Console.WriteLine("Cold!");
  286. break;
  287. }
  288.  
  289. // If not guessed in three attempts
  290. Console.WriteLine($"Unfortunately, you didn't guess the number. The correct number is: {numberToGuess}");
  291. Console.ReadLine();
  292. ```
  293.  
  294. **Task 3. PC Configurator**
  295. Ask students before giving the task:
  296. - Has anyone ever assembled a computer on their own?
  297. - What basic components of a computer do you know?
  298. - What is essential for basic computer operation, and what can be omitted?
  299. - What things should be considered to play newer games?
  300.  
  301. Write a program in which you assemble the required components of every desktop computer such as: processor, motherboard, RAM, graphics card, hard drive, power supply, and case.
  302. Example online configurator: [Komputronik Advanced Configurator](https://www.komputronik.pl/advanced-configurator)
  303.  
  304. ```csharp
  305. decimal computerPrice = 0;
  306. Console.WriteLine("Welcome to the desktop computer configurator. Create your dream set!");
  307.  
  308. Console.WriteLine("Choose a processor:");
  309. Console.WriteLine("1. Intel Core i5-11400F\n2. AMD Ryzen 5 5600X ");
  310. int processor = int.Parse(Console.ReadLine());
  311.  
  312. switch (processor)
  313. {
  314. case 1:
  315. computerPrice += 769.00m;
  316. break;
  317. case 2:
  318. computerPrice += 1050.00m;
  319. break;
  320. default:
  321. Console.WriteLine("Invalid data entered");
  322. break;
  323. }
  324. Console.WriteLine($"The current value of the set is {computerPrice}");
  325. Console.ReadKey();
  326. ```
  327.  
  328. Expand the program further by copying this fragment and changing to another parameter.
  329. ```csharp
  330. Console.WriteLine("Choose a graphics card:");
  331. Console.WriteLine("1. NVIDIA GTX 1660 SUPER\n2. NVIDIA RTX 3060Ti ");
  332. int graphicsCard = int.Parse(Console.ReadLine());
  333.  
  334. switch (graphicsCard)
  335. {
  336. case 1:
  337. computerPrice += 1500.00m;
  338. break;
  339. case 2:
  340. computerPrice += 2400.00m;
  341. break;
  342. default:
  343. Console.WriteLine("Invalid data entered");
  344. break;
  345. }
  346. ```
  347.  
  348. Finally, summarize the total cost of the computer, e.g.:
  349. ```csharp
  350. Console.WriteLine($"Congratulations, you have configured the entire computer. The price of your set is: {computerPrice:C}");
  351. Console.ReadKey();
  352. ```
  353.  
  354. **Summary**
  355. - Provide the syntax of the switch instruction.
  356. - What function does default serve in the switch instruction?
  357. - What is the purpose of the break instruction in the switch instruction?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement