주제

 

for, foreach 사용해서 1 에서 10 까지 출력하기

 

 

 방법

 

비교연산자 -le: 작거나 같음

 

for($i=1; $i -le 10; $i++) {

  Write-Host("i="+$i)
}

 

비교연산자 - 같음

 

foreach($i in 1..10) {

  Write-Host("i="+$i)

}

 

 

 실행

 

Powershell 콘솔창에서 실행한다.

 

# for

PS D:\>
>> for($i=1; $i -le 10; $i++) {
>>
>>   Write-Host("i="+$i)
>> }
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
PS D:\>

 

# foreach

PS D:\>
>> foreach($i in 1..10) {
>>
>>   Write-Host("i="+$i)
>>
>> }
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10

 

 

 관련글

2022.12.13 - [etc/Powershell] - [Powershell] 별칭(Alias) 확인하기 - Get-Alias

 

 

 출처

 

마이크로소프트 문서

 

for

about_For

https://docs.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_for?view=powershell-7.2 

 

비교연산자

about_comparison_operators

https://docs.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-7.2 

 

foreach

about_Foreach

https://docs.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.2

 

 

 

 

 

 

+ Recent posts