Foreach loop
Foreach loop is used to iterate over the elements of the collection. The collection may be an array or a list. Foreach loop iterates through each element present inside array hence it is called as foreach loop. Foreach loop will only work on collection of values. Foreach loop in C++ or more specifically, range-based for loop was introduced with the C++ 11.
CODE
EG 1 . Creating an array called musical instruments and printing out all elements in console using foreach loop
#include <iostream>
using namespace std;
int main() {
    string musicalInstruments[] = { "piano", "guitar", "violin", "drums" };
    for (string musicalInstrument : musicalInstruments) {
        cout << musicalInstrument << endl;
    }
}
EG 2 . Calculating total number of '$' present inside char[] using foreach loop
#include <iostream>
using namespace std;
int main() {
    char symbols[] = { '@', '$', '%', '&', ')', '(', '$', ')', 'P', '8', '$', '%' };
    int dollerCount = 0;
    for (char symbol : symbols) {
        //Inside if statement, if you going to have only one line of code then there is no need to use { and }
        if (symbol == '$') dollerCount++;
    }
    cout << "The number of $ present inside array is " << dollerCount << endl;
}
(Click Here, For more exercises)