超导是什么意思| 罗汉果泡水有什么好处| 上梁不正下梁歪是什么意思| 刚怀孕需要注意什么| na是什么牌子| 米糠是什么| 参透是什么意思| 安溪铁观音属于什么茶| 神经紊乱会出现什么症状| 甲苯对人体有什么危害| 柯是什么意思| blissful是什么意思| pd是什么意思| 稀饭配什么菜好吃| 肺活量大有什么好处| 晚生是什么意思| 什么是翘舌音| 伤官伤尽是什么意思| 永浴爱河是什么意思| 鼻血流不停是什么原因| 脂肪瘤挂什么科| 血热吃什么中成药| 伤口恢复吃什么好得快| 全血是什么| 两败俱伤是什么意思| 舌苔黄是什么原因| 夏天煲什么汤最好| 缺铁性贫血吃什么药| 吃优甲乐不能吃什么| 辅酶q10是什么| 尿常规是检查什么的| 手脱皮缺什么维生素| 东北易帜是什么意思| 拔气罐有什么好处| 高脂血症是什么病| 去火喝什么茶最好| 慢性萎缩性胃炎伴糜烂吃什么药| 外甥女是什么关系| 杏有什么作用和功效| 三刀六洞什么意思| 宝宝拉肚子吃什么| 胸部ct平扫能检查出什么| 鱼油有什么用| 为什么减肥一直减不下来| 太极贵人是什么意思| 女性做B超挂什么科| 鸣是什么家禽| 什么的食物| 小孩风寒感冒吃什么药| 氟哌酸又叫什么名字| 嘴唇干裂是什么原因引起的| 玻璃心是什么意思| 为什么怀孕前三个月不能说| 雷震子是什么神| 什么的雕像| 1948年属什么生肖| 跳闸是什么原因| 酒酿蛋什么时候吃效果最好| 生理性是什么意思| 暴饮暴食容易得什么病| 姝姝是什么意思| 两岁宝宝不开口说话是什么原因| 煤油是什么油| 试管婴儿是什么| 脑梗是什么原因引起的| 小姑独处是什么意思| 不排大便是什么原因| 不倒翁是什么意思| 什么是阴虚| 米醋和陈醋有什么区别| 腿不自觉的抖是什么原因| 倒挂金钩什么意思| 细小是什么病| 酒吧营销是做什么的| 前方起飞是什么意思| 同甘共苦什么意思| 什么是腺肌症| 肠胃不舒服挂什么科| 射手什么象星座| 什么是半月板损伤| 青色是什么颜色| 人为什么要读书| 舌强语謇是什么意思| 竖小拇指什么意思| 血压低有什么症状| 得了梅毒会有什么症状| 胆酷醇高有什么危害| 唐筛都检查什么| kcal是什么意思| 痛风反复发作什么原因| 北京市长是什么级别| 马蹄粉是什么粉| 心跳过快是什么原因| 生命线分叉是什么意思| 齐天大圣是什么意思| 补刀什么意思| hk是什么意思| 白露是什么季节的节气| 为什么人会打喷嚏| 什么是海藻糖| 钝是什么意思| 日照香炉生紫烟是什么意思| ear什么意思| 长宽高用什么字母表示| 静脉曲张做什么检查| 宫缩是什么原因引起的| 杀鸡吓什么| 贫血吃什么补的快| 舌头有麻木感什么原因| 待业是什么意思| 卯戌相合发生什么| 验孕棒一深一浅代表什么| 广义货币m2是什么意思| 左室高电压是什么意思| 毒瘾发作有什么症状| 肾上腺分泌什么激素| 部队政委是什么级别| 急性中耳炎吃什么药| 脑出血什么症状| 静脉曲张吃什么食物好| 梗塞是什么意思| 安之若素是什么意思| heineken是什么啤酒| 男同性恋叫什么| 心里紧张吃什么药缓解| 睡觉时头晕是什么原因| 身上为什么会起湿疹| ufc什么意思| 自费是什么意思| ckd3期是什么意思| 一个月来两次月经是什么原因| 什么水果对眼睛好| 条状血流信号是什么意思| 男人手大代表什么| 米娜桑什么意思| 人的本质属性是什么| 闹觉是什么意思| 狗为什么会咬人| 拆封是什么意思| 吃什么水果容易减肥| 三个土是什么字怎么读| 感冒为什么会流眼泪| 腔隙脑梗吃什么药最好| 双鱼座是什么象星座| 吃什么养颜美容抗衰老| 64年属什么的| 茅台酒为什么这么贵| bic是什么意思| 囊性灶是什么意思| 饱和脂肪是什么| 右肺下叶纤维化灶是什么意思| 用什么自慰| 减肥喝什么茶| 三维彩超主要检查什么| 啤酒加味精有什么作用| 深海鱼油起什么作用| 甘草泡水喝有什么功效| 碘伏和酒精有什么区别| 下关沱茶属于什么茶| 肋骨疼是什么原因| 舌苔黄腻吃什么药| 便溏什么意思| 海鲜过敏吃什么药| 为什么会脱发| dennis什么意思| 家里有壁虎是什么征兆| 克服是什么意思| 胆切除后吃什么好| 乳腺疼挂什么科| 什么东西有脚却不能走路| 激素6项检查是些什么| mcg是什么意思| 红色药片一般是什么药| 猫驱虫药什么牌子好| 中国最高学历是什么| 中医治未病是什么意思| 指甲黑是什么原因| 肩膀骨头响是什么原因| 白细胞低吃什么药可以增加白细胞| 姐姐的女儿叫什么称呼| 打马虎眼是什么意思| 老婆饼是什么馅| 退行性变是什么意思| 脾肾阳虚吃什么中成药| 湖南简称什么| 吃什么药可以延长性功能| 头晕想吐是什么症状| 女人的第二张脸是什么| 陈赫为什么离婚| ella是什么意思| 避重就轻什么意思| 梦见老公不理我是什么意思| 每天放屁多是什么原因| 81年属鸡的是什么命| 碳水化合物对人体有什么作用| 照字五行属什么| 颈椎增生吃什么药| 娃娃鱼吃什么| 钥匙像什么| 新生儿为什么有黄疸| 折耳根是什么| 铁瓷是什么意思| silence是什么意思| 酉时五行属什么| 梦见古墓是什么意思| 香蕉是什么季节的| 百年老枞属于什么茶| 罗非鱼是什么鱼| 六味地黄丸有什么副作用吗| 龙眼什么时候成熟| 小肚子一直疼是什么原因| 茯茶属于什么茶| 桐字五行属什么| 谷丙转氨酶高吃什么药可以降下来| 梦见抓螃蟹是什么征兆| 破损是什么意思| 投放是什么意思| 脑萎缩是什么意思| 小孩心肌炎有什么症状| 芒果有什么营养| 丑时属什么| 元肉是什么| 长绒棉和全棉什么区别| 心脏有个小洞叫什么病| 3n是什么意思| 来福是什么意思| 放臭屁吃什么药| 孤僻是什么意思| 旧衣服属于什么垃圾| 小节是什么意思| 正气是什么意思| 什么富什么车| 丝状疣是什么样子图片| rgp是什么| 高钾血症是什么原因引起的| 什么是酵素| 阴道内痒是什么原因| colorful是什么牌子| 淋菌性尿道炎吃什么药| 后腰左侧疼痛是什么原因男性| 肚子胀吃什么| 小腿肌肉疼是什么原因| 绣球花什么时候修剪| d二聚体偏高说明什么| 左上眼皮跳是什么预兆| 碱性磷酸酶偏高吃什么能降下来呢| 低血压适合吃什么食物| 化疗中的病人应该吃什么| 本心是什么意思| 腰间盘突出吃什么药好| 女性多吃什么滋补卵巢| 眩晕是怎么回事是什么原因引起| 用什么可以全身美白| 又什么又什么的草地| 黄体酮不足吃什么| 为什么午睡起来会头疼| 一直不射精是什么原因| 肾结石有什么症状哪里疼| 雅戈尔男装什么档次| 女性分泌物增多发黄是什么原因| 单身公寓是什么意思| 颈椎用什么字母表示| 苯甲酸钠是什么| 什么话是世界通用的| 百度

