+ 1
why?
public class Program { public static void main (String [] args) { int [] myArr = {6, 42, 3, 7}; int sum = 0; for (int x = 0; x <myArr.length; x ++) { sum + = myArr [x]; } System.out.println (sum); } } Hi, why is there X in this command myArr [x]; 
3 Antworten
+ 1
x there using for index value. It will work like myArr[0], myArr[1],.... Upto myArr[myArr.length - 1]..
Revise the topic of how loop works.. Or complete it if not done to understand it better..
Instead x is temparary variable taken in loop, so you take any other names also instead of like n, m, i, j... Any valid name you can declare and use as used x.
+ 1
see the "int x" inside of the for loop? Every iteration x is increases by 1 so the elements in the "myArr[]" array are selected using the the value of the x variable, then variable "sum" adds the values of each element to itself
+ 1
Thank you so much