0

i saw how to break the loop (using label and break the label) .. but i want to break the first for loop depends on the second for loop :

for example :

public class HelloWorld {

    static private int i;
    public static void main(String[] args) {
        int y = 20;
        for (; y <= 30; y += 2) 
        {
            System.out.printf("value of increamented  y Value is %d\n", y);
            increamentiValue();
        }
    }

    private static void increamentiValue() 
    {
        i = 0;
        for (; i <= 5; i += 2) {
            System.out.printf("value of i is %d\n", i);
        }
    }
}

for instance here i want to break the "y" loop depends on the number of iteration in "i" loop ..

for ex:

i want to break "y" for loop if the number iteriation in "i" loop is equal to 0 .. because in my program "i" checks the error ... in my ysytem error may occur any "i" .. if two times i dosnt have error (i==0) i want to break the "y" loop.

EX :
if
At y= 22 , i ==0; (no error occurs)
at y= 23 , i ==0; (no error occurs)

i dont want to proceed till 30. i want to break y loop .

4
  • Code is indented with four spaces, not with >. Commented Nov 26, 2013 at 13:54
  • 2
    Please take the time to ensure your code is formatted readably. I've done it for you on this occasion. Commented Nov 26, 2013 at 13:54
  • Please change int y=20;for(; y<=30... to for(int y=20; j<=30.... It is easier to read and understand. Commented Nov 26, 2013 at 13:55
  • 1
    Why use a for loop but put the initializer above it? Why use an instance variable in your for loop? Commented Nov 26, 2013 at 13:55

3 Answers 3

3

Have increamenti_Value return a value that main uses to terminate the loop.


Separately, though, using an instance variable (i) in the for loop in increamenti_Value is a very suspect thing to do. It's also quite odd to use a for loop, leave out the initializer clause, and yet set the initial condition on the line above.

Sign up to request clarification or add additional context in comments.

5 Comments

can u give me an example ... as i am one week old in java
@user2965711: I'm afraid I can't, because it's not clear what you mean by "i want to break "y" for loop if the number iteriation in "i" loop is equal to 0" Because you're setting i to 0 in the function, it will always loop three times (when i == 0, i == 2, i == 4, all of which are <= 5 which is the loop termination condition).
in my case .. "i" is a error check ... in my system error can fall between (i= 1 or 2 or 3 or 4 or 5) sometimes error wont fall .. hence if 2 times error not falls i want to break the "y"loop
@user2965711: I'm sorry, I think you need to have a friend or coworker help you with your English. Your question and comments are not at all clear. What does "if 2 times error not fails" mean? You're doing some operation twice and it needs to not fail both times? You've tried it twice and one of them worked?
np .. but now i am trying with your idea .. i will keep try .. thanks for your support
1

Have the incrementi_Value return the value of i and check that value in your y loop.

incrementi_Value == 0 ? break : continue;

Change private static void incrementi_Value -> private static int incrementi_Value

then place a return i; when you want to return the current value of i.

1 Comment

this is what i tried .. but i am not getting the output ... how to return the value of "i"
0

Instead of increamenti_Value() being void, make it return boolean - true if the caller should break, false otherwise:

private static boolean increamenti_Value() {
    // return true if you want the caller to break
}

And instead of calling it like you are, call it like this:

if (increamenti_Value())
    break;

Note that a called method having knowledge of when the caller should do something doesn't seem like a good design. You may want to reconsider your logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.