Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Modifying For Loop | For loop
C++ Loops
course content

Contenido del Curso

C++ Loops

C++ Loops

1. While Loop
2. For loop
3. Nested loops

bookModifying For Loop

In the previous chapters, we explored the basics of the for loop in C++. We learned how to use it for iterating through a sequence of values and performing repetitive tasks. Now, it's time to take our understanding to the next level by examining how we can modify and enhance the capabilities of the for loop to make it more powerful.

Advanced Loop Control

The traditional for loop in C++ is a highly efficient and expressive way to iterate through a range of values, but there are situations where more control over the loop is needed. This chapter will introduce you to various techniques for modifying and fine-tuning your for loops.

Customizing the Initialization, Condition, and Iteration Statements

First and foremost, it's important to note that none of the three components within the for loop structure are obligatory. You can eliminate each of them, resulting in the following code:

Surprisingly, this still works! This construct creates an infinite loop, the code enclosed within it will execute forever as long as your computer remains powered. With this in mind, you can accomplish any objective by simply experimenting with a for loop.

Examples

cpp

main

copy
1234567
#include <iostream> int main() { for (int i = 0, j = 5, k = 10; i < 5; i++) std::cout << i << ' ' << j << ' ' << k << '\t'; }

You can initialize multiple variables, separated by commas. This allows you to declare and initialize several variables before entering the loop. Each initialization can be a declaration and initialization of a variable, and they are executed in order from left to right.

cpp

main

copy
12345678910111213141516171819202122
#include <iostream> int main() { // no condition for (int i = 0; ; i++) std::cout << i << std::endl; // outer condition bool outer_condition = true; for (int i = 0; outer_condition; i++) { if (i == 10) outer_condition = false std::cout << i << std::endl; } // multiple conditions bool outer_condition = true; for (int i = 0; i < 10 && outer_condition; i++) std::cout << i << std::endl; }

There are several ways to structure the condition in a for loop. Choose the type of condition that best suits the logic and requirements of your program. Each approach provides flexibility in controlling the loop's behavior.

cpp

main

copy
123456789101112131415161718192021
#include <iostream> int main() { // no condition for (int i = 0; ; i++) std::cout << i << std::endl; // outer condition bool outer_condition = true; for (int i = 0; outer_condition; i++) { if (i == 10) outer_condition = false; std::cout << i << std::endl; } // multiple conditions for (int i = 0; i < 10 && outer_condition; i++) std::cout << i << std::endl; }

You can use multiple update statements separated by commas. Using custom or multiple updates allows you to have more control over the loop control variables and adapt the loop to the specific needs of your program.

What is the purpose of the following for loop construct?

What is the purpose of the following for loop construct?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt