+ 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; }

8th Mar 2020, 6:05 AM
Rahil Kamdar
Rahil Kamdar - avatar
2 Réponses
+ 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 .
8th Mar 2020, 6:34 AM
Saransh Agarwal
Saransh Agarwal - avatar