阳历2月份是什么星座| 什么水果含糖量最低| nine什么意思| 6月2日是什么星座| 为什么腋下会长小肉揪| 什么方法可以降血压| 眼睛发黄是什么原因| bigbang什么意思| 青少年嗜睡是什么原因| 本科是什么意思| dave是什么意思| 舌头辣辣的是什么原因| 胸外扩是什么样子| 吃什么奶水多| 红楼梦主要讲了什么| 吃什么东西化痰| 月经吃什么| 生普属于什么茶| 什么光什么色| 塌陷是什么意思| 吃荔枝有什么好处| 一五行属性是什么| 一个口一个我念什么| 清宫后可以吃什么水果| 普拉提是什么意思| 龙生九子是什么生肖| 决明子泡水喝有什么功效| 连坐是什么意思| 口腔医学技术是干什么的| 九品芝麻官是什么级别| 吃粽子是什么节日| 刻骨铭心是什么意思| 子宫长什么样子图片| 护理是什么意思| 山药为什么煮熟了也麻口| 便秘吃什么快速通便| 血脂指的是什么| 15号来月经排卵期是什么时候| 再生障碍性贫血是什么病| 灵芝搭配什么煲汤最好| 肺纤维化有什么症状| 肋骨属于什么骨| 抗环瓜氨酸肽抗体高是什么意思| 猴子偷桃是什么生肖| 双侧肾盂无分离是什么意思| 打下巴用什么玻尿酸最好| 纯净水和矿泉水有什么区别| 吸血鬼怕什么| 栀子对妇科有什么功效| 生殖科是检查什么的| 家去掉一点念什么| 低钾是什么原因引起的| 腹泻吃什么消炎药| 牛骨煲汤搭配什么最好| 江与河有什么区别| 头晕想睡觉是什么原因| 朱元璋是什么星座| 吃什么东西下火| 肺结核是什么| 什么的意思| 尿的酸碱度能说明什么| 螃蟹用什么呼吸| 什么病会引起牙疼| 声泪俱下是什么意思| 一个提手一个京念什么| 脖子变粗了是什么原因| 女人腰酸背痛是什么病| 冷战什么意思| 过敏性鼻炎吃什么药| 尿酸高是什么原因造成的| 做梦梦见剪头发是什么意思| fs是什么单位| 菠萝为什么要用盐水泡| 芹菜不能和什么食物一起吃| 什么菊花茶降火最好| 肠胃不好吃什么药最好| 潋滟什么意思| 鼻孔干燥是什么原因| 双卵巢是什么意思| 地龙是什么生肖| 二郎神叫什么名字| 蜂蜜是什么糖| 生理曲度存在是什么意思| 牡丹花像什么| 表示什么意思| d代表什么单位| 头发发黄是什么原因| 身份证后面有个x是什么意思| 皮试是什么意思| 猕猴桃对身体有什么好处| 什么叫多动症| bic是什么意思| 梅毒是什么症状| 心绞痛吃什么药缓解最快| 早上做什么运动最好| apf值是什么意思| 猫的偏旁叫什么| 皮肤过敏擦什么药膏好得快| 高血钾有什么症状| 朴实无华是什么意思| 刘邦和项羽是什么关系| 代谢不好吃什么药| 胃粘膜糜烂吃什么药| 男性尿道炎吃什么药| 过敏有什么症状表现| 昆仑山在什么地方| 空调病是什么症状| 不尽人意是什么意思| 阴部毛变白是什么原因| 小媳妇是什么意思| peb是什么意思| 乡试第一名叫什么| 咖啡加牛奶叫什么| 尿频看什么科| 维生素b2有什么功效| 为什么医院不用咳特灵| 肩膀疼是什么原因引起的| 什么羊不能吃| 血小板为什么会减少| 打嗝不停是什么原因| 什么是业力| 老叹气是什么原因| 烂尾是什么意思| 蜘蛛的血是什么颜色的| 附件炎吃什么药最好| 什么东西可以解酒| 新陈代谢是什么| 谷氨酸钠是什么东西| 孕妇肚子疼是什么原因| 遗传是什么意思| 虎与什么生肖相合| 啤酒加什么好喝| 雷锋是什么生肖| 小孩口腔溃疡是什么原因| 更年期什么时候结束| 乌鸦兄弟告诉我们什么道理| 焦虑症挂什么科| 中央处理器由什么组成| 胃炎不能吃什么| 为什么作什么的成语| 娘子啊哈是什么歌| 猫的胡须是干什么用的| 乙肝两对半和乙肝五项有什么区别| 梦到被雷劈是什么意思| 什么是坏血病| 殿试第一名叫什么| bml什么意思| 47是什么生肖| 雪燕是什么| 中国的国菜是什么| 微波炉蒸鸡蛋羹几分钟用什么火| 水飞蓟是什么| 退烧吃什么药| 吃什么能提高免疫力| 光脚走路有什么好处| 蕊五行属什么| 开边珠牛皮是什么意思| 冬阴功汤都放什么食材| 肌酐高什么原因引起的| 什么是浸润性乳腺癌| 99年的兔是什么命| 什么是ppi| 出口伤人是什么生肖| mk属于什么档次| 酒精对皮肤有什么伤害| 59是什么意思| 桥本氏甲状腺炎吃什么药| 冷笑话是什么意思| 长脸适合什么耳环| 手指甲软薄吃什么补| 天人合一是什么意思| 大便颜色发黑是什么原因| 6月16是什么星座| 补办手机卡需要什么| 1965属什么生肖| 湿气严重吃什么药好得快| 丢钱是什么预兆| 2004年出生属什么| 鸽子配什么煲汤最好| 吉人自有天相什么意思| 穹窿是什么意思| 境字五行属什么| 什么死法不痛苦| 夹腿是什么意思| 右手无名指戴戒指什么意思| 什么叫无产阶级| 催乳素偏高有什么影响| 蛇吃什么东西| 梦见流鼻血是什么征兆| 梓树为什么叫梧桐树| 小儿流清鼻涕吃什么药效果好| 眼底出血用什么眼药水| 拉肚子不能吃什么| 女用避孕套是什么样的| 什么是环切手术| 卫校学什么专业最好| 什么样的花| 牙齿根管治疗是什么意思| 肺结核阳性是什么意思| 十二月十二日是什么星座| 大便出油是什么原因| 齐白石擅长画什么| 血压高有什么好办法| 为什么会宫颈糜烂| 眼睛不舒服是什么原因引起的| 女生为什么喊你男神| 消化内科主要看什么病| 右肺中叶索条什么意思| 6.26什么星座| 安德玛是什么牌子| 血糖高吃什么水果好| 手突然抽搐是什么原因| 肝主筋的筋是指什么| 心慌吃点什么药| 肠胃炎需要做什么检查| 羊水破了是什么感觉| 生育酚是什么| 0r是什么意思| 喝红花有什么作用与功效| 给女生送礼物送什么好| 头孢不能和什么一起吃| 什么食物补气血| 怀孕可以喝什么饮料| 京东什么时候有活动| 双一流大学是什么意思| 水母是什么动物| 梦见苹果是什么意思| 2002年是什么生肖| 静脉曲张挂什么号| ctm是什么意思| 老年斑长什么样| 后背出汗是什么原因| 准生证什么时候办理| 黄芪入什么经| 左旋肉碱是什么| 什么水最解渴| AT代表什么| 腱鞘炎去医院挂什么科| 儿童回春颗粒主要治什么| 吃什么东西可以养胃| 吃什么东西可以除湿气| ovs是什么品牌| 月色真美是什么意思| 身上红痣多是什么原因| 隐血弱阳性是什么意思| 瓜怂是什么意思| 中暑了吃什么| 番石榴是什么| 眼睛像什么| moco是什么牌子| 空调扇的冰晶是什么| 早上九点半是什么时辰| 什么得当| 什么菜养胃| daily什么意思| 伤官伤尽是什么意思| 膀胱炎做什么检查能看出来| 地震为什么会发生| 属羊什么命| 豆粕是什么东西| 孕妇梦见鱼是什么意思| 女性长期便秘挂什么科| 排斥一个人什么意思| 浅表性胃炎吃什么药好使| 百度

