How does this work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How does this work?

**Update I figured it out. int sum = 0; for(int i = 1; i<=3; i++) { if(i==2) { continue; } sum+=1; } Console.WriteLine(sum); In this code, it outputs 2, but sum+=1; means sum=sum+1, how does it take value from i ?

8th May 2024, 10:25 AM
Adrita Khanra
Adrita Khanra - avatar
2 Answers
+ 1
The code which you have wrote simply declares an integer variable called "sum" and assign the value 0 to it. Then you have used a for loop and you declare another integer variable which is only used inside the loop and it says if that variable (int i = 1) is smaller than or equal to 3 then increment by one. Then the loop contain an if statement which says that if the "i variable" is equal to 2 (after the increment), then the program should be restarting the loop and increment by one. Then the program should display the final value of the sum variable. Please let me know if this is incorrect at any part. Hope it helped you.
8th May 2024, 10:44 AM
Danish Zubair
Danish Zubair - avatar
+ 1
Danish Zubair Actually I just figured it out, the thing is it I just mistook this simple loop with factorial I did last session. Thanks though.
8th May 2024, 10:55 AM
Adrita Khanra
Adrita Khanra - avatar