c# javascript
Programming languages like Javascript, C++, C#, Python provides while loops
in order to iterate over the given list, array, set etc. while loop
is a very important part of the programming language because of its functionality. In this tutorial, we will examine and compare a while loop for programming languages like Javascript, C++, C#, and Python.
诸如Javascript,C ++,C#,Python之类的编程语言提供了while loops
以遍历给定的列表,数组,集合等while loop
由于其功能而成为编程语言中非常重要的一部分。 在本教程中,我们将检查和比较Java语言,C ++,C#和Python等编程语言的while循环。
JavaScript while循环 (Javascript while loop)
Javascript provides different syntax of while
loops. The general syntax is like below.
Javascript提供了while
循环的不同语法。 通用语法如下。
唯一条件 (Only Condition)
We just need to provide condition after the while
keyword.
我们只需要在while
关键字之后提供条件。
while (CONDITION) {
CODE
}
In this example, we will increase the i
variable and check whether it is lower than 5.
在此示例中,我们将增加i
变量并检查其是否小于5。
while (i < 5){
text += "Value is " + i;
i++;
}
While Block之后的条件 (Condition After While Block)
We can also define condition checks after the while block. This will ensure that the whole block will be executed at least one time and checked after execution.
我们还可以在while块之后定义条件检查。 这将确保整个块至少执行一次并在执行后检查。
do {
CODE
}
while (CONDITION);
In this example we will first increase the i
variable and then check whether its lower than 5
在此示例中,我们将首先增加i
变量,然后检查其是否小于5
do{
text += "Value is " + i;
i++;
}while (i < 5);
C和C ++ while循环 (C and C++ while loop)
C and C++ programming languages are using very similar syntax. Their while
loop is very similar to the Javascript because Javascript derived some syntax from these languages.
C和C ++编程语言使用的语法非常相似。 它们的while
循环与Javascript非常相似,因为Javascript从这些语言派生了一些语法。
唯一条件 (Only Condition)
We just need to provide condition after the while
keyword.
我们只需要在while
关键字之后提供条件。
while (CONDITION) {
CODE
}
In this example, we will increase the i
variable and check whether it is lower than 5.
在此示例中,我们将增加i
变量并检查其是否小于5。
while (i < 5){
printf("Value is %d", i);
i++;
}
While Block之后的条件 (Condition After While Block)
We can also define condition check after the while block. This will ensure that the while block will be executed at least one time and checked after execution.
我们还可以在while块之后定义条件检查。 这将确保while块将至少执行一次并在执行后检查。
do {
CODE
}
while (CONDITION);
In this example we will first increase the i
variable and then check whether its lower than 5
在此示例中,我们将首先增加i
变量,然后检查其是否小于5
do{
printf("Value is %d", i);
i++;
}while (i < 5);
C#while循环 (C# while loop)
C# programming language uses very similar concepts of the C and C++ programming languages. We can use the following code in order to loop with a while. In this example, we will write i
variable value to the console and check whether it is lower than 5.
C#编程语言使用与C和C ++编程语言非常相似的概念。 我们可以使用以下代码来循环一段时间。 在此示例中,我们将i
变量值写入控制台,并检查其是否小于5。
using System;
class Program
{
static void Main()
{
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
}
}
做一会儿 (Do While)
We can also implement the same logic with do while
operations.
我们还可以使用do while
操作实现相同的逻辑。
using System;
class Program
{
static void Main()
{
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i<= 5);
}
}
Python while循环 (Python while loop)
Python is a bit different language from other counterparts like Javascript, C, C++, and C#. Python uses indents as block specifiers so we will start while block with 3 spaces. In this example, we will check the variable i
if it is lower than 5 and then print it.
Python与Javascript,C,C ++和C#等其他语言有点不同。 Python使用缩进作为块说明符,因此我们将从3个空格开始。 在此示例中,我们将检查变量i
是否小于5,然后进行打印。
i = 1
while i < 5:
print(i)
i += 1
翻译自: https://www.poftut.com/while-loop-in-java-javascript-cpp-csharp-python-programming-languages/
c# javascript