新天际幼教走进井陉长峪 孝老爱亲情暖燕赵

JavaScript while Loop

百度 两国电影界人士希望通过举办电影节进一步加强中印电影领域的交流合作。

The while loop repeatedly executes a block of code as long as a specified condition is true.

The syntax of the while loop is:

while (condition) {
    // body of loop
}

Here,

  1. The while loop first evaluates the condition inside ( ).
  2. If the condition evaluates to true, the code inside { } is executed.
  3. Then, the condition is evaluated again.
  4. This process continues as long as the condition evaluates to true.
  5. If the condition evaluates to false, the loop stops.

Flowchart of while Loop

Flowchart of while loop in JavaScript
Flowchart of JavaScript while loop

Example 1: Display Numbers From 1 to 3

// initialize variable i
let i = 1;

// loop runs until i is less than 4
while (i < 4) {
    console.log(i);
    i += 1;
}

Output

1
2
3

Here is how the above program works in each iteration of the loop:

Variable Condition: i < 4 Action
i = 1 true 1 is printed. i is increased to 2.
i = 2 true 2 is printed. i is increased to 3.
i = 3 true 3 is printed. i is increased to 4.
i = 4 false The loop is terminated.

To learn more about loop conditions, visit JavaScript Comparison and Logical Operators.


