Advertisement
Guest User

cop.ch4.read_9.23.19_4:53

a guest
Sep 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. https://azeembooks.com/wp-content/uploads/2019/06/Starting-Out-With-Cpp-From-Control-Structures-Through-Objects-8th-Edition.pdf
  2.  
  3. 9.23.19
  4.  
  5. LO1: Relational Operators
  6. --------------------------------------------------------------
  7. >, <, >=, <=, ==, !=
  8.  
  9. x > y relational expression, boolean expression, is x greater than y? true or false?
  10. true is not always 1, and false is not always 0
  11.  
  12. // This program displays the values of true and false states.
  13. #include <iostream>
  14. using namespace std;
  15.  
  16. int main()
  17. {
  18. bool trueValue, falseValue;
  19. int x = 5, y = 10;
  20.  
  21. trueValue = x < y;
  22. falseValue = y == x;
  23.  
  24. cout << "True is " << trueValue << endl;
  25. cout << "False is " << falseValue << endl;
  26. return 0;
  27. }
  28. OUTPUT
  29. True is 1
  30. False is 0
  31.  
  32. z = x < y z is assigned 0, x is not less than y
  33.  
  34.  
  35. LO2: The if Statement
  36. --------------------------------------------------------------
  37.  
  38. #include <iostream>
  39.  
  40. int main() {
  41. int temp = 0;
  42.  
  43. std::cout << "Enter temperature: ";
  44. std::cin >> temp;
  45. if (temp <= 60)
  46. std::cout << "Grab Coat!\n"; //if true display this, if false keep running program
  47. std::cout << "Eat breakfast\n";
  48. std::cout << "Grab keys\n";
  49. std::cout << "Leave House\n";
  50.  
  51. return 0;
  52. }
  53.  
  54. Is it cold outside?
  55. Yes
  56. Wear a coat. Continue running program -->
  57. No
  58. Continue running programm -->
  59.  
  60.  
  61. LO3: Expanding the if Statement
  62. --------------------------------------------------------------
  63.  
  64. #include <iostream>
  65.  
  66. int main() {
  67. int temp = 0;
  68.  
  69. std::cout << "Enter temperature: ";
  70. std::cin >> temp;
  71. if (temp <= 60) {
  72. std::cout << "Grab Coat!\n"; //if true display this, if false keep running program
  73. std::cout << "Not the Dolphins coat!\n"; //also display this
  74. } //don't forget the braces!!!
  75. std::cout << "Eat breakfast\n";
  76. std::cout << "Grab keys\n";
  77. std::cout << "Leave House\n";
  78.  
  79. return 0;
  80. }
  81.  
  82. LO4: The if/else Statement
  83. --------------------------------------------------------------
  84.  
  85. #include <iostream>
  86.  
  87. int main() {
  88. int temp = 0;
  89.  
  90. std::cout << "Enter temperature: ";
  91. std::cin >> temp;
  92. if (temp <= 60) {
  93. std::cout << "Grab Coat!\n"; //if true display this, if false keep running program
  94. std::cout << "Not the Dolphins coat!\n"; //also display this
  95. } else {
  96. std::cout << "Put on a regular shirt"; //if above is false, then display this
  97. }
  98. std::cout << "Eat breakfast\n";
  99. std::cout << "Grab keys\n";
  100. std::cout << "Leave House\n";
  101.  
  102. return 0;
  103. }
  104.  
  105. Is it cold outside?
  106. Yes
  107. Grab a coat, not the dolphins coat. Continue running program -->
  108. No
  109. Put on a regular shirt. Continue running programm -->
  110.  
  111. LO5: Nested if Statements
  112. --------------------------------------------------------------
  113.  
  114. #include <iostream>
  115.  
  116. int main() {
  117. int temp = 0;
  118.  
  119. std::cout << "Enter temperature: ";
  120. std::cin >> temp;
  121. if (temp <= 60) {
  122. std::cout << "Grab Coat!\n"; //if true display this, if false keep running program
  123. std::cout << "Not the Dolphins coat!\n"; //also display this
  124. } else {
  125. if (temp > 80) {
  126. std::cout << "Put on a short-sleeved shirt\n"; //if above is false, then display this
  127. } else {
  128. std::cout << "Put on a regular shirt\n"; //if above is false, then display this
  129. }
  130. }
  131. std::cout << "Eat breakfast\n";
  132. std::cout << "Grab keys\n";
  133. std::cout << "Leave House\n";
  134.  
  135. return 0;
  136. }
  137.  
  138. Is it cold outside?
  139. Yes
  140. Grab a coat, not the dolphins coat. Continue running program -->
  141. Is it hot?
  142. Yes
  143. Put on a short-sleeved shirt. Continue running programm -->
  144. No
  145. Put on a regular shirt. Continue running programm -->
  146.  
  147. LO6: The if/else if Statement
  148. --------------------------------------------------------------
  149.  
  150. #include <iostream>
  151.  
  152. int main() {
  153. int temp = 0;
  154.  
  155. std::cout << "Enter temperature: ";
  156. std::cin >> temp;
  157. if (temp <= 60) {
  158. std::cout << "Grab Coat!\n"; //if true display this, if false keep running program
  159. std::cout << "Not the Dolphins coat!\n"; //also display this
  160. } else if (temp > 80) {
  161. std::cout << "Put on a short-sleeved shirt\n"; //if above is false, then display this
  162. } else {
  163. std::cout << "Put on a regular shirt\n"; //if above is false, then display this
  164. }
  165.  
  166. std::cout << "Eat breakfast\n";
  167. std::cout << "Grab keys\n";
  168. std::cout << "Leave House\n";
  169.  
  170. return 0;
  171. }
  172.  
  173. LO7: Flags
  174. --------------------------------------------------------------
  175.  
  176.  
  177. LO8: Logical Operators
  178. --------------------------------------------------------------
  179.  
  180.  
  181. LO9: Checking Numeric Ranges with Logical Operators
  182. --------------------------------------------------------------
  183.  
  184.  
  185. LO10: Menus
  186. --------------------------------------------------------------
  187.  
  188.  
  189. LO11: Focus on Software Engineering: Validating User Input
  190. --------------------------------------------------------------
  191.  
  192.  
  193. LO12: Comparing Characters and Strings
  194. --------------------------------------------------------------
  195.  
  196.  
  197. LO13: The Conditional Operator
  198. --------------------------------------------------------------
  199.  
  200.  
  201. LO14: The switch Statement
  202. --------------------------------------------------------------
  203.  
  204. LO15: More About Blocks and Variable Scope
  205. --------------------------------------------------------------
  206.  
  207.  
  208. Checkpoint [LO1]
  209. --------------------------------------------------------------
  210. 1.
  211. A) T
  212. B) T
  213. C) F
  214. D) T
  215. E) T
  216. F) F
  217. G) T
  218.  
  219. 2.
  220. A) F
  221. B) F
  222. C) T
  223.  
  224. 3.
  225. A) T
  226. B) F
  227. C) F
  228.  
  229. 4.
  230. OUTPUT:
  231. 0
  232. 0
  233. 1
  234. 0
  235.  
  236. Checkpoint [LO2]
  237. --------------------------------------------------------------
  238. 5.
  239. if (x == 20)
  240. y = 0;
  241.  
  242. 6.
  243. if (price > 500)
  244. discountRate = 0.2;
  245.  
  246. 7.
  247. if (hours > 40)
  248. payrate *= 1.5;
  249.  
  250. 8. TRUE
  251.  
  252. 9. FALSE
  253. if (calls = 20) // assigns value 20 to calls!!!
  254. rate *=0.5;
  255.  
  256. Checkpoint [LO3]
  257. --------------------------------------------------------------
  258. 10.
  259. if (sales > 50000) {
  260. commissionRate = 0.25;
  261. bonus = 2.50;
  262. }
  263.  
  264. 11.
  265. It's missing the braces {}
  266.  
  267. Checkpoint [LO4]
  268. --------------------------------------------------------------
  269. 12.
  270. FALSE: cout's should be in the same order in both statements for them to display the same output ase they both are the same, x > y is the same as saying y <= x
  271.  
  272. 13.
  273. if (y == 100) {
  274. x = 1;
  275. } else {
  276. x = 0;
  277.  
  278. 14.
  279. if (sales >= 50000.00) {
  280. comissionRate = 0.20;
  281. } else {
  282. comissionRate = 0.10;
  283. }
  284.  
  285. Checkpoint [LO5]
  286. --------------------------------------------------------------
  287. 15. OUTPUT:
  288. 0: Zero
  289.  
  290. 15: Zero
  291. Ten
  292.  
  293. 30: Zero
  294. Ten
  295. Twenty
  296.  
  297. -1: [nothing, program doesn't finish running, because there's no else statement]
  298.  
  299. Checkpoint [LO6]
  300. --------------------------------------------------------------
  301. 16.
  302. if funny is not 1, then funny = 0, serious = 0 [TRUE] [funny = 7]
  303. else if funny is 2, then funny = 10, serious = 10 [FALSE]
  304. else funny = 1, serous = 1; [NOT AVAILABLE]
  305. OUTPUT:
  306. 0 0
  307.  
  308. 17.
  309. 1 1
  310. 3 2
  311. 4 2
  312. 5 3
  313. 10 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement