-1

While solving a problem on java I am finding trouble breaking through the if statement which is inside a for loop and this for loop is inside another if statement. The code fragment is as below:

package experment;
public class myclass_2{
 public static void main(String[] args){
  int t;
  int remaining;
  boolean is_undertaker_dead = false;
  Scanner s = new Scanner(System.in);
  System.out.println("hey");
  t = s.nextInt();
  for(int i=0; i<t; i++)
  {
      String S = Integer.toString(s.nextInt());
      char c[] = S.toCharArray();
      for(int j=0; j<c.length; j++)
      {
          if(c[j]=='2')
          {
              remaining = c.length-j;
              for(int a=j; a<c.length; a++)
              {
                  if(remaining<c.length)
                      {
                      remaining=0;
                  }
                  if(c[j+remaining]=='1')
                  {
                     is_undertaker_dead=true;
                     break;
                  }
                  remaining--;
              }
          }
      }
      if(is_undertaker_dead==true)
      { 
          System.out.println("The Streak is broken!");
      }
      else
      {
          System.out.println("The streak");
      }
  }
 }

I want to break outside of all the for loops once the break statement is called. I know this just break out of the if statement but the loop still continues. I also tried using the label method which I find on stackoverflow and google, but they somehow didn't work and confused me even more. Thank you in advance for your support, it means a lot!

Dev Joshi
  • 73
  • 1
  • 1
  • 8
  • "I tried using the label method" — what did you try and how did it not work? – khelwood Aug 02 '18 at 12:08
  • @sujatha Doing return would exit out of the rest of the method, and that may/may not be what he wants. He may want to break out of that subsection of code within the method only. – CAMD_3441 Aug 02 '18 at 12:11
  • @CDVAProgrammer then he needs to put the part that he wants to be able to break from, into a separate method, so that return does exactly what he needs it to do. – kumesana Aug 02 '18 at 12:14

3 Answers3

1

Firstly, break doesn't break out of if statement, it breaks out of loops, in your case second for loop.

In your code, break breaks out of inner for loop. To break out of outer for loop, simply put below condition, just prior to the closing bracket of the outer loop

if (is_undertaker_dead)
    break;
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
0

Add this line before the first loop:

mainloop:

and to break from all the loops, call this

break mainloop;
Hamza Hathoute
  • 438
  • 3
  • 12
0

Use a labeled version of break:

outerLoop:
for(int j=0; j<c.length; j++)
  {
      if(c[j]=='2')
      {
          remaining = c.length-j;
          for(int a=j; a<c.length; a++)
          {
              if(remaining<c.length)
                  {
                  remaining=0;
              }
              if(c[j+remaining]=='1')
              {
                 is_undertaker_dead=true;
                 break outerLoop;
              }
              remaining--;
          }
      }
  }

You can find more information and examples in the Java tutorial.