程式語言中,While迴圈(英語:while loop)是一種控制流程陳述。利用一個返回結果為布林值的表達式作為循環條件,當這個表達式的返回值為“真”時,則反覆執行迴圈內的程式碼;若表達式的返回值為“假”,則結束執行迴圈內的代碼,繼續執行迴圈下面的代碼。

因為While迴圈在區塊內代碼被執行之前,先檢查陳述是否成立,因此這種控制流程通常被稱為是一種前測試迴圈(pre-test loop)。相對而言Do While迴圈,是在迴圈區塊執行結束之後,再去檢查陳述是否成立,被稱為是後測試迴圈。

程式範例

编辑
 
while 迴圈

VB

编辑
'這是一個用While迴圈的例子
dim counter as Integer
dim Tick as Integer
counter=5
tick=1
Print "Start"
while counter>0
counter=counter-tick
'迴圈語句
Wend
Print "End"

C/C++

编辑
unsigned int counter = 5;
unsigned long factorial = 1;

while (counter > 0)
{
  factorial *= counter--;    /*當滿足迴圈條件(本例為:counter > 0)時會反覆執行該條語句 */
}

printf("%lu", factorial);

Java

编辑
public static void main(str args[]){
    while true{
        System.out.println("Hello World!") //因為條件已經固定為常量true,所以就會不斷執行迴圈內的語句
    }
    int counter = 0 ;
    while counter<5{
        System.out.println("已經運行了"+counter+"次")  //因為條件限定為counter不大於5,所以在counter不大於5的情況下會不斷重複迴圈中的内容
        counter++;
    }
}

Python語言

编辑
a = 0
while a <= 10 :   #如果a沒有大於10就執行
    a = a+1
    print(a)

另见

编辑

📚 Artikel Terkait di Wikipedia

类型双关

number into whatever it is as a long integer (32 bit), which will not be the same and may be incompatible with the long integer value on some systems. These

数据类型 (C语言)

能使用不同的数据位长和范围。请参考具体的参考。 在标准头文件limits.h 和 float.h中说明了基础数据的长度。float,double和long double的范围就是在IEEE 754标准中提及的典型数据。另外,C99添加了新的复数类型,C11添加了原子类型,它们不在本条目讨论范围内。关

C++类

char, short, long...)那样子使用。 struct A { unsigned a:2; // 可以存储0-3的数字,占据一个int前2 bit的空间. unsigned b:3; // 可以存储0-7的数字,占据之后3 bit的空间. unsigned :0; // 移动到下一个内置类型的末尾

埃拉托斯特尼筛法

eratosthenesSieve(unsigned long long int N, int verbose) { // prime numbers are positive, better to use largest unsiged integer unsigned long long int i, j, total;

Visual Basic .NET

XML注释,可以被NDoc等工具处理而自动生成文档; 操作符重载; 支持在其它语言中已经广泛使用的unsigned integer数据类型; 支持在许多C衍生编程语言已提供的复合操作符如:"+=", "-="; IsNot专利:If Not X Is Y表达式被转换成If

字面常量 (C语言)

unsigned int来表示该字面量。 整型字面量可以使用后缀u U ul UL ull ULL明确表示各种无符号整型;使用后缀l L ll LL表示该字面量至少为long或为long long型。 C++14引入了千分位分隔符。例如: auto integer_literal =

C++17

seq> struct my_integer_sequence { // Implementation here ... }; // Explicitly pass type `int` as template argument. auto seq = std::integer_sequence<int

LEB128

CHAR_BITS; /* no. of bits in signed integer */ /* will be assigned inside the do-while loop, but referenced afterwards */ unsigned char byte; do { byte =