从嵌套for循环继续

sil*_*ent 4 powershell loops

我有以下循环结构:

while ($reader.Read() -eq $true)
{
    $row = @{}
    for ($i = 0; $i -lt $reader.FieldCount; $i++)
    {
        if(something...)
        {
            #continue with while
        }
    }
    #do more stuff...          
}
Run Code Online (Sandbox Code Playgroud)

现在,有没有办法在for循环内继续进行外while循环的下一次迭代而没有任何中断变量?因此,如果"某事是真的",我不想去#do more stuff,而是做下一步$reader.read().Continue只进入for循环的下一次迭代.Break只会打破for循环.

Jer*_*ert 10

将内循环分解为函数可以提高可读性,具体取决于变量的混乱程度.

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...          
    }
}
Run Code Online (Sandbox Code Playgroud)

但是如果你想直接这样做,你可以,因为PowerShell标记了中断:

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