For faster navigation, this Iframe is preloading the Wikiwand page for 迴圈計數器.

迴圈計數器

軟體工程中,迴圈計數器或是迴圈變數是指for迴圈中控制執行次數的變數。命名為計數器的原因是這個變數的值一般會是整數,而且在迴圈進行中,變數的值也以遞增或遞減的方式變化(例如由0開始,每次加1,到10時結束)。

在每次迴圈迭代時,迴圈計數器都會變化,因此每次迭代時迴圈計數器都會不同,在for迴圈中會依迴圈計數器決定迴圈是否要繼續,或者結束迴圈,執行後續的程式。

常用的變數命名原則英语identifier naming convention會將迴圈計數器用i, jk等變數名稱表示。最外圈的迴圈計數器變數為i,再內層迴圈計數器變數為j,以此類推,有些程式的迴圈計數器順序會相反,用i表示最外圈的迴圈計數器變數。一般認為此命名原則是為了符合早期FORTRAN,因為這些字母開頭的變數若不特別指定型態,即為整數型態,而且迴圈計數器只是暫時使用,這些簡短的名字適合迴圈計數器的特性。這也影響中許多數學符號中的參數用i, jk表示。

舉例

[编辑]

以下是一個C語言巢狀for迴圈的例子,其內層的迴圈計數器為j,外層的迴圈計數器為i。

for(i = 0; i < 100; i++)//外層迴圈,計數器變數為i
   for(j = i; j < 100; j++)//內層迴圈,計數器變數為j
      some_function(i, j);

外部連結

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
迴圈計數器
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?