Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in PowerShell: for ($i=0; -le $total-1; $i++) instead of for ($i=0; -lt $total; $i++)

Tags:

powershell

I'm looking at some code examples on powershellpro.com and don't understand why he wrote sample code that loops through an array by:

...having an increment start at zero then increase by one until it is less than or equal to the length of an array minus one...

for ($i=0; $i -le $total-1; $i++)

...instead of having the increment start at zero then increase by one until it is less than the length of the array...

for ($i=0; $i -lt $total; $i++)

Am I missing something? They are functionally equivalent and will both loop through each item in the array. Personally I think the second version is cleaner. Is there a best practice or something that says you should use the first one?

like image 532
WD-40 Avatar asked May 24 '11 15:05

WD-40


1 Answers

>> Am I missing something?
No

>> Is there a best practice or something that says you should use the first one?
usage of 'for' cycle in powershell is the same like in any other language. just depends on attitude of the programmer which style he uses.

like image 82
Tomas Panik Avatar answered Sep 23 '22 23:09

Tomas Panik