主頁(yè) > 知識(shí)庫(kù) > Windows Powershell For 循環(huán)

Windows Powershell For 循環(huán)

熱門標(biāo)簽:百度競(jìng)價(jià)排名 Linux服務(wù)器 服務(wù)外包 呼叫中心市場(chǎng)需求 網(wǎng)站排名優(yōu)化 AI電銷 地方門戶網(wǎng)站 鐵路電話系統(tǒng)

如果你知道循環(huán)的確切次數(shù)可以使用For循環(huán),F(xiàn)or循環(huán)屬于計(jì)數(shù)型循環(huán),一旦達(dá)到最大次數(shù),循環(huán)就會(huì)自動(dòng)終止。下面的例子通過(guò)循環(huán)求1-100的數(shù)列和。

復(fù)制代碼 代碼如下:

$sum=0
for($i=1;$i -le 100;$i++)
{
    $sum+=$i
}
$sum

For循環(huán)是特殊類型的While循環(huán)

在For循環(huán)開(kāi)始的圓括號(hào)中,由分號(hào)隔開(kāi)的語(yǔ)句為循環(huán)的控制條件,分別為:初始化,循環(huán)執(zhí)行滿足的條件,增量。
For循環(huán)的控制語(yǔ)句第一個(gè)和第三個(gè)可以為空:

復(fù)制代碼 代碼如下:

$sum=0
$i=1
for(;$i -le 100;)
{
    $sum+=$i
    $i++
}
$sum

For循環(huán)的特殊應(yīng)用

上面的For循環(huán)示例停留在數(shù)字層面上,其實(shí)While循環(huán)能辦到的事,F(xiàn)or循環(huán)也可以,只是可能有時(shí)不方便而已。例如判斷域名的例子:

復(fù)制代碼 代碼如下:

for($domain="";!($domain -like "www.*.*");$domain=Read-Host "Input domain")
{
    Write-Host -ForegroundColor "Green" "Please give a valid domain name."
}
Please give a valid domain name.
Input domain: www
Please give a valid domain name.
Input domain: mossfly.com
Please give a valid domain name.

下面的例子演示逐行讀取文本文件

復(fù)制代碼 代碼如下:

for($file=[IO.File]::OpenText("c:autoexec.bat") ; !($file.EndOfStream);$line=$file.ReadLine() )
{
    $line;
}
$file.Close()
REM Dummy file for NTVDM

您可能感興趣的文章:
  • Windows Powershell IF-ELSEIF-ELSE 語(yǔ)句
  • Windows Powershell Switch 語(yǔ)句
  • Windows Powershell ForEach-Object 循環(huán)
  • Windows Powershell Foreach 循環(huán)
  • Windows Powershell Do While 循環(huán)
  • Windows Powershell Switch 循環(huán)

標(biāo)簽:蘭州 湘潭 湖南 黃山 崇左 銅川 仙桃 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Windows Powershell For 循環(huán)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266