2011-04-16 105 views
1

转换循环总是像这样工作吗?或者有一些情况不是?当我在考试中解决这样的问题时,最快的方法是什么?转换循环(Java初学者问题)

For循环:

for(xx;yy;zz) 
    { 
     aa 
    } 

While循环:

xx 
    while(yy) 
    { 
     aa 
     zz 
    } 

做,当:

 xx 
     do 
     { 
     aa 
     zz 
     } 
    while(yy) 
+0

你的意思是'aa'而不是'yy'在while循环体内吗? – jonsca 2011-04-16 21:47:45

回答

5

你有几个失误。特别是do-while循环是不正确的,因为它总是执行至少一次迭代,而对于forwhile循环而言则不是这种情况。

for(x(); y(); z()) 
{ 
    a(); 
} 

x(); 
while(y()) 
{ 
    a(); 
    z(); 
} 

x(); 
if(y()) 
{ 
    do 
    { 
    a(); 
    z(); 
    } while(y()) 
} 
+0

你能告诉我一个将for-loop转换为do-while的例子吗?你是对的,我在做Do-while时总会犯错误。非常感激。 – 2011-04-16 22:29:06

+0

@Mohammad:在这个答案中有一个do-while-loop的例子,它看起来等同于顶部的for-loop。 – 2011-04-16 23:15:49

+0

我不认为你的理解是正确的。一段时间保证至少执行一次代码。 – 2011-04-17 00:02:54

0

一个for声明和while陈述或do-while语句之间的唯一区别是,for还指定初始化和迭代的增量。
例如,让我们的Iterator这在Java可以被用于通过一个载体或类似的东西(如链接列表)迭代:

//we already have a variable v whom type implements List interface; 
for (Iterator vectorIterator = v.iterator(); vectorIterator.hasNext();) { 
    //I left blank after the last ";" because there should come 
    // the increment statement, and I need to store the .next() return value. 

    int current = vectorIterator.next(); 
} 

这里是while版本:

Iterator vectorIterator = v.iterator(); 
while (vectorIterator.hasNext()) { 
    int current = vectorIterator.next(); 
} 

do-whilewhile版本非常相似。

我给你提供了这个更复杂的例子,告诉你任何你用while做的事情,无论多么复杂,你也可以用for做。

EDIT:所述do-while语句存在作为替代while,条件是所述循环​​条件是在与迭代的检查的唯一区别。所以,你可以做一个相当于上面的例子中使用do-while这样的:

Iterator vectorIterator = v.iterator(); 
if (vectorIterator.hasNext()) { 
    do { 
     int current = vectorIterator.next(); 
    } while (vectorIterator.hasNext()); 
} 

但在这样做没有任何目的。您应该使用每个工具来达到自己的目的。

+1

还有一点就是,在执行任何形式的条件检查之前,do-while将通过其代码执行一次。 – Doug 2011-04-16 22:13:13

+0

谢谢。你能向我展示一个Do-While的例子,因为这是我经常犯错误的地方 – 2011-04-16 22:27:26

+0

你发布的这个do-you是完美的。你只需要记住,在检查它是否应该继续之前,它会经历一次代码。所以'while(false){a()}'永远不会运行一个(),'do {a()} while(false)'会运行一次。 – Doug 2011-04-16 22:35:25

0

其他成员已经涵盖了执行语义。但我想添加变量范围语义。

在Java中,你可以这样做:

for (Iterator<String> i = localList.iterator(); i.hasNext();) { 
    String element = i.next(); 
    if (...) { 
    i.remove(); 
    } 
} 

for (Iterator<String> i = importList.iterator(); i.hasNext();) { 
    String element = i.next(); 
    if (...) { 
    i.remove(); 
    } 
} 

其中变量 'i' 是使用了两次。

为了翻译,要同时用正确的作用域循环,你需要使用额外的代码块:

{ 
    Iterator<String> i = localList.iterator(); 
    while (i.hasNext()) { 
    String element = i.next(); 
    if (...) { 
     i.remove(); 
    } 
    } 
} 

{ 
    Iterator<String> i = importList.iterator(); 
    while (i.hasNext()) { 
    String element = i.next(); 
    if (...) { 
     i.remove(); 
    } 
    } 
} 

注意额外{ ... }如何允许您使用相同的变量名。

尽管Java在C++等语言中不那么重要,但范围界定除了能够使用相同的变量名之外,还具有重要的影响。对象破坏,IIA(实例化是获取)等受附加块的影响。

0
// This class demonstrates the transliteration 
// between while, for, and for-each loop syntax 
public class LoopTest { 

    // This method definition showcases do-while-loop syntax 
    public void doWhileLoop(Person[] people) { 
     int i=0; 
     do { 
      Person p = people[i]; 
      System.out.println(p.getName()); 
      i++; 
     }while(i<people.length); 
    } 

    // This method definition showcases while-loop syntax 
    public void whileLoop(Person[] people) { 
     int i=0; 
     while(i<people.length) { 
      Person p = people[i];    
      System.out.println(p.getName());    
      i++; 
     } 
    } 

    // This method definition showcases for-loop syntax  
    public void forLoop(Person[] people) { 
     for(int i=0; i<people.length; i++) { 
      Person p = people[i];    
      System.out.println(p.getName());    
     } 
    } 

    // This method definition showcases for-each-loop syntax   
    public void forEachLoop(Person[] people) { 
     for(Person p : people) { 
      System.out.println(p.getName()); 
     } 
    } 
}