+ 3

Why is the error coming in this program?

public class Program { public static void main(String[] args) { for (int i=1; i<=10; i++); { System.out.println (i); } } }

2nd Nov 2021, 11:38 PM
Sonam Kumari
Sonam Kumari - avatar
10 Réponses
+ 6
Sonam Kumari semicolons in general shows the compiler where an instruction ends and where the next instruction begins. The for loop itself requires the parentheses for the condition immediately followed by the constructs { what it is you wanted the program to do } So the semicolon actually interfered with and ended believing it had nothing more to do.
3rd Nov 2021, 12:45 AM
BroFar
BroFar - avatar
+ 4
The problem is in your for loop which u added at the end of statement . This errror is coming because when you use for loop it omit single statement without curly bracket . But when u using curly braces like this For () { } If you defining any variable in for loop which u did int i=0 its scope will be in inside of brackets u cannot use it outside same things happening in your case your loop is terminated and inside body your i variable showing undefined. for () statement 1; statement 2 ; here if i won't use brackets then statement 2 will not be part of for loop .
3rd Nov 2021, 1:09 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
/* Program to print a message without using a semicolon ';' Code (Java Pattern Programs Free) */  /*  Program to print 'Hello to Java Programming' without using   semicolon ';'   */    class ProgWithoutSemiColon {        public static void main(String[] args)      {          if (System.out.printf("Hello to Java Programming")               == null)          {                    }        }  }     /*  OUTPUT :    Hello to Java Programming    */ ---------*****--------- (Java Pattern Programs Free) Get it on Google Play https://sfet.in/JavaPatternPrograms
4th Nov 2021, 2:07 PM
Abdiraimov Otabek
Abdiraimov Otabek - avatar
+ 1
Jay Matthews error: cannot find symbol
2nd Nov 2021, 11:47 PM
Sonam Kumari
Sonam Kumari - avatar
+ 1
Jay Matthews it is working😇 But why is the error coming from the semicolon? To terminate the semicolon line is🤔
2nd Nov 2021, 11:57 PM
Sonam Kumari
Sonam Kumari - avatar
+ 1
Martin Taylor thank you so much
3rd Nov 2021, 3:53 PM
Sonam Kumari
Sonam Kumari - avatar
+ 1
General syntax for "for loop" is for(initialization;condition;increment or decrement) { Statements/expressions; } We use semicolon to end an expression , do not use it after for loop starting.
3rd Nov 2021, 4:42 PM
Reshma
+ 1
No need to add semicolon in line 6. Remove it and run again
4th Nov 2021, 1:06 AM
FATHIMA ZIHNA
FATHIMA ZIHNA - avatar
+ 1
bro there is " ; "in for loop remove it and everything will be fine , keep learning more 😇✌️
4th Nov 2021, 8:51 AM
Dollar
Dollar - avatar