Question

On button click, the for-loop is absolutely ignored. It just skips over the loop and continues executing. Why does this occur. The terminal prints "breakpoint" and then "finish" immediately.

Skeleton Code:

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    if(SomeClass.someMethod() == true){
           boolean passed = false;
           for(int p = 0; passed = false; p++){
                  //my code...

           }
        System.out.println("breakpoint");
    }
    else{
           someJTextArea.append(...some message...);    
    } 
   System.out.println("finish");
}
Was it helpful?

Solution

You're missing an equals sign, it should be

 boolean passed = false;
 for(int p = 0; passed == false; p++){
              //my code...
 }

As it is right now, you're not checking the value of passed on every iteration but instead you're doing an assignment.

Also, be sure to change the value of passed inside the loop to avoid infinite iterations.

OTHER TIPS

passed = false

means it assigns value false to passed and then gets evaluated to false

change it to

passed == false

if you want infinite iterations

You declare boolean passed = false; and use that as a condition in for loop. for(int p = 0; passed = false; p++)

when you say passed = false it returns false. So change = to ==

That's assignment: A = B That's comparing: A == B

So you need to change your loop to

for(int p = 0; passed == false; p++){ 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top