+ 2
Help with code
Hi, Can someone please explain this piece of code to me? I just started using sololearn, i have no prior coding experience and cant quite make this out. Thanks. int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } } System.out.println(result);
8 Answers
+ 30
result will be 17
//loop will run for 0,1,2,...4
//for 0,1,2&4 ... result will be increased by 0+1+2+4 ... ie by 7
//for 3 , result will be increased by 10
+ 12
int result = 0;
A variable named result has been declared which is an integer (like 1 , 2, 0, -5, 87 etc. but it cannot be fraction like 2.5, 1.3 etc.). It's initial value is 0.
for (int i = 0; i < 5; i++) {
if (i == 3) {
result += 10;
} else {
result += i;
}
}
This is a loop block, that means it'll keep running until it's condition becomes false.
for(int i=0; i< 5; i++)
A loop variable i has been declared with initial value 0. i < 5 means 0, 1, 2, 3, 4 are valid values for i. So, till i will have valid values, the loop will keep running. When i will become 5, the loop will be stopped. i++ means i will be added with 1 on each iteration. So, the loop variable i will be changed as 0, 1, 2, 3, ... and so on.
if (i == 3) {
result += 10;
}
This is block of if-statement. It means, if i is 3, then 10 should be added with result.
result += 10 means, result = old value of result + 10
else {
result += i;
}
This is else block. It will be executed when the if condition is false, that means i is not 3. When i will have any value other than 3, the value of i will be added with result.
System.out.println(result);
It will print the value of result on screen when the loop is done.
Now, let's trace the output:
when i=0,
i is not 3, so else block will work: result += i => result = 0+0 = 0
when i=1,
i is not 3, so else block will work: result += i => result = 0+1 = 1
when i=2,
i is not 3, so else block will work: result += i => result = 1+2 = 3
when i=3,
i is 3, so if block will work: result += 10 => result = 3+10 = 13
when i=4,
i is not 3, so else block will work: result += i => result = 13+4 = 17
when i=5,
Loop condition becomes false. So now it's time to print the result.
OUTPUT: 17
+ 9
By initializing result as 0, we're just giving it an initial value, so that next time we can add more values with it. The value of result will keep changing on each iteration.
The value of a variable is changeable. That's why it's called variable (because it varies). If you want any constant value, you'll have to use the keyword "final". Right now, just think about variables :)
initially, result = 0
i=0 => result = 0
i=1 => result = 1
i=2 => result = 3
i=3 => result = 13
i=4 => result = 17
i=5 => loop stops.
Now, which value is stored in result? it's 17 (The other values have been replaced by 17)
+ 8
The value of result changes with i because of the if-else part. The code is written in such a way that 10 will be added with result if i is 3, otherwise i will be added with it. If you do nothing with result inside loop body, result will remain 0.
You're welcome :)
+ 4
okay i understand that perfectly now. with each iteration the value of i gets added to the previous value of result but i still don't understand why the value of result changes when the value of i changes.
Nevertheless I understand it now, Thanks a lot Gaurav and Shamima. Excellent help.
+ 2
Okay but i need an explanation, I have no idea how you got to 17.
+ 2
sorry that makes no sense to me at all.
I was fine using sololearn untill now, that one i dont understand at all. Is there any other way to explain it?
+ 2
ok that makes more sense to me but still something i dont understand.
int i becomes 4 before the statement becomes false, so then else block runs but how did int result become 13? at the beginning it states that int result = 0.
nowhere does it state that int result changes so how can int result = 13 when the loop stops running?