+ 7
I can't understand this code, so please can anyone explain me it?
int [ ] myArr = {6, 42, 3, 7}; int sum=0; for(int x=0; x<myArr.length; x++) { sum += myArr[x]; } System.out.println(sum); Output = 58 what does that myArr [x] does? and what is that += means?
20 Antworten
+ 21
😥 it's gonna be a long day, but let's figure it out!
in first round of for loop
x=0, so myArr[x] is myArr[0]=6, sum=0 +6
so sum is 6 now
round 2
x=1, so myArr[x] is myArr[1]=42, sum=6 +42
sum is 48 now
round 3
x=2, so myArr[x] is myArr[2]=3, sum=48+3
sum is 51 now
round 4, final round
x=3, so myArr[x] is myArr[3]=7, sum=51+7
so final sum value 58
+ 13
myArr[x] is used to get all the elements of this array using for loop.myArr[0] means the value '6',myArr[1] means the value '42' and so on.'myArr.length' returns the length or size of this array(here the size is 4) and check whether x<size(inside for loop).Also,inside the for loop, x is assigned with 0,when it runs it check that x<myArr.length (here 0<4),which is true.As it is true it will go inside the for loop and do what ever according to the statement.Here,
sum+=myArr[x],same as sum=sum+myArr[x] which means,
sum=0+myArr[0]
sum=0+6
sum=6 i.e. stored in the variable sum.
After this the for loop will update the value of x(i.e. x++),now the value of x turns to 1 and 1<4,which is true.Then get inside the loop,
sum=6+myArr[1]
sum=6+42
sum=48
Again,the loop will update the value of x and x will be 2,checks x<4 i.e.2<4,which is true,then
sum=sum+myArr[2]
sum=48+3
sum=51
Next,the value of x will be updated to 3 which is also less than 4.Then,
sum=sum+myArr[3]
sum=51+7
sum=58,we get the answer.
After this the value of x is updated again and become x=4,when checking the condition 4<4 which is false and the for loop is terminated.
Then outside the for loop,the statement to print sum will display the sum as,
58
That's all!!!!!!!!
+ 5
i got it.....
Thanks Neetish Raj
You are the best!
+ 4
so how does the output comes out 58?
and what does that myArr [x] does?
+ 4
thanks Sreerag but already understod
+ 3
myArr is your one dimensional integer array , consisting of 4 elements , see below with their position no.s in array , it always start from 0
array elements 6 42 3 7
position 0 1 2 3
x+=y is just a short notation for x=x+y, that's it
similarly
x-=y is x=x-y
x/=y is x=x/y
for * and % also applicable
+ 3
sorry but still i am not understanding what x does in myArr [x]
+ 3
ok ,
myArr is just a list , used to store items, and their numbering for position. begins with 0
so if u want to access some value in that array list , this is the notation to access it
myarr[x]
where depending on the value of x that element is chosen so if
x is 0 then first element of myArr
x is 1 then second element and so on
+ 2
all elements of myArr are added 6+42+3+7=58
elements of array are accessed using position no.s
myArr[0] is 6
myArr[1] is 42 and so on till 4th element
using for loop value of x is changed from 0, 1,2, and 3 , hence myArr[x] access all elements of array and keep adding them into sum variable
+ 2
ya
+ 2
woudn't that then output 7,
what i understand =
An array with the type int is assigned these values, 6, 42, 3, 7
an variable named sum with the type int is assigned the value 0
type int variable x = 0, if the x (0) is less than the length of the array (4)
sum (0) = sum (0) + myArr [x] (0) [ this line is confusing]
print the 'sum'
+ 2
Thank you both of you! now i can proceed!
😀👍
+ 2
Sorry but this discussion is closed, so no need of answering it. thankyou for your kindness!
+ 1
Question: what does that myArr [x] does?
Answer: It means to start counting from 0 to the number of elements of myArr.
myArr have 4 elements: 6, 42, 3 and 7.
In other words it's 0 < 4, 0-3 or 4 numbers.
First will have to add all 4 elements 6+42+3+7 to get 58.
Question: and what is that += means?
Answer: It means to add the total of numbers to the variable sum.
sum is 0 and all 4 elements of myArr is 58.
In other words you will add myArr to sum or 0+58=58.
0
😅🤘
0
Till all elems in myArr are iterated,
sum = sum + myArr[0], first iteration, sum is 6
sum = sum + myArr[1], second iteration, sum is 6+ 42
sum = sum + myArr[2], third iteration, sum is 6+42+3
sum = sum + myArr[3], fourth iteration, sum is 6+42+3+7
condition fails exit;
prints sum
0
could anyone explain me this snippet
print ("sum : " + sum);
i am able to understand the function but this statement.
function sumOfIntegers(int n)
{
int i, sum = 0; // declare the variables i, sum
for (i = 1; i <= n; ++i) { // loop through till satisfies the
condition i <= n
sum += i; // sum = sum + i
}
print ("sum : " + sum);
}
0
var count = 0
item
myArr{
item % 2 == 0 {
count
;
}
}
please help
- 1
sem have you been through courses of java?
- 5
ah shet