这是有多长啊 新款华颂7加长版将亮相上海车展

百度 “我坚信这是完全可以做到的。

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

While loop flow diagram

Overview

edit

The while construct consists of a block of code and a condition/expression.[1] The condition/expression is evaluated, and if the condition/expression is true,[1] the code within all of their following in the block is executed. This repeats until the condition/expression becomes false. Because the while loop checks the condition/expression before the block is executed, the control structure is often also known as a pre-test loop. Compare this with the do while loop, which tests the condition/expression after the loop has executed.

For example, in the languages C, Java, C#,[2] Objective-C, and C++, (which use the same syntax in this case), the code fragment

int x = 0;

while (x < 5) {
    printf ("x = %d\n", x);
    x++;
}

first checks whether x is less than 5, which it is, so then the {loop body} is entered, where the printf function is run and x is incremented by 1. After completing all the statements in the loop body, the condition, (x < 5), is checked again, and the loop is executed again, this process repeating until the variable x has the value 5.

It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that controls termination of the loop. For example:

while (true) {
    // do complicated stuff
    if (someCondition)
        break;
    // more stuff
}

Demonstrating while loops

edit

These while loops will calculate the factorial of the number 5:

ActionScript 3

edit
var counter: int = 5;
var factorial: int = 1;

while (counter > 1) {
    factorial *= counter;
    counter--;
}

