+ 1

How to use break statement?

FIND THE MISTAKE IT SHOWS TOTAL 3 ERRORS... PLEASE HELP ME public class Program { public static void main(String[] args) { for(int i; i<5; i++) } if (i<=4) { break; } } /THIS IS MY PROGRAM AND THIS PROGRAM KEEP GIVING ERRORS..

13th Jun 2020, 11:51 AM
Aryan Goel
Aryan Goel - avatar
12 Respostas
+ 4
Aryan Goel You can't use break anywhere other than loops or switch. There is 2 problem. One is you declared i in loop and using outside the loop 2nd Problem is you used break outside the loop which is wrong. Check my first reply and compare with your Code.
13th Jun 2020, 12:06 PM
AÍąJ
AÍąJ - avatar
+ 5
Aryan Goel it showing no output where is your print statement System.out.print without this how it will be print.
14th Jun 2020, 6:56 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Put your if condition inside loop and do int i = 0 for(int i = 0; i < 5; i++) { if (i == 4) break; }
13th Jun 2020, 11:55 AM
AÍąJ
AÍąJ - avatar
+ 2
Aryan Goel Check my code once. You will not get any error.
13th Jun 2020, 12:02 PM
AÍąJ
AÍąJ - avatar
+ 2
Aryan Goel Because you are not printing anything. You should know to get Output you need to print. write System.out.println(i) before if condition
13th Jun 2020, 3:55 PM
AÍąJ
AÍąJ - avatar
+ 2
In for loop the compiler will execute the loop untill the condition is true It's like a loop Break Syntax break this loop Simple bro
14th Jun 2020, 9:31 AM
Coder
Coder - avatar
+ 1
AJ Anant Thanks alot .Your answers to my questions really cleared my doubts. Really thanks alot for guiding me. And pls do help me ......
13th Jun 2020, 2:56 PM
Aryan Goel
Aryan Goel - avatar
0
AJ Anant it's showing" illegal start of the statement" pointing to the bracket after if When I put if inside for loop .
13th Jun 2020, 11:59 AM
Aryan Goel
Aryan Goel - avatar
0
Ok showAJ Anant
13th Jun 2020, 12:02 PM
Aryan Goel
Aryan Goel - avatar
0
Dear AJ Anant See this program it's showing No Output .Why? public class Program { public static void main(String[] args) { for(int i=2; i<10; i++) if (i==6) break; } }
13th Jun 2020, 3:11 PM
Aryan Goel
Aryan Goel - avatar
0
this is correct, you don't have to type parentheses {} in loop and if() if there is just one command in block public static void main(String[] args) { for (int i=2; i<10; i++) if (i==6) break; } //end of main()
13th Jun 2020, 5:23 PM
zemiak
- 1
Break it use with switch case and loop
14th Jun 2020, 8:52 PM
Amani Toama
Amani Toama - avatar