「PowerShell」while文にcontinueを使ってループの先頭に戻る

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
while (条件式)
{
if(条件式) {
処理コード
continue
}
}
while (条件式) { if(条件式) { 処理コード continue } }
while (条件式)
{
    if(条件式) {
       処理コード
       continue
       }
}

条件がtrueの間、処理を繰り返します。
条件式がfalseの場合、処理を終了します。
最初の判定がfalseの場合、処理は1回も実行されません。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$i = 10
while ($i -lt 15) {
if ($i -eq 13) {
$i = $i + 1
continue
}
Write-Host($i)
$i = $i + 1
}
$i = 10 while ($i -lt 15) { if ($i -eq 13) { $i = $i + 1 continue } Write-Host($i) $i = $i + 1 }
$i = 10

while ($i -lt 15) {
    if ($i -eq 13) {
        $i = $i + 1
          continue
    }
    Write-Host($i)
    $i = $i + 1
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PS C:\study\powershell> ./test.ps1
10
11
12
14
PS C:\study\powershell> ./test.ps1 10 11 12 14
PS C:\study\powershell> ./test.ps1
10
11
12
14

 

PowerShell

Posted by arkgame