Example 2: Sum of Only Positive Numbers

let num = 0, sum = 0;

// loop as long as num is 0 or positive
while (num >= 0) {

    // add all positive numbers
    sum += num;

    // take input from the user
    num = parseInt(prompt("Enter a number: "));
}

// last, display sum
console.log(`The sum is ${sum}`);

Output

Enter a number: 2
Enter a number: 4
Enter a number: -3
The sum is 6

The above program prompts the user to enter a number.

Since JavaScript prompt() only takes inputs as string, parseInt() converts the input to a number.

As long as we enter positive numbers, the while loop adds them up and prompts us to enter more numbers.

So when we enter a negative number, the loop terminates.

Finally, we display the total sum of positive numbers.

Note: When we add two or more numeric strings, JavaScript treats them as strings. For example, "2" + "3" = "23". So, we should always convert numeric strings to numbers to avoid unexpected behaviors.


JavaScript do...while Loop

The do...while loop executes a block of code once, then repeatedly executes it as long as the specified condition is true.

The syntax of the do...while loop is:

do {
    // body of loop
} while(condition);

Here,

  1. The do…while loop executes the code inside { }.
  2. Then, it evaluates the condition inside ( ).
  3. If the condition evaluates to true, the code inside { } is executed again.
  4. This process continues as long as the condition evaluates to true.
  5. If the condition evaluates to false, the loop terminates.

Flowchart of do...while Loop

Flowchart of do...while loop in JavaScript
Flowchart of JavaScript do...while loop

Example 3: Display Numbers from 3 to 1

let i = 3;

// do...while loop
do {
    console.log(i);
    i--;
} while (i > 0);

Output

3
2
1

Here, the initial value of i is 3. Then, we used a do...while loop to iterate over the values of i. Here is how the loop works in each iteration:

Action Variable Condition: i > 0
3 is printed. i is decreased to 2. i = 2 true
2 is printed. i is decreased to 1. i = 1 true
1 is printed. i is decreased to 0. i = 0 false
The loop is terminated. - -
What is the difference between while and do...while loops.

The difference between while and do...while is that the do...while loop executes its body at least once. For example,

let i = 0;

// false condition
// body executes once
do {
    console.log(i);
} while (i > 1);

// Output: 0

On the other hand, the while loop doesn't execute its body if the loop condition is false. For example,

let i = 0;

// false condition
// body not executed
while (i > 1) {
    console.log(i);
};

Example 4: Sum of Positive Numbers

let sum = 0, num = 0;

