Topic: 关于FOR、WHILE循环的讨论!

  Print this page

1.关于FOR、WHILE循环的讨论! Copy to clipboard
Posted by: jameszhang
Posted on: 2004-11-11 16:50

for (boolean more = true; more; ) {
....
if (....) {
....
more = false;
}
.....
}

2.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: Jove
Posted on: 2004-11-11 17:17

倒,为什么不用while(flag)呢, 真够BT Big Smile

3.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2004-11-11 18:44

For loop is always better than while loop.

4.Re:这样写FOR循环的举手!! [Re: HenryShanley] Copy to clipboard
Posted by: 烂泥
Posted on: 2004-11-11 20:31

HenryShanley wrote:
For loop is always better than while loop.

请指教哦

5.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2004-11-11 21:27

ArrayList al=new ArrayList();
Iterator it=al.iterator();
while(it.hasNext()){
//.......it.next();
}

for(Iterator it=al.iterator();it.hasNext() ; ){
//.......it.next();
}
//局部变量要尽量使自己变成黑箱black box,从这点上来看我们总认为for要比while好一点。

6.Re:这样写FOR循环的举手!! [Re: HenryShanley] Copy to clipboard
Posted by: why
Posted on: 2004-11-11 22:47

HenryShanley wrote:
ArrayList al=new ArrayList();
Iterator it=al.iterator();
while(it.hasNext()){
//.......it.next();
}

for(Iterator it=al.iterator();it.hasNext() ; ){
//.......it.next();
}
//局部变量要尽量使自己变成黑箱black box,从这点上来看我们总认为for要比while好一点。

{
Iterator it=al.iterator();
while(it.hasNext()) {
//.......it.next();
}
}

has the same effect. Smile
while is good syntatic sugar for clarity.
Anyway, I like to use for than while.

7.Re:这样写FOR循环的举手!! [Re: why] Copy to clipboard
Posted by: edgeloner
Posted on: 2004-11-12 13:04

我iterator一贯喜欢用for,while在实际中我几乎没有怎么用到,能用for我一般不去用while。for感觉要干净些

8.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-11-13 00:52

我比较喜欢用FOR,几乎从来不用while.

9.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: raulmadrid
Posted on: 2004-11-13 12:06

for( ; ; ) {
}
不就可以了么

10.Re:这样写FOR循环的举手!! [Re: raulmadrid] Copy to clipboard
Posted by: qingbo777
Posted on: 2004-11-13 17:30

raulmadrid wrote:
for( ; ; ) {
}
不就可以了么

个人认为,用这种方法要跳出的话必须用break,不如人家的方法有节奏感.

11.Re:这样写FOR循环的举手!! [Re: qingbo777] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-13 18:47

qingbo777 wrote:
我比较喜欢用FOR,几乎从来不用while.

可是有的时候用FOR是不是比较明白

比方说线程里的那个
public void run(){
while(true){
// your thread contents
}
}


是不是这样好点呢,还有Stream里边

while(myStream.available()>certainValue){
myStream.read(yourBuffer);
}


我觉得用while好像更自然易懂一些
:)不知道大家编写程序时,还有什么习惯呢

12.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: zangweiren
Posted on: 2004-11-13 21:27

我觉得只要不影响效率,用哪种方法就是大家的习惯问题了

13.Re:这样写FOR循环的举手!! [Re: jameszhang] Copy to clipboard
Posted by: dingligang
Posted on: 2004-11-15 01:59

Effective Java 第七章第29条(我看的是中文版)讲到了这个问题。
for和while实现的功能差不多,但是for相对while至少有2点好处:
1)for可以比while少一行

for:
for(Iterator i=c.iterator();i.hasNext() ; ){
//do sth.
}

but while
Iterator i = c.iterator();
while(i.hasNext()){
//do sth.
}


2)for可以使局部变量的作用域最小化
还是上面那个例子,Iterator i在for中,属于for的局部变量,不暴露给外面的代码,而while则不同,后续的代码可能会继续错误的使用(引用)这个变量
如:
if(i!=null){ //this i is not that i Sad, but the compiler can not detect it!!
//do sth.
}

14.Re:关于FOR、WHILE循环的讨论! [Re: jameszhang] Copy to clipboard
Posted by: wuyongjin
Posted on: 2004-11-15 13:53

哈哈,大家 的解释都不错啊
还是看实际啊 问题时候在说啊吧!

15.Re:关于FOR、WHILE循环的讨论! [Re: jameszhang] Copy to clipboard
Posted by: myster
Posted on: 2004-11-19 16:46

我个人对for循环有种亲切感。

16.Re:关于FOR、WHILE循环的讨论! [Re: jameszhang] Copy to clipboard
Posted by: loverzhongping
Posted on: 2004-11-22 16:08

I like use for ,and I never use while!

17.Re:关于FOR、WHILE循环的讨论! [Re: jameszhang] Copy to clipboard
Posted by: zcjl
Posted on: 2004-11-23 17:54

看不出跟楼主的代码相比:
while (true) {
if (....) {
....
break;
}
.....
}

不好在哪里?
尽管我也多数用for,而不是while

只因为 Effective Java 里的一个Item,就否决了while存在的必要
是否太极端了些?

18.Re:关于FOR、WHILE循环的讨论! [Re: jameszhang] Copy to clipboard
Posted by: cicy02
Posted on: 2005-02-18 15:45

用C和C++时,比较喜欢用for;
现在偏好while,尤其是do...while;


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923