詳解for迴圈(各種用法)

2021-06-16 07:40:07 字數 2586 閱讀 6328

常見的for迴圈一般是一下**這種結構:

for(

inti =0

; i 

<

100; i++)

或者遞減的:

for(

inti 

=100

; i 

>

0; i--)

但for當然不止這樣一種用法。for的定義,()內的三段表示式,除了中間的必須產生布林型,並未對其餘兩段有所限制,只要是表示式就可以了。在lucene.net中就有好幾次這樣的用法。例如:

for(token token 

=input.next(result); token 

!=null

; token 

=input.next(result))}

這個語句和下面**的效果是一樣的:

token token;

while

((token 

=input.next(result)) 

!=null)}

其實我認為在這兩種迴圈中,第二種比第一種好理解一點。

還有這種

for(i =75

; i--

>

0; )

jjrounds[i] 

=0x80000000;

出了乙個空表示式,呵呵。其實理解一下也很簡單,和下面**的效果一樣:

for(i =75

; i 

>

0; i--)

jjrounds[i] 

=0x80000000;

for(i = 74;i > 0;i--)

又弄錯了,接受批評。應該換成下面的

for(i = 74; i >= 0;i--)

jjrounds[i] =

0x80000000;

空表示式,也是乙個表示式啊,放在這裡也不犯法。

嘿嘿,還有其他的表示式,比如:

for(

inti =0

; i 

<

length; i

++, pos++)

這個應該不難理解,第三個表示式有兩個,第乙個當然也可以有兩個

比如for (int i = 100, j = 100; i > 0 ; i--,j++)

中間的表示式要想用兩個就要加運算子了for (int i = 100, j = 100; i > 0 || j>0 ; i--,j++)

這樣就總結出三種for迴圈樣式

1、for(int i = 0;i < 100;i++)  //遞減和遞加的算一種

2、for(;true;)     //有空表示式的

3、for (int i = 100, j = 100; i > 0 || j>0 ; i--,j++)   //有多表示式的

好像就這麼多了。但是還有一種,我無法理解的表示式

for(;;)這是個 死迴圈 

無限迴圈(沒有跳出語句,才能成為死迴圈),汗!!!廬山瀑布汗啊,反正我理解不了。

嘿嘿,理解上面的表示式,基本上看別人的**就不會摸不著頭腦了。那是不是真的沒有了呢?

來試試這種

static

void

main(

string

args)

console.read();

}static

void

act()

哈哈,真是徹底被打敗了。注意:沒見過有這麼用的,純粹是實驗,應用產生的後果我不負責啊。

放上三個方法爽爽:

static

void

main(

string

args)

console.read();

}static

void

act1()

static

bool

act2()

static

bool

act3()

當然,你非要用個委託,我也沒意見:

delegate

void

bind();

class

program

console.read();

}static

void

act1()

static

bool

act2()

static

bool

act3()}

我考事件也出來了:

delegate

void

bind();

class

program

console.read();

}static

void

program_bindevent()

static

void

act1()

static

bool

act2()

static

bool

act3()}

看出來了,只要是表示式,就能使用啊!除了第二個表示式必須為空,或者布林值外,其他兩個基本沒什麼限制。第二表示式為空則是死迴圈。

各種迴圈遍歷

一 for迴圈 1 function 8 console.log demo1arr i demo1arr i 9 10 關於for迴圈,有以下幾點需要注意 二 for in 1 function 8 console.log demoarr i demoarr i 9 10 console.log 1...

Python學習筆記之For迴圈用法詳解

python 中的for迴圈 python 有兩種型別的迴圈 for 迴圈和 while 迴圈。for 迴圈用來遍歷可迭代物件。可迭代物件是每次可以返回其中乙個元素的物件,包括字串 列表和元組等序列型別,以及字典和檔案等非序列型別。還可以使用迭代器和生成器定義可迭代物件 for 迴圈示例 itera...

各種JOIN 用法

declare ta table id int,va varchar 10 declare tb table id int,vb varchar 10 insert into ta select 1,aa insert into ta select 2,bc insert into ta selec...