Continue while from nested for-loop Continue while from nested for-loop powershell powershell

Continue while from nested for-loop


Factoring out the inner loop to a function could improve readability, depending on how tangled up your variables are.

function processRow($reader) {    $row = @{}    for ($i = 0; $i -lt $reader.FieldCount; $i++)    {        if(-not something...) { return $null }        # process row    }    $row}while ($reader.Read()) {    $row = processRow $reader    if ($row) {        #do more stuff...              }}

But if you want to do this directly, you can, because PowerShell has labeled breaks:

:nextRow while ($reader.Read()) {    $row = @{}    for ($i = 0; $i -lt $reader.FieldCount; $i++) {        if(something...) {            #continue with while            continue nextRow        }    }    #do more stuff...          }


EDIT: a revised, recursive (and untested!) solution so your millage may vary:

function doReader(){    while ($reader.Read() -eq $true)    {        $row = @{}        for ($i = 0; $i -lt $reader.FieldCount; $i++)        {            if(something...)            {                #continue with while                doReader                break;            }        }    }}doReader#do more stuff