LOOPS in C++

Why was the for loop feeling cold?

Because it left its i out!

Loops in C++ allow you to execute a block of code multiple times, based on a certain condition. There are a few types of loops in C++, but the most commonly used are for loops, while loops, and do-while loops.

A for loop is used to execute a block of code a certain number of times. The basic syntax of a for loop is as follows:

for (initialization; condition; increment) {
    // code to be executed
}

The initialization statement is executed only once, before the loop starts. The condition is checked before each iteration of the loop, and if it’s true, the code block inside the loop is executed. The increment statement is executed after each iteration of the loop.

For example, to print the numbers from 1 to 10 using a for loop:

for (int i = 1; i <= 10; i++) {
    cout << i << endl;
}

This for loop will execute the code block inside the loop 10 times, and on each iteration, it will print the value of i, and then increment i by 1.

A while loop is used to execute a block of code as long as a certain condition is true. The basic syntax of a while loop is as follows:

while (condition) {
    // code to be executed
}

The condition is checked before each iteration of the loop, and if it’s true, the code block inside the loop is executed.

For example, to print the numbers from 1 to 10 using a while loop:

int i = 1;
while (i <= 10) {
    cout << i << endl;
    i++;
}

This while loop will execute the code block inside the loop 10 times, and on each iteration, it will print the value of i, and then increment i by 1.

A do-while loop is similar to a while loop, but the condition is checked after the code block inside the loop is executed, which means that the code block inside the loop will be executed at least once. The basic syntax of a do-while loop is as follows:

do {
    // code to be executed
} while (condition);

For example, to print the numbers from 1 to 10 using a do-while loop:

int i = 1;
do {
    cout << i << endl;
    i++;
} while (i <= 10);

This do-while loop will execute the code block inside the loop 10 times, and on each iteration, it will print the value of i, and then increment i by 1.

You can also use the break and continue statements inside loops to control the flow of execution. The break statement is used to exit the loop immediately, while the continue statement is used to skip the current iteration of the loop and continue with the next one.

It’s worth noting that, when using loops, it’s important to make sure that the condition used to control the loop will eventually evaluate to false, otherwise, you will end up with an infinite loop.

C++ does not have a built-in foreach loop like some other programming languages. However, you can achieve similar functionality using a for loop and the begin() and end() member functions of the container you are iterating over.

For example, if you have a vector of integers and you want to iterate over each element and print it, you can use a for loop like this:

std::vector<int> my_vector = {1, 2, 3, 4, 5};
for (auto i = my_vector.begin(); i != my_vector.end(); ++i) {
    std::cout << *i << std::endl;
}

Another way to do this is by using the range-based for loop, which was introduced in C++11.

std::vector<int> my_vector = {1, 2, 3, 4, 5};
for (auto &i : my_vector) {
    std::cout << i << std::endl;
}

In both examples, the begin() and end() member functions are used to obtain iterators pointing to the first and one-past-the-last elements of the vector, respectively. The for loop then iterates through the elements by incrementing the iterator. auto is used to automatically deduce the type of the iterator.

This is commonly called as range based for loop, and it provides a more concise and readable way to iterate over elements of containers and arrays.

So while C++ doesn’t have a built-in foreach loop, the range-based for loop can be used to iterate over the elements of a container or array in a similar way.

Here’s a complete example of how to use a range-based for loop to iterate over the elements of a vector of integers in C++:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> my_vector = {1, 2, 3, 4, 5};
    for (auto &i : my_vector) {
        std::cout << i << std::endl;
    }
    return 0;
}

This program includes the <iostream> and <vector> headers, which are necessary for using the cout function to print the elements of the vector, and for using the vector container, respectively.

The vector is created and initialized with values {1,2,3,4,5} and then using the range based for loop, we are iterating through the vector, and for each element, we are printing the element using the cout function.

In this example, the auto keyword is used to automatically deduce the type of the variable i from the type of the elements in the vector. auto &i means that the variable i is a reference to the current element of the vector, not a copy. This is useful when you want to modify the elements of the vector inside the loop.

This program will print the numbers 1, 2, 3, 4, 5 each in a new line, once you run this program.

Here are some resources:

https://www.w3schools.com/cpp/cpp_conditions.asp

https://www.programiz.com/cpp-programming/if-else

https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm

https://www.cprogramming.com/tutorial/lesson2.html

https://en.cppreference.com/w/cpp/language/if

Check out Caleb Curry YouTube and Subscribe to him I love his content

Here are some exercises to help you practice:

  1. Write a program that uses a for loop to print the numbers from 1 to 100.
  2. Write a program that uses a for loop to print the even numbers from 0 to 100.
  3. Write a program that uses a for loop to calculate the factorial of a number entered by the user.
  4. Write a program that uses a for loop to find the sum of all the numbers from 1 to 100.
  5. Write a program that uses a for loop to print the elements of an array of integers.
  6. Write a program that uses a for loop to find the largest number in an array of integers.
  7. Write a program that uses a for loop to find the common elements in two arrays of integers.
  8. Write a program that uses a for loop to reverse the elements of an array of integers.
  9. Write a program that uses a for loop to print a multiplication table for a number entered by the user.
  10. Write a program that uses a for loop to generate the Fibonacci sequence up to a specified number of terms.

This is all for now!

Please let me know if I forgot something.

Peace!