Printf("Factorial = %d", factorial);
with Ada.Integer_Text_IO;

procedure Factorial is
    Counter   : Integer := 5;
    Factorial : Integer := 1;
begin
    while Counter > 0 loop
        Factorial := Factorial * Counter;
        Counter   := Counter - 1;
    end loop;

    Ada.Integer_Text_IO.Put (Factorial);
end Factorial;
counter  5
factorial  1

:While counter > 0
    factorial × counter
    counter - 1
:EndWhile

?  factorial

or simply

!5

AutoHotkey

edit
counter := 5
factorial := 1

While counter > 0
    factorial *= counter--

MsgBox % factorial

Small Basic

edit
counter = 5    ' Counter = 5
factorial = 1  ' initial value of variable "factorial"

While counter > 0
    factorial = factorial * counter
    counter = counter - 1
    TextWindow.WriteLine(counter)
EndWhile

Visual Basic

edit
Dim counter As Integer = 5    ' init variable and set value
Dim factorial As Integer = 1  ' initialize factorial variable

Do While counter > 0
    factorial = factorial * counter
    counter = counter - 1
Loop     ' program goes here, until counter = 0

'Debug.Print factorial         ' Console.WriteLine(factorial) in Visual Basic .NET

Bourne (Unix) shell

edit
counter=5
factorial=1
while [ $counter -gt 0 ]; do
    factorial=$((factorial * counter))
    counter=$((counter - 1))
done

echo $factorial

C, C++

edit
int main() {
    int count = 5;
    int factorial = 1;

    while (count > 1)
        factorial *= count--;

    printf("%d", factorial);
}

ColdFusion Markup Language (CFML)

edit

Script syntax

edit
counter = 5;
factorial = 1;

while (counter > 1) {
    factorial *= counter--;
}

writeOutput(factorial);

Tag syntax

edit
<cfset counter = 5>
<cfset factorial = 1>
<cfloop condition="counter GT 1">
    <cfset factorial *= counter-->
</cfloop>
<cfoutput>#factorial#</cfoutput>

Fortran

edit
program FactorialProg
    integer :: counter = 5
    integer :: factorial = 1

    do while (counter > 0)
        factorial = factorial * counter
        counter = counter - 1
    end do

    print *, factorial
end program FactorialProg

Go has no while statement, but it has the function of a for statement when omitting some elements of the for statement.

counter, factorial := 5, 1

for counter > 1 {
	counter, factorial = counter-1, factorial*counter
}

Java, C#, D

edit

The code for the loop is the same for Java, C# and D:

int counter = 5;
int factorial = 1;

while (counter > 1)
    factorial *= counter--;

JavaScript

edit
let counter = 5;
let factorial = 1;

while (counter > 1)
    factorial *= counter--;

console.log(factorial);
counter = 5
factorial = 1

while counter > 0 do
  factorial = factorial * counter
  counter = counter - 1
end

print(factorial)

MATLAB, Octave

edit
counter = 5;
factorial = 1;

while (counter > 0)
    factorial = factorial * counter;      %Multiply
    counter = counter - 1;                %Decrement
