0

What is the best way to simply this expression so that I don't have to set colFinished twice? (colFinished has to be reset to false in each run of the outer loop)

boolean colFinished = false;
    for (int c = 0; c < SIZE; c += 1) {
        colFinished = false;
        while (!colFinished) {
            for (int r = 1; r < SIZE; r++) {
            ...
4
  • 1
    "colFinished has to be reset to false in each run of the outer loop" - Why? There is no apparent reason for that. Commented Sep 22, 2014 at 7:48
  • If you want the boolean flag colFinished because you wish to break from the inner for-loop, you might want to consider using a label. Commented Sep 22, 2014 at 8:01
  • @Seelenvirtuose I haven't made that clear in the little snippet I gave you, but each run of the outer loop is a different column, so I reset my check on whether the column is finished. Commented Sep 22, 2014 at 8:05
  • Looking at your code, I feel like do..while may suit you! Commented Sep 22, 2014 at 9:22

2 Answers 2

2

If you have no use of colFinished outside the for loop, try :

for (int c = 0; c < SIZE; c += 1) {
    boolean colFinished = false;
    ...............

Else I think there is no other way.

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

3 Comments

A good compiler would optimize your code, because you declare colFinished in the loop. Declare it outside and just set it false inside the loop.
@null : yes. but I think OP is not aware of this type of syntax. That's why such a question is being asked.
@null If the variable has only meaning inside the loop, then I highly advise for declaring it inside the loop. This is all about intention. Let the compiler do its work. As a programmer you should write readable code that carries your intention!
0

You can declare your variable inside the first for-loop:

for (int c = 0; c < SIZE; c += 1) {
    boolean colFinished = false;
    while (!colFinished) {
        for (int r = 1; r < SIZE; r++) {
        ...

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.