title: Powershell Do while loop with inside If else statement
body: <p>I would like to develop a looping which if the error was caught, and attempts 3 times, then stop and exit.</p>
  <p>The question is how to make the loop count 3 times and stop it?
  Thanks.</p>
  <p>Powershell Code</p>
  <pre><code>
  function loop() {
  
  $attempts = 0
  $flags = $false
  
  do {
  
      try {
  
          Write-Host 'Updating...Please Wait!'
      
           ***Do something action***
  
          Write-Host 'INFO: Update Completed!' -BackgroundColor BLACK -ForegroundColor GREEN
  
          Start-Sleep 1
          
          $flags = $false
          
  } catch {
  
          Write-Host 'Error: Update Failure!' -BackgroundColor BLACK -ForegroundColor RED
          
          $attempts++
          
          $flags = $true
          
          Start-Sleep 2
      
          }
      
      } while ($flags)
      
  } 
  
  </code></pre>