Why I obtain this error although I declared the type of I as int in the for loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why I obtain this error although I declared the type of I as int in the for loop?

Import java.util.*; Public class prog{ public static void main (String[] args){ int sum=0; for(int i=0;i<3;i++); sum+=i; System.out.println(sum); } }

5th Feb 2020, 10:37 AM
Hichem GOUIA
Hichem GOUIA - avatar
4 Answers
+ 2
because of the semicolon after for loop
5th Feb 2020, 10:42 AM
Mihai Apostol
Mihai Apostol - avatar
+ 2
Thanks Friends for your help 😊
5th Feb 2020, 9:39 PM
Hichem GOUIA
Hichem GOUIA - avatar
+ 1
Hichem GOUIA , I think your problem is that you put semicolon in the line of the loop. Loop has to be enclosed in curly brackets.
5th Feb 2020, 10:42 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
No it's not because of the semicolon. If I add a new line int i=0; After the int sum=0; the code compiles I have a doubt here if the i is considered as a local variable for the for loop...
5th Feb 2020, 12:34 PM
Hichem GOUIA
Hichem GOUIA - avatar