do {

    // add all positive numbers
    sum += num;

    // take input from the user
    num = parseInt(prompt("Enter a number: "));

    // loop terminates if num is negative
} while (num >= 0);

// last, display sum
console.log(`The sum is ${sum}`);

Output

Enter a number: 2
Enter a number: 4
Enter a number: -3
The sum is 6

In the above program, the do...while loop prompts the user to enter a number.

As long as we enter positive numbers, the loop adds them up and prompts us to enter more numbers.

If we enter a negative number, the loop terminates without adding the negative number.


More on JavaScript while and do...while Loops

What is an infinite while loop in JavaScript?

An infinite while loop is a condition where the loop runs infinitely, as its condition is always true. For example,

let i = 1;

// always true condition
while(i < 5) {
    console.log(i);
}

Also, here is an example of an infinite do...while loop,

let i = 5;

// always true condition
do {
    console.log(i);
} while (i > 1);

In the above program, the condition 1 > 1 is always true, which causes the loop body to run forever.

Note: Infinite loops can cause your program to hang. So, avoid creating them unintentionally.

What is the difference between for and while loops?

We use a for loop when we need to perform a fixed number of iterations. For example,

// display hi 3 times

for (let i = 1; i <= 3; i++) {
    console.log("hi");
}

console.log("bye");

Output

hi
hi
Hi
bye

Meanwhile, we use a while loop when the termination condition can vary. For example,

// display hi as long as user wants
let isDisplay = true;
let userChoice = "";

while (isDisplay) {
    console.log("hi");

    userChoice = prompt("print hi again? y for yes: ");
    if (userChoice != "y")
        isDisplay = false;
}

console.log("bye");

Output

hi
print hi again? y for yes: y
hi
print hi again? y for yes: y
hi
print hi again? y for yes: n
bye

In the above program, we let the user print hi as much as they desire.

Since we don't know the user's decision, we use a while loop instead of a for loop.


Also Read:

Before we wrap up, let’s put your knowledge of JavaScript while and do...while Loop to the test! Can you solve the following challenge?

Challenge:

Write a function to calculate the factorial of a number.

  • The factorial of a non-negative integer n is the product of all positive integers less than or equal to n.
  • For example, the factorial of 3 is 3 * 2 * 1 = 6.
  • Return the factorial of the input number num.

Video: JavaScript while Loop

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges
治飞蚊症用什么眼药水 尿黄是什么原因引起的 psy是什么意思 咸鸭蛋不能和什么一起吃 尿里有结晶是什么原因
处女是什么意思 麦冬是什么植物 英五行属什么 2026年属什么生肖 乙肝两对半245阳性是什么意思
古代天花是现代什么病 不踏实是什么意思 胰岛素高是什么意思 dfi是什么意思 肚子疼吃什么食物好
洗手指征是什么 dumpling是什么意思 尿胆原高是什么原因 处女座幸运颜色是什么 什么是反流性食管炎
伤寒病有什么症状hcv8jop0ns7r.cn 吃什么能补钙hcv9jop4ns4r.cn 软件开发需要学什么hcv8jop6ns3r.cn 艾特是什么意思hcv8jop7ns0r.cn 卜姓氏读什么hcv9jop1ns6r.cn
微信附近的人都是些什么人qingzhougame.com 黑猫警长为什么只有5集hcv9jop4ns0r.cn 胰腺癌晚期什么症状hcv8jop6ns3r.cn 澳大利亚属于什么洲hcv8jop6ns4r.cn 内讧是什么意思hcv9jop3ns9r.cn
热疹用什么药膏最好hcv9jop0ns8r.cn 五根手指叫什么hcv8jop5ns7r.cn 伦字五行属什么hcv7jop9ns5r.cn 女性内科检查什么hcv8jop6ns6r.cn 上马是什么意思xinmaowt.com
吃葡萄干对身体有什么好处youbangsi.com 银杏叶是什么颜色hcv8jop0ns9r.cn 知了为什么要叫hkuteam.com 秋葵什么时候种植最好fenrenren.com 流鼻血是什么引起的imcecn.com
百度