+ 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..
12 odpowiedzi
+ 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.
+ 5
Aryan Goel it showing no output where is your print statement System.out.print without this how it will be print.
+ 4
Put your if condition inside loop and do int i = 0
for(int i = 0; i < 5; i++) {
if (i == 4)
break;
}
+ 2
Aryan Goel Check my code once. You will not get any error.
+ 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
+ 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
+ 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 ......
0
AJ Anant it's showing" illegal start of the statement" pointing to the bracket after if
When I put if inside for loop .
0
Ok showAJ Anant
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;
}
}
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()
- 1
Break it use with switch case and loop