end

factorial

Mathematica

edit
Block[{counter=5,factorial=1},  (*localize counter and factorial*)
    While[counter>0,            (*While loop*)
        factorial*=counter;     (*Multiply*)
        counter--;              (*Decrement*)
    ];

    factorial
]

Oberon, Oberon-2, Oberon-07, Component Pascal

edit
MODULE Factorial;
IMPORT Out;
VAR
    Counter, Factorial: INTEGER;
BEGIN
    Counter := 5;
    Factorial := 1;

    WHILE Counter > 0 DO
        Factorial := Factorial * Counter;
        DEC(Counter)
    END;

    Out.Int(Factorial,0)
END Factorial.

Maya Embedded Language

edit
int $counter = 5;
int $factorial = 1;

int $multiplication;

while ($counter > 0) {
    $multiplication = $factorial * $counter;

    $counter -= 1;

    print("Counter is: " + $counter + ", multiplication is: " + $multiplication + "\n");
}
var
  counter = 5            # Set counter value to 5
  factorial = 1          # Set factorial value to 1

while counter > 0:       # While counter is greater than 0
    factorial *= counter # Set new value of factorial to counter.
    dec counter          # Set the counter to counter - 1.

echo factorial

Non-terminating while loop:

while true:
  echo "Help! I'm stuck in a loop!"

Pascal

edit

Pascal has two forms of the while loop, while and repeat. While repeats one statement (unless enclosed in a begin-end block) as long as the condition is true. The repeat statement repetitively executes a block of one or more statements through an until statement and continues repeating unless the condition is false. The main difference between the two is the while loop may execute zero times if the condition is initially false, the repeat-until loop always executes at least once.

program Factorial1;
var
    Fv: integer;

    procedure fact(counter:integer);
    var
        Factorial: integer;

    begin
         Factorial := 1;

         while Counter > 0 do
         begin
             Factorial := Factorial * Counter;
             Counter := Counter - 1
         end;

         WriteLn(Factorial)
     end;

begin
    Write('Enter a number to return its factorial: ');
    readln(fv);
    repeat
         fact(fv);
         Write('Enter another number to return its factorial (or 0 to quit): ');
     until fv=0;
end.

Perl

edit
my $counter   = 5;
my $factorial = 1;

while ($counter > 0) {
    $factorial *= $counter--; # Multiply, then decrement
}

print $factorial;

While loops are frequently used for reading data line by line (as defined by the $/ line separator) from open filehandles:

open IN, "<test.txt";

while (<IN>) {
    print;
}

close IN;
$counter = 5;
$factorial = 1;

while ($counter > 0) {
    $factorial *= $counter--; // Multiply, then decrement.
}

echo $factorial;

PL/I

edit
declare counter   fixed initial(5);
declare factorial fixed initial(1);

do while(counter > 0)
    factorial = factorial * counter;
    counter = counter - 1;
end;

Python

edit
counter = 5                           # Set the value to 5
factorial = 1                         # Set the value to 1

while counter > 0:                    # While counter(5) is greater than 0
    factorial *= counter              # Set new value of factorial to counter.
    counter -= 1                      # Set the counter to counter - 1.

print(factorial)                      # Print the value of factorial.

Non-terminating while loop:

while True:
    print("Help! I'm stuck in a loop!")

Racket

edit

In Racket, as in other Scheme implementations, a named-let is a popular way to implement loops:

#lang racket
(define counter 5)
(define factorial 1)
(let loop ()
    (when (> counter 0)
        (set! factorial (* factorial counter))
        (set! counter (sub1 counter))
        (loop)))
(displayln factorial)

Using a macro system, implementing a while loop is a trivial exercise (commonly used to introduce macros):

#lang racket
(define-syntax-rule (while test body ...) ; implements a while loop
    (let loop () (when test body ... (loop))))
(define counter 5)
(define factorial 1)
(while (> counter 0)
    (set! factorial (* factorial counter))
    (set! counter (sub1 counter)))
(displayln factorial)

However, an imperative programming style is often discouraged in Scheme and Racket.

Ruby

edit
# Calculate the factorial of 5
i = 1
factorial = 1

while i <= 5
  factorial *= i
  i += 1
end

puts factorial

