Programming Lesson Plans
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.8 KiB

3 years ago
  1. # Operators
  2. WIthin basic programming, there are a couple of operators that are almost
  3. universal amongst all C-like programming languages. These are the following:
  4. 1. \+ (adding)
  5. 2. \- (subtraction)
  6. 3. \* (multiplication)
  7. 4. \/ (division)
  8. 5. \% (modulo)
  9. 6. && (and)
  10. 7. || (or)
  11. 8. \! (not)
  12. Think of these operators as being ways by which you can mutate the state of
  13. the program to reflect how much information you have at the moment.
  14. # Conditional flow
  15. If statements have the following syntax in C++:
  16. ```cpp
  17. if (condition) {
  18. // resulting code
  19. }
  20. ```
  21. Usually, the condition is a boolean statement that you pass in that evaluates to
  22. true or false. However, you will find that if you write the following:
  23. ```cpp
  24. if (1) {
  25. //code
  26. }
  27. ```
  28. The inside code will always run. This is because non-null and non-zero values
  29. get evaluated to true by default (this is convenient as a shortcut at times)
  30. while nulls and zeroes are false.
  31. Conditionals are often used to handle edge cases in a program or a base case
  32. in recursion, in which you execute those extra steps only if the desired case
  33. applies.
  34. # Looping
  35. ## For loop
  36. There are two different syntaxes in a for loop. The first is as follows:
  37. ```cpp
  38. for (int i = 0; i < (number); ++i)
  39. ```
  40. (the second is a for-each loop, which we will get to once we talk about
  41. sets and maps)
  42. Inside this for loop, the variable i will be brought to scope, from which
  43. you can use it in whatever way you wish.
  44. This is particularly useful for when you need a sequence of numbers that
  45. represent indices of an array (from 0 to N-1) or when there are a certain
  46. number of test cases.
  47. ### Warning (Variable Shadowing)
  48. There are many instances when programmers tend to write the following when
  49. writing nested for loops:
  50. ```cpp
  51. for (int i = 0; i < N; ++i) {
  52. for (int i = 0; i < M; ++i) {
  53. // code
  54. }
  55. }
  56. ```
  57. Since the inner i has the same name as the outermost i, it *shadows* the
  58. other value, leading to behavior that is incredibly difficult to debug.
  59. In order to warn us when we do such things, we add the -Wshadow flag in
  60. our build command.
  61. For the above, you probably want to do this instead:
  62. ```cpp
  63. for (int i = 0; i < N; ++i) {
  64. for (int j = 0; j < M; ++j) {
  65. // code
  66. }
  67. }
  68. ```
  69. This prevents the inner and outer for loop variables from shadowing
  70. each other.
  71. ## While loop
  72. The while loop syntax is as follows:
  73. ```cpp
  74. while (condition) {
  75. //code
  76. }
  77. ```
  78. Although a for loop will be used far more often than a while loop, it is
  79. important to still understand its semantics. A while loop will check
  80. **at the beginning of each iteration** as to whether or not its condition
  81. is true, and will terminate the loop if its condition is false.
  82. # Termination statements
  83. Sometimes, you need a way to exit a loop, an individual loop iteration, or
  84. possibly an entire program if certain constraints apply.
  85. For instance, you might want to check for a very trivial case in a program,
  86. in which you can do the following:
  87. ```cpp
  88. if (trivial_case) {
  89. cout << endl;
  90. return 0;
  91. }
  92. ```
  93. Within this code, we are flushing all of the output we have to the console,
  94. and then returning with an error code. 0 indicates no error, while positive
  95. integers correspond with specific types of errors.
  96. In a loop, if you wish to terminate the current iteration of the loop, you can
  97. do the following:
  98. ```cpp
  99. for (loop statement) {
  100. // code block 1
  101. if (condition) {
  102. continue;
  103. }
  104. // code block 2
  105. }
  106. ```
  107. If the statement is true in a certain iteration, then code block 1 will be
  108. executed, but code block 2 will not, as the continue directive immediately
  109. sends the execution of the program to the next iteration.
  110. This is useful if, for instance, you have an array of integers, some of which
  111. you need to do a very trivial manipulation on. You can then use the continue
  112. statement to go to the next iteration once this manipulation is done. This is
  113. better in contrast to writing a bunch of nested if/else statements and makes
  114. your code slightly more neat.
  115. The **break** statement will terminate a loop entirely. For instance, let's
  116. say you are implementing a program that uses a simple linear search to find
  117. the first element in an array that satisfies a certain constraint. We could
  118. do the following:
  119. ```cpp
  120. // arr is a vector.
  121. int ans = arr.size();
  122. for (int i = 0; i < arr.size(); ++i) {
  123. if (arr[i] satisfies a constraint) {
  124. ans = i;
  125. break;
  126. }
  127. }
  128. ```
  129. In this case, we want to use the **break** statement, as we have now found our
  130. answer.
  131. Of course, it is possible to eliminate the break statement altogether
  132. by running the for loop backwards:
  133. ```cpp
  134. int ans = arr.size();
  135. for (int i = (int) arr.size()-1; i >= 0; --i) {
  136. if (arr[i] satisfies a constraint) {
  137. ans = i;
  138. }
  139. }
  140. ```
  141. As we go from last to the first index, ans will be updated to the index that is
  142. closest to the front. This goes to show you that you should be thinking heavily
  143. not just about when you have for loops, but in what **direction** they should go.