Зміст курсу
C++ Loops
C++ Loops
Modifying For Loop
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:
for
for (;;) { }
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.
Initializing of more than one variable in for loop
This can be useful when you need to traverse an array from both the beginning and the end simultaneously. For example, it’s helpful for checking if a word is a palindrome.
main
#include <iostream> int main() { std::string word = "radar"; for (int i = 0, j = word.length() - 1; i < j; ++i, --j) if (word[i] != word[j]) return 0; // Mismatch found std::cout << "The world is palindrome!"; }
You can initialize multiple variables, separated by commas. This allows you to declare and initialize several variables before entering the loop.
Different ways of using condition in for loop
You can use multiple conditions in a for
loop to handle complex scenarios where the loop's execution depends on more than one factor. For example, you can terminate the loop based on both the index limit and an external flag.
main
#include <iostream> int main() { // Simulated temperature readings (in Celsius) int size = 8; int temperatures[size] = {25, 27, 30, 33, 36, 40, 38, 42}; // Threshold for critical temperature const int criticalTemperature = 40; bool safe = true; // Temperatures until all are processed or a critical temperature is found for (int i = 0; i < size && safe; i++) if (temperatures[i] >= criticalTemperature) safe = false; // Stop loop if (!safe) std::cout << "The critical temperature was reached!"; }
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.
Using custom or multiple updates
Custom or multiple updates in a for
loop allow you to modify the loop variable(s) dynamically or perform complex updates beyond the standard single increment or decrement. This flexibility is particularly useful in scenarios where you want to control the flow of the loop to skip elements, traverse multiple ranges, or handle conditional updates.
main
#include <iostream> int main() { int size = 8; int values[size] = {1, 2, 3, 4, 5, 6, 7, 8}; for (int i = 0; i < size; i += 2) std::cout << "Value: " << values[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.
Дякуємо за ваш відгук!