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

29th Jun 2017, 6:46 AM
Walter Mugo
Walter Mugo - avatar
30 ответов
+ 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.
29th Jun 2017, 7:04 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
29th Jun 2017, 6:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
17 is ans.
29th Jun 2017, 6:56 AM
Rohan Vijayvergiya
Rohan Vijayvergiya - avatar
+ 3
17
9th Feb 2018, 7:46 AM
Rachelle Cahayon
Rachelle Cahayon - avatar
+ 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*
14th May 2021, 5:39 AM
Natraj M
Natraj M - avatar
+ 2
There is a comment section in java solo learn you should use it its very helpful with alot of people explaing the question
29th Jun 2017, 7:34 AM
D_Stark
D_Stark - avatar
+ 1
thanks but how is it coming 17 ..would you please asssist me there
29th Jun 2017, 7:00 AM
Walter Mugo
Walter Mugo - avatar
0
whats the answer?
29th Jun 2017, 6:47 AM
Walter Mugo
Walter Mugo - avatar
0
17
9th Oct 2018, 5:40 PM
Vijaymala G Kamble
Vijaymala G Kamble - avatar
0
If anyone explain me plz i don't understand how its come?
7th Dec 2018, 5:03 AM
John Peter
John Peter - avatar
0
17
1st Jun 2019, 2:24 AM
Kommineni Nagasushma
Kommineni Nagasushma  - avatar
0
add
28th Oct 2019, 11:25 AM
tae hyun kim
tae hyun kim - avatar
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 += ; }
20th Nov 2019, 5:11 PM
Tahani kahlifa ALBishri
0
A class defines... (choose two)
20th Nov 2019, 5:13 PM
Tahani kahlifa ALBishri
0
The answer is 17.
11th Jan 2020, 2:56 AM
VADDE NAGARAJU
VADDE NAGARAJU - avatar
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
22nd May 2020, 11:04 PM
javlonbek shoyimqulov
javlonbek shoyimqulov - avatar
0
17
7th Jul 2020, 1:56 PM
ATTAMAH BENEDICT
ATTAMAH BENEDICT - avatar
0
I do not understand how this is 17 it just does not make sense.
11th Jan 2021, 5:39 PM
Henry Mauer
Henry Mauer - avatar
0
17 is the correct answer
6th Apr 2021, 10:23 AM
Joseph S. Lablah, Jr
Joseph S. Lablah, Jr - avatar
0
answer is 17
23rd Nov 2021, 12:02 PM
Muhammadqodir Mahammadov
Muhammadqodir Mahammadov - avatar