Hash Tables
A hash table is an efficient way to store, modify and retrieve data, especially when a large amount of data is involved.
A hash table stores elements in a key-value pair where the key is an integer unique to the hash table for indexing the values and the value is the data associated with the key. The integer value is generated by a hash function.
So, in a way, it is like an array that links a key to a specific data value.
A hash function takes the key and generates an integer of fixed size which will then be associated with that key. – used in cryptography, security, etc.
You can think of a hash table as a form of dictionary. The name is the key and the entry is a value.
A hash table can be useful when trying to keep a database of users in an organisation.
Control Flow
The control flow of a program is the order in which a program executes. Types of flow include:
Conditionals
It is also known as Decision Control. It means a program will do one multiple alternatives based on if a condition resolves to true or false. For example, when making coffee, you can decide to add milk if you like it, else you can leave it.
Some conditional statements you can find in different programming languages include:
Switch statements
A type of conditional statement – similar to if/else. It is present in programming languages like JavaScript, Java, C, C++, C#, etc.
Switch statements use keywords like switch, case, break, etc.
A typical switch statement looks somewhat like this:
switch( expression ) {
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;}
Loops
Remember our code for making coffee from earlier? Now imagine we want to make cups of coffee for all 90 students in a class. How would we go about it?
We could rewrite our code 90 times, but that wouldn’t be efficient – as computers are designed to be. So we use something called a loop instead.
A loop is a programming feature that lets us repeat a sequence of instructions until a condition is met – the break condition.
In the example earlier, that condition is the number of students in the class – 90.
If a break condition is not specified, the loop will continue running for as long as your program is active. This is called an infinite loop.
Types of Loops
Most programming languages have at least two types of loops, both identified by their keywords – FOR loops and WHILE loops.
While both loops can be used interchangeably, FOR loops are more commonly used when we know the actual number of times we want the loop to run. (In our coffee example, 90 times).
The structure is usually in the format:
for (initialize; condition; increment/decrement)
{
doStuff()}
Breakdown of a FOR Loop
A FOR loop consists of a header portion and a body portion with the header typically consisting of 3 parts and the body containing the code to be executed while our condition remains true.
In the first part of the header, we initialize our loop variable.
This is then checked against a condition we declare in the second part. If our condition is met, the code in the body of our loop runs
Our loop variable is then increased or decreased by a value we declare in the third part of the loop header, checked against the loop condition and if it’s met, the loop runs again.
A WHILE loop
A while loop runs for as long as a conditional statement passed into it remains true. It is usually used when you do not know how many times your loop will run.
For example, instead of making coffee for 90 people, imagine we were making coffee till we run out of boiled water and we do not know how much water it would take to make a cup of coffee or be wasted in between
A while loop would be helpful in this case.
while (boiledWater) {
makeCoffee()
}
Usually, there’d be a way to update the boiledWater variable in our makeCoffe function. So, whenever we makeCoffee, the boiledWater variable is updated.
It becomes falsy when we run out of boiledWater and the while loop stops running
Recursion
Imagine reading through a wikipedia entry about an historical event, and then you come across another interesting event, you click on that, find another and keep clicking until you no longer find anything that interests you, and then stop.
A recursion is a program that calls itself until it gets to a base condition. A recursive function can call itself directly or indirectly.
A direct recursive function calls itself, within itself, while an indirect recursive function calls other functions that eventually calls the original function.
It is important to have a base condition else, your function continues to call itself infinitely.
Usually, a recursion problem can be solved with loops and vice versa.
Practical Recursion Example
Let’s try to create a recursive function that adds integers from 1 to a given number. (This code is in JavaScript)
function sumRange(range) {
return range + sumRange(range – 1)
}
The code above will run infinitely or throw an error as there is no base case, so we should add one.
function sumRange(range) {
if (range === 1) {
return 1
}
return range + sumRange(range – 1)
}
This works, but for only positive numbers only. The same error thrown in the first code will be seen here if 0 or a negative number is shown.
function sumRange(range) {
if (range <= 0) {
return -1
}
if (range === 1) {
return 1
}
return range + sumRange(range – 1)
}
With a base case and type checking, we now have a working recursive function to find the sum of numbers up to a range.
Recursive functions are used in a lot of sorting algorithms.