[Solved] Why does the count variable have to be defined outside the curly brackets of the for loop?


Because count would reset to 0 on every iteration of the loop (also count would no longer be visible after the loop)! But, you don’t need count in the first place (since you have i and count is effectively i + 1 – and would be three after loop) –

for (int i=0; i<5; i++) {
    System.out.println(i);
    if (i + 1 == 3) {
        break;
    }
}

or

for (int i=0; i<5; i++) {
    System.out.println(i);
    if (i == 2) {
        break;
    }
}

2

solved Why does the count variable have to be defined outside the curly brackets of the for loop?