# Thread: Break from a for loop with a flag. - Game get at least 3 numbers from 6

1. ## Break from a for loop with a flag. - Game get at least 3 numbers from 6

I donīt understand why my program fail, when i try to jump out of a loop using a flag.

Code:
```        int flag = 0;

for (int i = 0; i < KEY_SIZE; i++) {
for (int j = 0; j < KEY_SIZE || flag == 1; j++) {
if (key[i] == keyinput[j]) {
jackpot++;
flag = 1;
}
}
flag = 0;
}```
If i do with a break everything works ok

Code:
```        for (int i = 0; i < KEY_SIZE; i++) {
for (int j = 0; j < KEY_SIZE; j++) {
if (key[i] == keyinput[j]) {
jackpot++;
break;
}
}
}```

The full code is here.

If you got more then 2 numbers on a key you are a winner.

Text file(keys.txt)
6 7 8 9 40 45
8 9 33 34 36 41
1 2 3 4 5 8

Code:
```#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
#define KEY_SIZE 6

int main(int argc, char** argv) {

FILE *pFile;

int keyinput[KEY_SIZE] = {2, 3, 4, 5, 6, 7};
int key[KEY_SIZE];
char buffer[SIZE];
int jackpot = 0;

if ((pFile = fopen(argv[1], "r")) == NULL) {
return -1;
}

while (fgets(buffer, sizeof (buffer), pFile)) {
sscanf(buffer, "%d %d %d %d %d %d", &key[0], &key[1], &key[2], &key[3], &key[4], &key[5]);

int flag = 0;

for (int i = 0; i < KEY_SIZE; i++) {
for (int j = 0; j < KEY_SIZE; j++) {
if (key[i] == keyinput[j]) {
jackpot++;
break;
}
}
flag = 0;
}

if (jackpot >= 3) {
puts("That key is a winner");
} else {
jackpot = 0;
}
}

return (EXIT_SUCCESS);
}```

2. I think you can use the for loop to break as you want if the middle part is like this:

j < KEY_SIZE && flag == 0

I could be wrong but that seems right to me.

3. Originally Posted by BpB
I think you can use the for loop to break as you want if the middle part is like this:

j < KEY_SIZE && flag == 0

I could be wrong but that seems right to me.
Yes i was messing with || and &&
Code:
` for (int j = 0; j < KEY_SIZE || flag == 0; j++)`
it works

Code:
```   int flag = 0;

for (int i = 0; i < KEY_SIZE; i++) {
for (int j = 0; j < KEY_SIZE && flag == 0; j++) {
if (key[i] == keyinput[j]) {
jackpot++;
flag = 1;
}
}
flag = 0;
}```