Good weekend everyone! I really hope you are enjoying your free time!
In case you are feeling bored, feel free to deep dive into this new episode of Learn2Code.
Last time, we had a general overview on what iterations are and how they work. Today, our side quest in the world of coding brings us to deep dive on for loops and on its different configurations.
Let’s dive in!
Overview
This type of loop iterates through a collection of values, for example and array or a vector. It is based on the keyword for and it is used when the exact number of iterations to be made on a certain object is known a priori.
The simplest example you can make is the following. We declare an array with length 3 and we use a for loop to print every component of it.
int array[3] = { 1, 5, 7};
// For loop initialization
for(int i = 0; i < 3; i++) {
// This is the code block
std::cout << “Array value: “ << array[i] << std::endl;
} The console output would read:
Array value: 1
Array value: 5
Array value: 7Let’s see more in details how it works!
Consider supporting this newsletter by using one of my affiliate links. These are not sponsorships, just products I use everyday. Thank you!
Simple For Loop
Let’s take a look at the initialization of a for loop.
for(int i = 0; i < 3; i++)Other than the for keyword, we can see that the declaration is composed of three different parts:
int i = 0→ This first statement is executed once before the execution of the code block. It initializes the variable that will be used to count the number of iterations.i < 3→ The second statement is the stopping condition. The loop will continue to iterate while the variableiremains lower than a certain amount (3in this case).i++→ The last statement is run every time the code block is executed. It increases the value of theivariable.
The following image visually summarizes the previous point.
Inside the execution code, i can be used in different ways, like keeping track of iterations or accessing a certain position in an array.
// Accessing i-th position in the array
std::cout << “Array value: “ << array[i] << std::endl;Unlock the Secrets of Bitcoin Code with My Upcoming “How to C++” Guide!
Are you ready to dive deep into the world of Bitcoin programming? I’m crafting a concise, beginner-friendly “How to C++” guide – your essential toolkit for navigating and understanding Bitcoin’s codebase on your own. Think of it as a streamlined, action-packed version of Learn2Code!
Launching soon at just 21,000 sats, but here’s the deal: Pre-order now and get a massive 21% discount!
Don’t miss out – DM me today for pre-order details, more info, or to secure your spot!
Range-based For Loops
This particular type of for loop was introduced in C++11 and is used as a more readable equivalent to the traditional one. It operates over a range of values, such as all elements in a container. Here is the basic syntax:
for (declaration: range) {
// statements
}Here is a quick example. We declare a vector of integers, a container that holds N different values.
std::vector<int> v = {1, 10, 17, 25, 31};
// Iterating through vector
for (int i : v) {
std::cout << i << “ “;
}The variable i, declared as an int, is assigned one of the values inside the vector v at each iteration.
First iteration -> i = v[0] = 1 // Value at position 0
Second iteration -> i = v[1] = 10 // Value at position 1
.
.
N (fifth) iteration -> i = v[4] = 31 // Value at position N-1The loop stops right after the last value is assigned.
Let’s keep in touch:
Check out my writings on btc++ insider edition
Try my new app Sats Tracker, an expense tracker app for people living in the Bitcoin standard.
Zap me a coffee and leave me a message: tuma@wallet.yakihonne.com



