CS50x - Lecture 1 - Part 2.
conditionals, loops, and operators
Last week I went over the first part of CS50's Lecture 1, taking a look at linux commands, variables, and data types in C. I went over my very first program in C, hello.c, and included quite a bit of syntax and instructions for making it... and only it. I'm still figuring out the format I want these posts to be, but I'd like to talk more about ideas and concepts, putting them into my own words, rather than syntax and instruction. Let's try something a little different.
Conditionals
A conditional is exactly what it sounds like. It tells the computer to do something when something is true. For example: if you want to check if a problem has the correct solution, or if a number (usually a variable) is bigger or smaller than another number, you'd use a conditional. In C, conditionals are wrapped in brackets ( ), and notice the first word in my previous example - if. So you'd tell the computer to do something if (something is true). Then, after you can add more specific conditions with else if, and usually to cap it off a general condition of else that catches any other value.
You can also combine conditionals, that is to say, do something if X is true AND/OR Y is true - && and || in C respectively. Alternatively, you can preface a condition with a bang ! which is the logical not operator, and flips the condition around, so != means not true, whereas == means true.
Loops
These go hand in hand with conditionals, in fact, you can't make a loop without a condition. As the name implies, a loop just tells the computer to do something over and over again. There are two types of loops: while and for loops.
A while loop tells the computer to repeat the instructions while something is true. In C, this looks like: while (whatever is in the brackets is true). Usually you would initialize a counter variable above a while loop like int i = 0; then tell the computer to repeat something while (i < n number of times - let's say 3), then inside the loop after you do what you want to, you'd add 1 to your i variable. In this example, the loop would run 3 times.
- Why 3 times if you're saying while i is less than 3? Remember that computers always count from 0, so the loop would run when:
- i = 0
- i = 1
- i = 2
- So, while i is still less than 3, we ran 0, 1, 2, which is 3 times.
A for loop is slightly different in that it tells the computer to do something a certain number of times. They can usually be interchanged, and programmers tend to use whatever they're more comfortable with. But the biggest difference between the two is that for loops run the amount of times you tell them to, whereas while loops run until something happens.
In C you'd usually write a for loop in one line (that I've learned), which is another consideration for reading, styling, and understanding your code. It would look something like this: for (initialize variable(s); tell the loop how many times to run; do something after the loop is done (like i++)).
for (int i = 0; i < 3; i++)
{
do_something( )
}
This for loop would do something 3 times.
Operators
Operators are quite simple. Like a calculator, they are mathematical operations that the computer knows how to do. Here's a list:
- + for addition
- - for subtraction
- * for multiplication
- / for division
- % for remainder. This one is quite new, and it took a couple minutes to wrap my head around, it's called a modulo operator.
The modulo operator gives you the remainder after you divide two numbers together, called the modulus. For example, if you divide 6 / 2 = 3. Which is a whole number. So, 6 % 2 = 0, because there is no remainder in the operation. But 5 % 2 = 1, because 2 completely fits into 5 twice and you're left with 1 remaining. This is great for finding even numbers among other things.
Abstraction
Arguably one of the most important ideas in computer science, abstraction is the process of simplifying code. It can be defined as "the process of considering something independently of its associations" or "the process of removing something". This means, in effect, abstracting code, or more specifically, abstracting a function is removing that code from the main code, and writing an independent, smaller piece of code to solve a smaller problem.
This is huge in cloud computing, CI/CD (continuous integration and continuous deployment), and professional environments where potentially hundreds of people are all working on the same project. So instead of all working on top of each other and potentially duplicating or ruining their code, maybe one person is working on a button over here, while another person is developing a login screen over there, and yet a third group is working on security somewhere else. Then, you build a program from all its separate but connected parts.
I'm still working to get into the habit of thinking and problem-solving through abstraction. To solve computer problems, and more importantly real-world problems, a programmer has to really break down or abstract every problem into smaller and smaller pieces, then write an algorithm - specific instructions that the computer can follow.
There's so much more interesting stuff in this lecture. I didn't even talk about integer overflow, floating-point imprecision, or how computers are literally running out of time! I highly recommend watching the lecture yourself if you get a chance. But alas, my time is also running out, so goodbye for now.