Integer Overflow, String Comparisons, Range-Based For Loops, Nested Loops
Today we went over the Branches CE (and take 2 is now up), then talked about a lot of important stuff:
1) Integer overflow and underflow. Every time you do a +, - or * there is a possibility your integer (or char or short or long long or uint64_t or or or) will overflow, which means it exceeds the limits of the number and wraps around to the lowest possible number. If you've ever played a video game in which you got too many points or something and it set your score to negative, that was integer overflow in action.
Contrawise, if you ever drop below the lowest possible number you will underflow and wrap around to the highest number. This is especially likely to happen when you use unsigned integers, which should be avoided in normal circumstances, even if the values you're going to hold can't be negative (like length or quantities or age) because if you ever difference them, it can easily underflow.
2) String comparisons do something called "lexicographical ordering" which is how you alphabetize books by title in a library - you compare the first letters first, and put the A's first, the Z's last, etc., and if there is a tie on the first letter, you move on to the second letter. You can compare a string using the normal less than and greater than operators.
There is a twist, and the twist is that what is actually being compared are the ASCII values (if you want to view the ASCII values, type 'man ascii' on the server), so there's some weirdness, like '=' being less than 'A' which is less than 'a'.
It's pretty common to do case-insensitive comparisons, so one approach is to uppercaseify a string and then compare the all-caps version of the strings, so that apple == APPLE == appLe == Apple.
3) To uppercaseify a string, you could do a normal for loop, or you could use the learning point for today, which is the range-based for loop (sometimes called a for each loop):
for (char &c : s) c = toupper(c);
In English, the above means, "For each character c in the string s, set c to be the uppercase version of c." It's simpler, safer, and more compact than doing a full for loop.
It requires C++11 or better.
4) Finally, we talked about nested for loops (putting a loop inside of a loop). The main thing to watch out for is to make sure that you don't use i for both of the loop counters! Use i for the outer loop, j for the nested loop, k for the doubly nested loop, and so forth, by convention.
Other Videos By Bill Kerney
2021-09-08 | Functions Part II |
2021-09-08 | Arguments + Invalid/Valid/Sound |
2021-09-07 | Voroni |
2021-09-07 | CSG, Sprinting, Water (Old and New) |
2021-09-07 | Constexpr, Permutations, Combinations, Binomial Theorem, and Infinite Sets |
2021-09-03 | Functions in C++ Part I |
2021-09-03 | Five Theories of Truth |
2021-09-02 | UE4 Blueprints |
2021-09-02 | Set Theory I |
2021-09-02 | The Laser Zombies |
2021-09-01 | Integer Overflow, String Comparisons, Range-Based For Loops, Nested Loops |
2021-09-01 | Truth Part 1 |
2021-09-01 | Landscapes Part I |
2021-09-01 | Project Updates |
2021-08-31 | Hash Tables + Counting |
2021-08-30 | Coding with Loops |
2021-08-30 | How to Make an Argument and Counterargument - Piracy: For and Against |
2021-08-27 | For and While Loops |
2021-08-27 | Examining Sources |
2021-08-27 | Gaming Psychology and Lerping Materials |
2021-08-27 | Hash Tables |