š Loop Constructs in C++
Loops execute a block of code repeatedly as long as a specified condition remains true. Modern C++ (C++11 and higher) also introduces range-based for loops for effortless container traversal.
š Loop Types Comparison
š» Code Example: Iteration & Range Traversal
āC++ Source Code (main.cpp)
#include <iostream>
#include <vector>
int main() {
// 1. Traditional for loop
std::cout << "--- Counting 1 to 5 ---" << std::endl;
for (int i = 1; i <= 5; ++i) {
std::cout << "Count: " << i << std::endl;
}
// 2. Range-based for loop (C++11)
std::vector<std::string> frameworks = {"Qt", "Unreal Engine", "Boost", "CUDA"};
std::cout << "
--- C++ High Performance Frameworks ---" << std::endl;
for (const auto& fw : frameworks) {
std::cout << "ā” " << fw << std::endl;
}
return 0;
}
āWindows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
āAdministrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)
āā”ā
Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)
(c) Microsoft Corporation. All rights reserved.
C:\Users\Student\CPP_Project>#include <iostream>
C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp
C:\Users\Student\CPP_Project> main.exe
Hello World from C++ on Windows 11 Pro!
[Process exited with code 0 in 0.001 seconds]
š ļø Execution Output on ā Windows 11 CMD:
āCommand Execution Snippet
--- Counting 1 to 5 ---
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
--- C++ High Performance Frameworks ---
ā” Qt
ā” Unreal Engine
ā” Boost
ā” CUDA
āWindows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
āAdministrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)
āā”ā
Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)
(c) Microsoft Corporation. All rights reserved.
C:\Users\Student\CPP_Project>--- Counting 1 to 5 ---
C:\Users\Student\CPP_Project> --- Counting 1 to 5 ---
Execution successful! Output verified on Windows 11 Pro x86_64.
[Process exited with code 0 in 0.002 seconds]