+ 3

What is the meaning of this "for" line? What output should i expect?

#include<iostream> Using namespace std; int main() { int arr[5] = {10, 0, 20, 0, 30}; For(int i : arr) { cout<< arr[i] <<"\n"; } }

21st Feb 2019, 7:03 AM
Duna000
Duna000 - avatar
2 Answers
+ 4
Using : in a for loop just goes through all the items in the array. But since the items are there, it will output garbage values. To get the items correctly, use: for(int i : arr) { cout << i << endl; } Just a small note: For should be for and Using should be using.
21st Feb 2019, 7:14 AM
Rowsej
Rowsej - avatar
+ 2
Ha... Thanks.. and for the note I typed in my phone!! Autocorrect dictionary! :D
21st Feb 2019, 8:51 AM
Duna000
Duna000 - avatar