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

書式
for(初期化式 ; 条件式 ; 増減式){
if(条件式){continue}
}
処理を繰り返す時に使用します。
条件式がtrueの間、処理を繰り返します。
条件式がfalseになるとループ処理を終了します。
continue文でループの先頭に戻ります

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for ($i = 5; $i -lt 8; $i++) {
if ($j -eq 5) {
continue
}
Write-Host ("i =" + $i)
}
for ($i = 5; $i -lt 8; $i++) { if ($j -eq 5) { continue } Write-Host ("i =" + $i) }
for ($i = 5; $i -lt 8; $i++) {
  if ($j -eq 5) {
    continue
  }
  Write-Host ("i =" + $i) 
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PS C:\study\powershell> .\test.ps1
i =5
i =6
i =7
PS C:\study\powershell> .\test.ps1 i =5 i =6 i =7
PS C:\study\powershell> .\test.ps1
i =5
i =6
i =7

 

PowerShell

Posted by arkgame