Rust

edit
fn main() {
    let mut counter = 5;
    let mut factorial = 1;

    while counter > 1 {
        factorial *= counter;
        counter -= 1;
    }

    println!("{}", factorial);
}

Smalltalk

edit

Contrary to other languages, in Smalltalk a while loop is not a language construct but defined in the class BlockClosure as a method with one parameter, the body as a closure, using self as the condition.

Smalltalk also has a corresponding whileFalse: method.

| count factorial |
count := 5.
factorial := 1.
[count > 0] whileTrue:
    [factorial := factorial * count.
    count := count - 1].
Transcript show: factorial

Swift

edit
var counter = 5                 // Set the initial counter value to 5
var factorial = 1               // Set the initial factorial value to 1

while counter > 0 {             // While counter(5) is greater than 0
    factorial *= counter        // Set new value of factorial to factorial x counter.
    counter -= 1                // Set the new value of counter to  counter - 1.
}

print(factorial)                // Print the value of factorial.
set counter 5
set factorial 1

while {$counter > 0} {
    set factorial [expr $factorial * $counter]
    incr counter -1
}

puts $factorial
int counter = 5;
int factorial = 1;

while (counter > 1)
    factorial *= counter--;

printf("%d", factorial);

PowerShell

edit
$counter = 5
$factorial = 1

while ($counter) {
    $factorial *= $counter--
}

$factorial

While (language)

edit

While[3] is a simple programming language constructed from assignments, sequential composition, conditionals, and while statements, used in the theoretical analysis of imperative programming language semantics.[4][5]

C := 5;
F := 1;

while (C > 1) do
    F := F * C;
    C := C - 1;

See also

edit

References

edit
  1. ^ a b "The while and do-while Statements (The Java Tutorials > Learning the Java Language > Language Basics)". Dosc.oracle.com. Retrieved 2025-08-07.
  2. ^ "while (C# reference)". Msdn.microsoft.com. Retrieved 2025-08-07.
  3. ^ "Chapter 3: The While programming language" (PDF). Profs.sci.univr.it. Retrieved 2025-08-07.
  4. ^ Flemming Nielson; Hanne R. Nielson; Chris Hankin (1999). Principles of Program Analysis. Springer. ISBN 978-3-540-65410-0. Retrieved 29 May 2013.
  5. ^ Illingworth, Valerie (11 December 1997). Dictionary of Computing. Oxford Paperback Reference (4th ed.). Oxford University Press. ISBN 9780192800466.
口苦口干口臭吃什么药 法医是干什么的 胃胀气适合吃什么食物 什么降胆固醇 什么是阴茎
高血压高血糖挂什么科 鹿晗女朋友叫什么名字 天蝎和什么星座最配对 子什么意思 吃什么食物治便秘
相什么成趣 芳心暗许什么意思 学生吃什么补脑子增强记忆力最快 比干是什么神 大学体检都检查什么
猪的五行属什么 月子里生气有什么危害 鹿角粉有什么功效和作用 宫商角徵羽是什么意思 jojo什么意思
底线是什么意思hcv8jop1ns9r.cn 离是什么生肖hcv8jop0ns0r.cn 什么手机好用hcv9jop8ns3r.cn 身上长扁平疣是什么原因造成的hcv9jop2ns5r.cn 打扰了是什么意思hcv9jop5ns8r.cn
毕业是什么意思hcv9jop0ns6r.cn 急性胃炎吃什么药好hcv9jop5ns8r.cn 借条和欠条有什么区别96micro.com 蚊虫叮咬红肿用什么药hcv8jop9ns8r.cn 政法委是干什么的yanzhenzixun.com
开天辟地是什么生肖hcv8jop0ns3r.cn 辟谷有什么好处hcv9jop4ns5r.cn 吃什么减脂肪hcv8jop6ns5r.cn 阿斯伯格综合征是什么wzqsfys.com 青城之恋是什么生肖shenchushe.com
总梦到一个人说明什么hcv8jop5ns2r.cn 吃什么东西排酸最快hcv9jop3ns5r.cn 什么是事故隐患hcv9jop3ns7r.cn 早上打喷嚏是什么预兆hcv8jop9ns6r.cn 二月四号是什么星座hcv8jop5ns0r.cn
百度