-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRange-based-for-loop.cpp
More file actions
52 lines (31 loc) · 1.09 KB
/
Range-based-for-loop.cpp
File metadata and controls
52 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
// Range based for loops
// Link: https://youtu.be/8qrZYjL2jBg
// Title: C++ Programming Tutorial 58 - Range based for loops
// Creator: Caleb Curry
// More: https://www.geeksforgeeks.org/range-based-loop-c/amp/
int main() {
setlocale(LC_ALL, "italian");
int data[5] = {1, 2, 3, 4, 5}; // tutti gli indicidi del array devono
// contenere un valore! in caso contrario
// il loop scrivere 0 al posto dei valori
// mancanti!
for (int n : data) // è uguale a: n prende il valore di data al indice n e
// passa attraverso ogni casella come un postino.
cout << n << " ";
cout << endl;
int data2 [5];
int somma = 0;
for (int & n : data2) { // si possono usare per riempire un array con &
// cout << "Inserisci il " << n + 1 << " valore: "; // non funziona!
cin >> n; // funziona!
somma += n; // funziona!
}
for (int n : data2)
cout << n << " ";
cout << somma;
return 0;
}
// una cosa importante non si possono usare nelle funzioni e sono disponibili
// da C++11