+ 6
What is the output of this code? int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else {
What is the output of this code? int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result);
30 Respostas
+ 31
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
System.out.println(result);
// Values of i ranges from 0 to 4 for each loop.
When i = 0, i == 3 is false, result += i, result = 0.
When i = 1, i == 3 is false, result += i, result = 1.
When i = 2, i == 3 is false, result += i, result = 3.
When i = 3, i == 3 is true, result += 10, result = 13.
When i = 4, i == 3 is false, result += i, result = 17.
+ 13
The result is the sum of all values of i except 3, in which the sum value is replaced with 10. Hence result is 0 + 1 + 2 + 10 + 4, which is 17.
+ 5
17 is ans.
+ 3
17
+ 3
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
System.out.println(result);
the loop runs like , 0=0,0<5,0++ |0==3|result =+i (0+0=0) |1<5,1++ |1==3|result =+i (0+1=1)
2<5,2++ |2==3|result =+i (1+2=3)
3<5,3++ |3==3|result =+10 (3+10=13)
4<5,4++ |4==3|result =+i (13+4=17)
so the result will be *17*
+ 2
There is a comment section in java solo learn you should use it its very helpful with alot of people explaing the question
+ 1
thanks but how is it coming 17
..would you please asssist me there
0
whats the answer?
0
17
0
If anyone explain me plz i don't understand how its come?
0
17
0
add
0
Fill in the blanks to calculate the sum of all elements in the array "arr" using an enhanced for loop:
int res = 0;
for
(int el
:
arr) {
res +=
;
}
0
A class defines... (choose two)
0
The answer is 17.
0
int result = 0;
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
System.out.println(result);
the answer is 17
0
17
0
I do not understand how this is 17 it just does not make sense.
0
17 is the correct answer
0
answer is 17