+ 1
What is the use of x<5 in the below code?? I mean the use in arrays and not in the for loop
int myArr[5]; for(int x=0; x<5; x++) { myArr[x] = 42; }
2 Answers
+ 2
Your array can have a maximum of 5 elements.
The index of array starts from 0 .
So , your first element will have the index of zero
(i.e, if myArr[5]={1,2,3,4,5} , then myArr[0]=1 & myArr[4]=5)
So in your program every index/address of array myArr[] is initialised with 42 .
Here we are using x<5 (x is less than 5) because of this index number , a array of
5 elements will have index no. From 0 to 4
10 elements will have index from 0 to 9
So the loop for your program will run as x<5 OR x<=4 .