for loop

Nov 1, 2012 at 11:57am
hi
I reed that code in some where:
1
2
3
4
5
int arr[] = {3,4,5,32,4,2,4,5,2,5,3};
for(int i = 0; arr[i] ; i++)
{
   //some statements
}


what is the meaning of arr[i] in for ?
Nov 1, 2012 at 12:11pm
i think your code should be have some mistake isn't?

your code should be
1
2
3
for( int i = 0 ; i < 11 ; i++){
cout << arr[i];
}

this will print out the array from [0] to [10]
Nov 1, 2012 at 12:11pm
Any scalar expression in a condition explicitly converted to the bool type. If the value of the expression is equal to zero then it is converted to bool false. Otherwise it is converted to bool true.

So the expression arr[i] in the condition of the for loop checks whether it is true (not equal to zero) or false (equal to zero).

Your arrray has no element equal to zero. So the loop will continue even when the i will have value outside the acceptable values for indexes of the array.:)
Topic archived. No new replies allowed.