Someone please explain me this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Someone please explain me this code

public class Program { public static void main(String[] args) { int result = 0; for (int x=0; x<5; x++){ if (x==3){ result += 10; } else { result +=x; } } System.out.println(result); } }

22nd Jan 2021, 9:49 PM
Dhanya Raj Ravi
Dhanya Raj Ravi - avatar
2 Answers
+ 2
This program will use result as storage variable to store x value in a for loop iteration for five time started from zero but checking first the value of x if equal to three if x equal three increment by 10 else by 1 so 0+1+2+10 +4 is the final value stored in result.
22nd Jan 2021, 9:59 PM
HBhZ_C
HBhZ_C - avatar
+ 1
if x is equal to 3 program adds 10 to result otherwise it adds number 0,1,2,4 so the answer will be 17
22nd Jan 2021, 9:58 PM
Abhay
Abhay - avatar