Shortcut to seniority
Home
Go to main page
Section level: Junior
A journey into the programming realm
Section level: Intermediate
The point of no return
Section level: Senior
Leaping into the unknown
Go to main page
A journey into the programming realm
The point of no return
Leaping into the unknown
Theory is not everything, and only reading about code will not make you a very good programmer, you still need (a lot of) practice. However, if this is your first interaction with code, you probably won't understand much from the code snippets.
Therefore, this chapter will provide to you a quick summary of the language syntax. As the code snippets are written in C++, we will talk about what this language has to offer. Keep in mind that most languages share the same functionality, but the syntax / language keywords may differ.
Hello world is usually the first code snippet a programmer is introduced to, and out of respect and tradition, we'll start with that. The utility of it is to familiarize the viewer with the code, by showing it how to display something on screen.
#include <iostream>
int main()
{
std::cout << "Hello World";
return 0;
}
Keep in mind that you are writing code. An empty program does not have any functionality. If you want to have functionality, you'll need to write it yourself, or to use what was written by others. The C++ languages comes together with the standard library (STL), which contains generic functionality that you could use. Let's take the code line by line.
On the first line, we include the iostream header. First of all, header is a file format, in which a C++ developer writes the functions that can be called (the functionality that can be used). I will explain a bit more later, but for now, that's all that matters. Furthermore, iostream comes from input/output stream, which is a file that contains logic that allow us to interact with the console, so we can read from the keyboard and write to the display. If we do not include such functionality, our application will not have access to that logic.
The next line is a function, called main, that returns a value of type int. We will talk about types and functions a bit later on, but what you need to know is that this function is required in all C++ applications. This is the entry point of our software, the first function that is called, and once the code reaches the end of this function, the application will end. The brackets that surround the code tells us where the function starts and where it ends.
Afterwards, we have std::cout. What it does, is that it calls the cout object, from the std namespace. We haven't talked about namespaces yet, but basically we previously loaded all the stream related functions via the include line, and now we want to use the cout object from it. cout comes from console out and means that we want to write something to the console. And what we want to write? The string "Hello world".
In the next line, we see return 0. Return is a keyword used to specify that we will leave the function that we're in, and main is a function that expects the user to return an error code. The error code for success is 0 in this case.
Note: This is the only snippet in this book that contains the include line and the std prefix on a function call. For the remaining ones, i've removed them to improve readability.
A variable is basically a location in memory that stores a value, that is identifieable by a name, and that can be called to retrieve that value and use in the application.
In other words, we tell our software to keep some space in memory for a variable of a given type. The type is required because each data type has a memory space required, so declaring it tells the software how much memory to store and how to read the data from inside that memory location.
Let's declare a data of type int, under the name bookIdentifier, and assign it the value 555.
int bookIdentifier;
bookIdentifier = 555;
Operators are language keywords that allow us to interact with our variables.
We already discussed above about the assignment operator. Assignment operator is used to assign a value to a variable. If the variable already contains a value, it will be replaced with the new one.
int bookIdentifier = 12345;
Arithmetic operators allow us to do basic math operations with our variables.
int currentIndex = 1;
currentIndex = currentIndex + 1;
// the value of currentIndex is now 2
Compound assignments modify the value of our variable by performing an operation on it. This is similar to what we have in the previous example.
int currentIndex = 1;
currentIndex += 1; // same as currentIndex = currentIndex + 1;
currentIndex -= 6; // same as currentIndex = currentIndex - 6;
When we want to increment or decrement our variable by the value of 1, we can shorten it even further by using the increase/decrease operators.
int currentIndex = 1;
currentIndex--;
// same as currentIndex -= 1;
// same as currentIndex = currentIndex - 1;
currentIndex++;
// same as currentIndex += 1;
// same as currentIndex = currentIndex + 1;
The result of the comparison operators is a boolean type, meaning that the result is either true or false.
int a = 5;
int b = 5;
bool isSameValue = (a == b); // returns true
bool isDifferentValue = (a != b); // returns false
bool firstIsSmaller = (a < b); // returns false
bool firstIsSmallerOrEqual = (a <= b); // returns true
bool firstIsBigger = (a > b); // returns false
bool firstIsBiggerOrEqual = (a >= b); // returns true
We talked about logical operators prior to this chapter, and now we use them to evaluate our code.
int smallThreshold = 10;
int largeThreshold = 100;
int value = 51;
bool isSmallNumber = (value < smallThreshold);
// 51 < 10 returns false
bool isMediumNumber = (value > smallThreshold) && (value < largeThreshold);
// 51 > 10 returns true, 51 < 100 returns true, true && true returns true
bool isSmallOrMedium = (isSmallNumber || isMediumNumber);
// isSmallNumber is false, isMediumNumber is true, false || true returns true
bool isNotSmallOrMedium = !isSmallOrMedium;
// isSmallOrMedium is true, so the opposite is false
Functions are blocks of code that perform some logic. Each function has a return type (which can be void, meaning we don't return any value back), a name so we can call it when we want it to be executed, and optionally, parameters that can be sent to that function.
Let's see how we would declare a function that sums up two values received as input.
int sum(int first, int second)
{
return first + second;
}
When we talk about a decision to be made, it means we will pick and choose from two or more options, and that's the case here as well.
If is a keyword that receives a boolean value and evaluates it. If the value is true, we will execute the code within.
For example: if it's raining outside, i'll get an umbrella.
int number = 10;
int threshold = 20;
if (number < threshold)
{
cout << "The number is smaller than the threshold";
}
Else is the keyword pair of if, and cannot be standing by itself, which means we cannot have an else without an if prior.
For example: If it's raining outside, i'll go to the cinema. If not, i'll go to the park.
int number = 10;
int threshold = 20;
if (number < threshold)
{
cout << "The number is smaller than the threshold";
}
else
{
cout << "The number is bigger than the threshold";
}
Else if is a continuation of the if... decisional, allowing us to keep interrogating other entries in case the previous case fails.
For example: If it's raining outside, i'll go to the cinema. IF not - if it's summer, i'll go to the park, and if not, i'll go to the mall.
int smallThreshold = 10;
int mediumThreshold = 100;
int bigThreshold = 1000;
int value = 253;
if (value < smallThreshold)
{
cout << "The number is very small";
}
else if (value < mediumThreshold)
{
cout << "The number is small";
}
else if (value < bigThreshold)
{
cout << "The number is medium";
}
else
{
cout << "The number is big";
}
Loops allow us to repeat the same logic multiple times.
While receives a boolean and evaluates it. If it's evaluated to true, the code within will be executed, and then we will reevaluate the condition.
For example: I have a basket and I want to fill it with apples. Until my basket is full, take one apple and put it into the basket.
int myNumber = 10;
int currentNumber = 1;
while (currentNumber < myNumber)
{
currentNumber++;
}
Note that if the condition is evaluated to false, the code within will not be executed, not even once.
Do While does the same thing, but the condition is evaluated after the code within has been executed already. This means that we will execute the code inside at least once.
For example: I'm asked to guess a number between 1 to 10.
I first think about a number and ask if that's the one, and if it's not, I keep guessing another one.
int number = 5;
int guess = -1;
do
{
guess = GuessNumber();
}
while (guess != number);
For is used when we know in advance how many iterations we will loop through.
For example: I'm asked to count from 1 to 10.
for (int current = 0; current < 10; current++)
{
cout << "The current number is " << current;
}
Jump statements are used to change the flow of the current loop.
Break is used to break out from the current loop and continue the execution from outside of it.
For example: Count from 1 to 10, but stop at 5.
for (int i = 0; i < 10; ++i)
{
if (i == 5) break;
cout << "Current number is " << i;
}
Continue is used to skip the remaining lines for the current loop and continue with the next element (in for) or reevaluate the condition (in while).
For example: Count from 1 to 10, but skip the number 5.
for (int i = 0; i < 10; ++i)
{
if (i == 5) continue;
cout << "Current number is " << i;
}
An example of platform is the operating system, so we can include windows-related code only if we’re building the binary for windows, for example.
A pointer references a location in memory.
CPU cache is a small storage space that keeps the most frequently used data in order to reduce the accesses to the main memory.
Except for languages that contain garbage collector (which automatically frees memory that is no longer referenced).
NULL refer to the absence of a value, which means that it is not the same as having the value 0.
A Complete binary tree is a binary tree in which all nodes except leaves have two children, and the last level has all keys as left as possible.