- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use PowerShell break statement with the For loop?
To use the Break statement with the For loop, consider the below example.
Example
for($i=1; $i -le 10; $i++){ Write-Host "i = $i" if($i -eq 5){break} }
Output
i = 1 i = 2 i = 3 i = 4 i = 5
Here, when the value of $i reaches to 5, For loop gets terminated.
To use the Break with the nested For loop.
- Inner For loop Break statement.
Example
for($i=1; $i -le 3; $i++){ for($j=1; $j -le 5; $j++){ Write-Host "i = $i" Write-Host "j = $j`n" if($j -eq 3){break} } }
Output
i = 1 j = 1 i = 1 j = 2 i = 1 j = 3 i = 2 j = 1 i = 2 j = 2 i = 2 j = 3 i = 3 j = 1 i = 3 j = 2 i = 3 j = 3
Here, when the value of $j reaches 3, the only inner loop gets terminated but the outer loop execution still continues.
- Outer For loop Break Statement.
Example
for($i=1; $i -le 3; $i++){ for($j=1; $j -le 2; $j++){ Write-Host "i = $i" Write-Host "j = $j`n" } if($i -eq 2){break} }
Output
i = 1 j = 1 i = 1 j = 2 i = 2 j = 1 i = 2 j = 2
In the above example, when the value of $i reaches to 2 it breaks outer and inner loop both.
- Related Articles
- How to use PowerShell Break statement with the While Loop?
- How to use PowerShell break statement in foreach loop?
- How to use PowerShell Break statement with the Switch command?
- How to use PowerShell Break with the Label.
- How to use the break statement to come out of a loop in JavaScript?
- How to use else conditional statement with for loop in python?
- How do we use a break statement in while loop in C#?
- What is PowerShell Break statement?
- How to use the foreach loop parallelly in PowerShell?
- How to break a for loop in Python?
- How can I use a label with break statement in JavaScript?
- How to use continue statement in Python loop?
- How to use PSCustomObject in PowerShell foreach parallel loop?
- How to use for...in statement to loop through an Array in JavaScript?
- How to use range-based for() loop with std::map?

Advertisements