1string texts[] = {"Apple", "Banana", "Orange"};
2for( unsigned int a = 0; a < sizeof(texts); a = a + 1 )
3{
4 cout << "value of a: " << texts[a] << endl;
5}
1for (int i = 0; i < arr.size(); ++i){
2//use if we explicitly need the value of i
3cout << i << ":\t" << arr[i] << endl;
4}
5for (int element : arr){
6//modifying element will not affect the array
7cout << element << endl;
8}
9for (int &element : arr){
10//modifying element will affect the array
11cout << element << endl;
12}
1#include <iostream>
2#include <array>
3
4int main()
5{
6 int aNumbers[] = { 0, 1, 2, 3, 4, 5 };
7 int count = 0;
8
9 for (int aNumber : aNumbers)
10 {
11 std::cout << "Element "<< count << " : " << aNumber << std::endl;
12 count++;
13 }
14}
1int v[] = {1,2,3,4,5};
2for (int n : v)
3 cout << n << endl;
4//make sure to compile with -std=c++11
1/*sizeof(array_scores) is a pointer to array_scores[],
2and has to be divided by each first-object[0]*/
3for(int a = 0; a < sizeof(array_scores)/sizeof(array_scores[0]); a = a + 1 ){
4 cout << "for loop, a = " << array_scores[a] << " at position " << a << "\n";
5}
6//https://stackoverflow.com/questions/20234898/correct-way-of-looping-through-c-arrays