int main() { // Outer loop for (int i = 1; i <= 2; ++i) { cout << "Outer: " << i << "\n"; // Executes 2 times // Inner loop for (int j = 1; j <= 3; ++j) { cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3) } } return 0;...
Two loops that run separately A loop that never ends A single loop that repeats twiceSubmit Answer » What is an Exercise? Test what you learned in the chapter: C++ Nested Loops by completing 4 relevant exercises. To try more C++ Exercises please visit our C++ Exercises page....
for(inti : myNumbers) { cout << i <<"\n"; } Try it Yourself » Loop Through a String You can also use a for-each loop to loop through characters in a string: Example string word ="Hello"; for(charc : word) { cout << c <<"\n"; ...
for(inti=0;i<10;i++){if(i==4){break;}cout<<i<<"\n";} Try it Yourself » Definition and Usage Thebreakkeyword is used to break out of aforloop, awhileloop or aswitchblock. More Examples Example Break out of a while loop: ...
for(inti =1; i <=10; i = i +2) { cout << i <<"\n"; } Try it Yourself » Example Use a for loop to print the powers of 2 up to 512: for(inti =2; i <=512; i *=2) { cout << i <<"\n"; } Try it Yourself » ...
inti; // Get the length of the array intlength = sizeof(ages) / sizeof(ages[0]); // Loop through the elements of the array for(intage : ages) { sum += age; } // Calculate the average by dividing the sum by the length
#include <vector> using namespace std; int main() { // Create a vector called cars that will store strings vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"}; // Loop through the vector with an iterator in reverse order for (auto it = cars.rbegin(); it != ...
Exercise: C++ The foreach LoopWhat will be the output of the following code?int values[3] = {10, 20, 30};for (int x : values) { cout << x << " ";} 10 20 30 0 1 2 10 30 20 No output Submit Answer » What is an Exercise? Test what you learned in the chapter: C++ ...
Exercise: C++ FunctionsWhat is a function in C++?A loop that runs indefinitely A data type for integers A pointer variable A block of code that runs only when calledSubmit Answer » What is an Exercise? Test what you learned in the chapter: C++ Functions by completing 4 relevant ...
keyword to break out of a loop. Read more about for loops in our C++ For Loop Tutorial.Read more about while loops in our C++ While Loop Tutorial.Read more about break and continue in our C++ Break Tutorial.❮ C++ Keywords Track your progress - it's free! Log in Sign Up COLOR...