Advertisement
ada1711

Untitled

Jul 20th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. Review Materials
  2. Lesson 5-6. Conditional Instruction IF.
  3.  
  4. Lesson Objective
  5. The goal of the lesson is to present the IF conditional instruction.
  6.  
  7. Conditional instructions allow for executing specific parts of a program depending on the conditions met. The IF instruction appears in almost every programming language, although its syntax may vary slightly.
  8.  
  9. This instruction has several variants. In C#, the simplest syntax looks like this:
  10. ```csharp
  11. if (logical expression)
  12. {
  13. //code block that executes if the logical expression is true;
  14. }
  15. ```
  16.  
  17. A more advanced version, assuming the existence of several possible variants:
  18. ```csharp
  19. if (condition_1)
  20. {
  21. //if the condition is met, this piece of code will execute;
  22. }
  23. else if (condition_2)
  24. {
  25. //executes if condition_1 is not met, but condition_2 is;
  26. }
  27. else // in all other cases, this piece of code will execute
  28. {
  29. //code block;
  30. }
  31. ```
  32. Example 1:
  33. ```csharp
  34. int age = Console.Readline();
  35. if (age == 17)
  36. {
  37. Console.WriteLine("You will reach adulthood this year");
  38. }
  39. ```
  40.  
  41. Example 2:
  42. ```csharp
  43. int number1 = 4;
  44. int number2 = 5;
  45.  
  46. if (number1 > number2)
  47. {
  48. Console.WriteLine($"{number1} is greater than {number2}");
  49. }
  50. else if (number2 > number1)
  51. {
  52. Console.WriteLine($"{number2} is greater than {number1}");
  53. }
  54. else
  55. {
  56. Console.WriteLine("The numbers are equal");
  57. }
  58. Console.WriteLine(number1);
  59. ```
  60.  
  61. Nesting instructions allows for adding conditional instructions inside another instruction.
  62.  
  63. Example task - A presidential candidate must be at least 35 years old and have collected 100,000 votes. Check if you can run for president.
  64.  
  65. ```csharp
  66. if( are you 35 years old? )
  67. {
  68. if( Have you collected 100,000 votes? )
  69. {
  70. You can run for president!
  71. }
  72. else
  73. {
  74. You cannot run for president
  75. }
  76. }
  77. else
  78. {
  79. You cannot run for president
  80. }
  81. ```
  82.  
  83. Try to convert the above pseudocode into a working program 😉
  84.  
  85. Watch out for these mistakes!
  86. - Never place a semicolon after the parentheses in if.
  87. ```csharp
  88. if(condition); <- WRONG!
  89. {
  90. }
  91. ```
  92.  
  93. - Remember that a variable declared inside a block is not accessible outside it.
  94. ```csharp
  95. if(condition)
  96. {
  97. int x = 1;
  98. }
  99. x++; <- Error! The variable x is not accessible here!
  100. ```
  101.  
  102. Additional Programmer's Mission:
  103. Create a program that draws 2 numbers from the range of 0 to 100, then check (using the If conditional instruction) if their sum is greater than 50. The program should display a message indicating whether the condition is met or not.
  104.  
  105. Hint - the draw can be implemented as follows:
  106. ```csharp
  107. Random randomMachine = new Random();
  108. int drawnValue = randomMachine.Next(1, 101);
  109. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement