Advertisement
ItzEdInYourBed

69 - C++ CONDITIONALS & LOGIC

Jul 12th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.04 KB | None | 0 0
  1. [SIZE=7][B]Introduction to Conditionals & Logic[/B][/SIZE]
  2. Every program we’ve seen so far has only had one possible path of execution — they all execute line by line, from top to bottom. And every time you run one of those programs, it gives you the same exact result. But it’s the twenty-first century, and we like options!
  3. In this lesson, we will explore how programs make decisions by evaluating conditions and introduce logic into our code!
  4. We’ll be covering the following concepts:
  5. [LIST]
  6. [*][ICODE]if[/ICODE], [ICODE]else if[/ICODE], and [ICODE]else[/ICODE] statements
  7. [*][ICODE]switch[/ICODE] statements
  8. [*]Relational operators
  9. [*]Logical operators
  10. [/LIST]
  11. So… [I]if[/I] you’ve already learned these concepts in another language, go to the next lesson — [I]else[/I], prepare yourself and let’s get started!
  12.  
  13. [SIZE=7][B]Coin Flip Demo[/B][/SIZE]
  14. Before we dive deep into the syntax of the if statement, let’s do a demo!
  15.  
  16. Here, we have [URL='https://pastebin.com/utxtSVrN'][B]coinflip.cpp[/B][/URL] program that simulates a coin toss:
  17.  
  18. [LIST]
  19. [*]50% of the time, it’s Heads.
  20. [*]50% of the time, it’s Tails.
  21. [/LIST]
  22.  
  23. [SIZE=7][B]If Statement[/B][/SIZE]
  24. An [ICODE]if[/ICODE] statement is used to test an expression for truth and execute some code based on it. Here’s a simple form of the [ICODE]if[/ICODE] statement:
  25. [CODE]if (condition) {
  26.  
  27. some code
  28.  
  29. }[/CODE]
  30.  
  31. If the [I]condition[/I] is [ICODE]true[/ICODE], then the [I]statements[/I] within are executed. Otherwise, the [I]statements[/I] are skipped and the program continues on.
  32.  
  33. [CODE]if (flip == 1) {
  34.  
  35. std::cout << "Heads\n";
  36.  
  37. }[/CODE]
  38.  
  39. The [ICODE]if[/ICODE] keyword is followed by a set of parentheses [ICODE]()[/ICODE]. Inside the parentheses [ICODE]()[/ICODE], a condition is provided that evaluates to [ICODE]true[/ICODE] or [ICODE]false[/ICODE]:
  40.  
  41. [LIST]
  42. [*]If the condition evaluates to true, the code inside the curly braces {} executes.
  43. [*]If the condition evaluates to false, the code won’t execute.
  44. [/LIST]
  45. So in the code above, if [ICODE]flip[/ICODE] is equal to 1, the program outputs “Heads”; if it does not, then nothing happens and the program continues.
  46.  
  47. [SIZE=7][B]Relational Operators[/B][/SIZE]
  48. When writing conditional statements, sometimes we need to use different types of operators to compare values. These operators are called [I]relational operators[/I].
  49. To have a condition, we need relational operators:
  50. [LIST]
  51. [*][ICODE]==[/ICODE] equal to
  52. [*][ICODE]!=[/ICODE] not equal to
  53. [*][ICODE]>[/ICODE] greater than
  54. [*][ICODE]<[/ICODE] less than
  55. [*][ICODE]>=[/ICODE] greater than or equal to
  56. [*][ICODE]<=[/ICODE] less than or equal to
  57. [/LIST]
  58. [SIZE=7][B]Else Clause[/B][/SIZE]
  59. We can also add an [ICODE]else[/ICODE] clause to an [ICODE]if[/ICODE] statement to provide code that will only be executed if the condition is [ICODE]false[/ICODE]. Here’s a form of an [ICODE]if[/ICODE] statement that includes an [ICODE]else[/ICODE] clause:
  60.  
  61. [CODE]if (condition) {
  62.  
  63. do something
  64.  
  65. } else {
  66.  
  67. do something else
  68.  
  69. }[/CODE]
  70. [LIST]
  71. [*]If [I]condition[/I] is [ICODE]true[/ICODE], [I]statement1[/I] is executed. Then the program [I]skips[/I] [I]statement2[/I] and executes any code statements following the [ICODE]if[/ICODE]/[ICODE]else[/ICODE] clause.
  72. [*]If [I]condition[/I] is [ICODE]false[/ICODE], [I]statement1[/I] is [I]skipped[/I] and [I]statement2[/I] is executed. After [I]statement2[/I] completes, the program executes any code statements following the [ICODE]if[/ICODE]/[ICODE]else[/ICODE] clause.
  73. [/LIST]
  74. [CODE]if (coin == 1) {
  75.  
  76. std::cout << "Heads\n";
  77.  
  78. }
  79. else {
  80.  
  81. std::cout << "Tails\n";
  82.  
  83. }[/CODE]
  84.  
  85. So in the code above, if [ICODE]coin[/ICODE] is equal to 1, the program outputs “Heads”; if it does not, then it outputs “Tails”.
  86.  
  87. [B]Note:[/B] It’s either or — only one of them will execute!
  88.  
  89. [SIZE=7][B]Else If[/B][/SIZE]
  90. So what happens if you want more than two possible outcomes?
  91. This is where [ICODE]else if[/ICODE] comes in!
  92. [CODE]if (condition) {
  93.  
  94. some code
  95.  
  96. } else if (condition) {
  97.  
  98. some code
  99.  
  100. } else {
  101.  
  102. some code
  103.  
  104. }[/CODE]
  105. The [ICODE]else if[/ICODE] statement always comes after the [ICODE]if[/ICODE] statement and before the [ICODE]else[/ICODE] statement. The [ICODE]else if[/ICODE] statement also takes a condition.
  106.  
  107. And you can have more than one of them! Here’s an example with three of them:
  108. [CODE=cpp]if (grade == 9) {
  109.  
  110. std::cout << "Freshman\n";
  111.  
  112. }
  113. else if (grade == 10) {
  114.  
  115. std::cout << "Sophomore\n";
  116.  
  117. }
  118. else if (grade == 11) {
  119.  
  120. std::cout << "Junior\n";
  121.  
  122. }
  123. else if (grade == 12) {
  124.  
  125. std::cout << "Senior\n";
  126.  
  127. }
  128. else {
  129.  
  130. std::cout << "Super Senior\n";
  131.  
  132. }[/CODE]
  133.  
  134. [SIZE=7][B]Switch Statement[/B][/SIZE]
  135. Now that we know how [ICODE]if[/ICODE], [ICODE]else if[/ICODE], [ICODE]else[/ICODE] work, we can write programs that have multiple outcomes. Programs with multiple outcomes are so common that C++ provides a special statement for it… the [ICODE]switch[/ICODE] statement!
  136. A [ICODE]switch[/ICODE] statement provides an alternative syntax that is easier to read and write. However, you are going to find these less frequently than [ICODE]if[/ICODE], [ICODE]else if[/ICODE], [ICODE]else[/ICODE] in the wild.
  137. A [ICODE]switch[/ICODE] statement looks like this:
  138. [CODE=cpp]switch (grade) {
  139.  
  140. case 9:
  141. std::cout << "Freshman\n";
  142. break;
  143. case 10:
  144. std::cout << "Sophomore\n";
  145. break;
  146. case 11:
  147. std::cout << "Junior\n";
  148. break;
  149. case 12:
  150. std::cout << "Senior\n";
  151. break;
  152. default:
  153. std::cout << "Invalid\n";
  154. break;
  155.  
  156. }[/CODE]
  157. [LIST]
  158. [*]The [ICODE]switch[/ICODE] keyword initiates the statement and is followed by [ICODE]()[/ICODE], which contains the value that each case will compare. In the example, the value or expression of the switch statement is [ICODE]grade[/ICODE]. One restriction on this expression is that it must evaluate to an integral type ([ICODE]int[/ICODE], [ICODE]char[/ICODE], [ICODE]short[/ICODE], [ICODE]long[/ICODE], [ICODE]long long[/ICODE], or [ICODE]enum[/ICODE]).
  159. [*]Inside the block, [ICODE]{}[/ICODE], there are multiple cases.
  160. [*]The [ICODE]case[/ICODE] keyword checks if the expression matches the specified value that comes after it. The value following the first case is [ICODE]9[/ICODE]. If the value of [ICODE]grade[/ICODE] is equal to [ICODE]9[/ICODE], then the code that follows the [ICODE]:[/ICODE] would run.
  161. [*]The [ICODE]break[/ICODE] keyword tells the computer to exit the block and not execute any more code or check any other cases inside the code block.
  162. [*]At the end of each switch statement, there is a [ICODE]default[/ICODE] statement. If none of the cases are [ICODE]true[/ICODE], then the code in the [ICODE]default[/ICODE] statement will run. It’s essentially the [ICODE]else[/ICODE] part.
  163. [/LIST]
  164. In the code above, suppose [ICODE]grade[/ICODE] is equal to [ICODE]10[/ICODE], then the output would be “Sophomore”.
  165.  
  166. [B]Note:[/B] Without the [ICODE]break[/ICODE] keyword at the end of each case, the program would execute the code for the first matching case and [I]all[/I] subsequent cases, including the [ICODE]default[/ICODE] code. This behavior is different from [ICODE]if[/ICODE] / [ICODE]else[/ICODE] conditional statements which execute only one block of code.
  167.  
  168. [SIZE=7][B]Review[/B][/SIZE]
  169. Congratulations! 🙌
  170. Here are some of the major concepts:
  171. [LIST]
  172. [*]An [ICODE]if[/ICODE] statement checks a condition and will execute a task if that condition evaluates to true.
  173. [*][ICODE]if[/ICODE] / [ICODE]else[/ICODE] statements make binary decisions and execute different code blocks based on a provided condition.
  174. [*]We can add more conditions using [ICODE]else if[/ICODE] statements.
  175. [*]Relational operators, including [ICODE]<[/ICODE], [ICODE]>[/ICODE], [ICODE]<=[/ICODE], [ICODE]>=[/ICODE], [ICODE]==[/ICODE], and [ICODE]!=[/ICODE] can compare two values.
  176. [*]A [ICODE]switch[/ICODE] statement can be used to simplify the process of writing multiple [ICODE]else if[/ICODE] statements. The [ICODE]break[/ICODE] keyword stops the remaining cases from being checked and executed in a switch statement.
  177. [/LIST]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement