+ 5
What it do? x<myArr.length;
its part of this code int [ ] myArr = {6, 42, 3, 7}; int sum=0; for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } System.out.println(sum); // 58
4 ответов
+ 10
This case:
x=0
myArr.length = 4 //the size of the array
In the loop:
Iteration 0:
0<4 = true
x=0+1 //1
Iteration 1:
1<4 = true
x=1+1 //2
Iteration 2:
2<4 = true
x=2+1 //3
Iteration 3:
3<4 = true
x=3+1 //4
Iteration 4:
4<4 = false
End of the loop.
+ 11
if x>=length of array->end of loop .
+ 4
It represents the condition for the loop to be executed. While that condition is true, the loop continues; if the condition is false, the loop ends.
+ 1
This is called loop. The meaning of myArr.length is the total length that is in inside the array i.e. { }. So, whenever we try to sum all the number inside the array, we specially put .length method on it.
if we do not want to loop all number then your code will be like
for(int x = 0; x< 1; x++)
Overall, If you want to loop all number inside the array, because we are lazy to count all the number then we put .lenght.
If you want to learn more coding stuff, visit thenewboston channel on youtube. This is a great place to study about programming.