Posts

A Student's C Book: 1.4. Control Flow

Level 1. Introduction to C 1.4. Control Flow Branching is a strategy to make perfect plans in the imperfect or incomplete world.  The control flow of a program execution is the path of sequential instructions the computer executes while running the program. So far, you have seen a linear and non-branching control flow where the computer executes each instruction in the main() function. However, we can change this behavior and make the computer sometimes skip some instructions or decide between multiple execution paths depending on some conditions. Before moving to the next section, I would like to mention one thing about a new data type you will need to keep in mind for this tutorial. The new data type I would like to introduce is the boolean or bool  type. The boolean type is used to represent logical truth values, such as true  and false . All conditional expressions (e.g., 3 < 5 , 4 > 4 , and so on) in C are evaluated to one of these boolean values. ...

A Student's C Book: 1.3. Functions

Level 1. Introduction to C 1.3. Functions I receive Inputs, You receive Output What is a function? Roughly speaking, in the mathematical sense, it is a mapping from some input space to some output space such that no two different output values correspond to the same input value. In programming, we also call inputs the arguments given to a function. In fact, we even make a slight distinction between the arguments and the parameters of the functions. We call variable inputs, used in the output expression of the function, the arguments, and exact valued inputs, used in the final computation of the function's output, the arguments. Let's now recall a simple mathematical function as an example: $$ f(x) = x + 1 $$ A computation function (i.e., a function in programming) is not too much different than a mathematical one. It has all the components that the mathematical one has: A name (it is named f  in our example) Parameters or input variables (it is just a single paramet...

A Student's C Book: 1.2. Basics

Image
Level 1. Introduction to C 1.2. Basics Your computer can memorize things One of the things that you can make your computer remember is integer numbers. As one would do in math, you can assign numbers to variables (usually to reuse them later on when needed). Roughly speaking, this is how computers remember things. You use a variable to make a computer remember a number. You may ask why there needs to be a variable to remember a number. Well, let's imagine that you were to be given multiple facts to memorize. For simplicity, I'll only assume two of them: Alice is 3 years old, and Bob is 5 years old. Now, you are asked about Bob's age. If you didn't associate 3 and 5 with the names Alice and Bob, respectively, then you wouldn't be able to answer this question with certainty. So, your mind probably did something similar to what we do in math -- use variables to represent those numbers, that is, Alice = 3  and Bob = 5 . That's why you were able to answer...