TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell For Loop

PowerShell For Loop with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

For Loop

The For loop is also known as a 'For' statement in a PowerShell. This loop executes the statements in a code of block when a specific condition evaluates to True. This loop is mostly used to retrieve the values of an array.

Syntax of For loop

for (; ; )
{
   Statement-1
   Statement-2
   Statement-N
}

In this Syntax, the Initialization placeholder is used to create and initialize the variable with the initial value.

The Condition placeholder in a loop gives the Boolean value True or False. PowerShell evaluates the condition part each time when this loop executes. When it returns a True value, the commands or statements in a command block are executed. The loop executed its block until the condition become false.

The Repeat placeholder in a loop denotes one or more commands which are separated by commas. It is used to modify the value of a variable which is checked inside the Condition part of the loop.

Flowchart of For loop

PowerShell For Loop

Examples

Example1: The following example describes how to use a 'for' loop in PowerShell:

for($x=1; $x -lt 10; $x=$x+1) 
>>  { 
>> echo $x 
>>     }

Output:

1
2
3
4
5
6
7
8
9

In this example, the variable $x is initialized to 1. The test expression or condition $x less than 10 is evaluated. Since 1 less than 10 is true, the statement in for loop is executed, which prints the 1 (value of x).

The repeat statement $x=$x+1 is executed. Now, the value of $x will be 2. Again, the test expression is evaluated to true, and the statement in for loop is executed and will print 2 (value of $x). Again, the repeat statement is executed, and the test expression $x -lt 10 is evaluated. This process goes on until $x becomes 9. When the value of x becomes 10, $x < 10 will be false, and the 'for' loop terminates.

Example2: The following example describes the loop which prints the string values of an array in PowerShell:

PS C:\> $arrcolors = "Red","Orange","Green","White","Blue","Indigo","black","Violet"
PS C:\> for($i=0; $i -lt $arrcolors.Length; $i++) 
>>  { 
>> $arrcolors[$i]
>>     }

Output:

Red
Orange
Green
White
Blue
Indigo
black
Violet

Example3: The following example of for loop displays the same value of variable repeatedly until you press the key: 'ctrl+C' in PowerShell.

PS C:\> $j = 10
PS C:\> for (;;)
>> {
>>     echo $j
>> }

Output:

10
10
10
10
10
10........................

Example4: The following example prints the even and odd number from 1 to 30 in a table form.

PS C:\> for($i=1;$i -le 30;$i++){
>> if($i -le 1)
>> { 
>> echo "Even   -   Odd"
>>}
>> $res=$i%2
>> if($res -eq 0)
>> {
>> echo "  $i          "
>> }else
>> {
>> echo "           $i"
>> }
>> }

Output:

Even   -   Odd
                1
  2
                3
  4
                5
  6
                7
  8
                9
  10
                11
  12
                13
  14
                 15
  16
                 17
  18
                 19
  20
                 21
  22
                 23
  24
                 25
  26
                  27
  28
                  29
  30

Next TopicForEach loop




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf