【问题标题】:How do I write a While loop [closed]如何编写 While 循环 [关闭]
【发布时间】:2010-09-07 17:30:22
【问题描述】:

如何编写 While 循环的语法?

C#

int i = 0; 
while (i != 10)
{ 
   Console.WriteLine(i); 
   i++; 
}

VB.Net

Dim i As Integer = 0
While i <> 10
    Console.WriteLine(i)
    i += 1
End While  

PHP

<?php
while(CONDITION)
{
//Do something here.
}
?>


<?php
//MySQL query stuff here
$result = mysql_query($sql, $link) or die("Opps");
while($row = mysql_fetch_assoc($result))
{
$_SESSION['fName'] = $row['fName'];
$_SESSION['lName'] = $row['lName'];
//...
}
?>

Python

i = 0
while i != 10:
    print i
    i += 1

【问题讨论】:

    标签: language-agnostic conditional


    【解决方案1】:

    在 PHP 中,while 循环如下所示:

    <?php
    while(CONDITION)
    {
    //Do something here.
    }
    ?>
    

    现实世界的例子可能看起来像这样

    <?php
    //MySQL query stuff here
    $result = mysql_query($sql, $link) or die("Opps");
    while($row = mysql_fetch_assoc($result))
    {
    $_SESSION['fName'] = $row['fName'];
    $_SESSION['lName'] = $row['lName'];
    //...
    }
    ?>
    

    【讨论】:

    • 是有原因的。哇。我现在感觉好多了。
    【解决方案2】:

    TCL

    set i 0
    while {$i != 10} {
        puts $i
        incr i
    }
    

    C++、C、JavaScript、Java 和无数其他类似 C 的语言看起来都与 C# 完全相同,除了它们将输出写入控制台的方式,或者可能是您创建变量 @987654322 的方式@。回答这个问题属于其他问题。

    【讨论】:

      【解决方案3】:

      这类问题可能会有一席之地,但前提是答案正确。 While 不是 C# 中的关键字。 while 是。此外,! 和 = 之间的空格无效。试试:

      int i=0; 
      while (i != 10)
      { 
          Console.WriteLine(i); 
          i++; 
      }
      

      当我在这里时,Python:

      i = 0
      while i != 10:
          print i
          i += 1
      

      【讨论】:

        猜你喜欢
        • 2013-07-24
        • 2020-08-14
        • 2015-06-26
        • 2012-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-14
        相关资源
        最近更新 更多