lukronos
lukronos

Reputation: 75

Get out of a "for loop" in Java

I would like to get out from a "for loop" when an event happened. For instance there is a part of my code :

for (ScanResult scanResult : listeScan) {
    if(sampleposition[position]==0){ 
        change=true;
        while(i < position && change==true){ 
            if(macAdress[i].equals(scanResult.BSSID)){ 
                change = false;
            }
            i++;
        }
        i=0;
        if(change == true && scanResult.level >= -80){ 
            valueSSID[position] = scanResult.level;
            macAdress[position] = scanResult.BSSID;
            sSID[position] = scanResult.SSID;
            if(valueSSID[position]!=0){sampleposition[position]++;}
          //exit here
        }
    }
    else{
        if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
        valueSSID[position] = valueSSID[position] + addlevel;
            if(addlevel!=0){sampleposition[position]++;}
          //exit here

        }
    }
}

Actually, I would like to stop the loop where there are the comments "exit here".

Please, can you tell me how can I proceed? Sorry for my english...

Upvotes: 1

Views: 186

Answers (4)

AmirC
AmirC

Reputation: 326

Use:

break;

Please refer to the following links for further information.

1) Control Flow - Wiki

2) "Break" and "Continue" in JAVA

Hope they would help.

Upvotes: 0

Lovish Sindhwani
Lovish Sindhwani

Reputation: 193

Simply use keyword

break;

where you want to stop the loop

for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){ 
    change=true;
    while(i < position && change==true){ 
        if(macAdress[i].equals(scanResult.BSSID)){ 
            change = false;
        }
        i++;
    }
    i=0;
    if(change == true && scanResult.level >= -80){ 
        valueSSID[position] = scanResult.level;
        macAdress[position] = scanResult.BSSID;
        sSID[position] = scanResult.SSID;
        if(valueSSID[position]!=0){sampleposition[position]++;}
      break;
    }
}
else{
    if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
    valueSSID[position] = valueSSID[position] + addlevel;
        if(addlevel!=0){sampleposition[position]++;}
      break;

    }
}
}

Upvotes: 1

TronicZomB
TronicZomB

Reputation: 8747

You are looking for the break keyword, this will end the loop immediately:

    for (ScanResult scanResult : listeScan) {
    if(sampleposition[position]==0){ 
        change=true;
        while(i < position && change==true){ 
            if(macAdress[i].equals(scanResult.BSSID)){ 
                change = false;
            }
            i++;
        }
        i=0;
        if(change == true && scanResult.level >= -80){ 
            valueSSID[position] = scanResult.level;
            macAdress[position] = scanResult.BSSID;
            sSID[position] = scanResult.SSID;
            if(valueSSID[position]!=0){sampleposition[position]++;}
            break;
        }
    }
    else{
        if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
        valueSSID[position] = valueSSID[position] + addlevel;
            if(addlevel!=0){sampleposition[position]++;}
             break;

        }
    }
}

Upvotes: 5

user2483079
user2483079

Reputation: 533

break;

This exits out of the for loop entirely.

continue;

This goes on to the next iteration of the loop.

Upvotes: 10

Related Questions