Thursday, October 7, 2021
The while loop statement in JavaScript simply executes a statement block until a condition is not true anymore the syntax for the while loop is as follow
while (expression is true)
{
execute these statements
}
The while loop is an iterative loop if the condition is true and the statements are executed, it starts at the top of the loop again and executes until the expression is false. Therefore, there's a potential for an infinite loop.
Here is an example of a while loop:
var countdown = 10;
while (countdown > 0) {
console.log(countdown);
countdown--;
}
console.log("Happy New Year!");
The code above simulates a new years count down which we are familiar with, the loop tests to see if countdown is greater than zero or not. If countdown is greater than zero then we continue to execute the loop else we exit the loop.
Here is the output:
Now let's see how this loop can turn into an infinite loop if the exit condition is never met!
var countdown = 10;
while (countdown > 0) {
console.log(countdown);
countdown--;
countdown++;
}
console.log("Happy New Year!");
The code above decrements the countdown variable which is what we used to test our expression then increments it back therefore not changing the value at all and the expression always evaluates to true. Hence, we have the countdown from hell! Stuck at 10! Never getting to "Happy New Year!" which is waiting patiently outside the while loop. Poor "Happy New Year!" , my heart bleeds for you.
while (expression is true)
{
execute these statements
}
The while loop is an iterative loop if the condition is true and the statements are executed, it starts at the top of the loop again and executes until the expression is false. Therefore, there's a potential for an infinite loop.
Here is an example of a while loop:
var countdown = 10;
while (countdown > 0) {
console.log(countdown);
countdown--;
}
console.log("Happy New Year!");
The code above simulates a new years count down which we are familiar with, the loop tests to see if countdown is greater than zero or not. If countdown is greater than zero then we continue to execute the loop else we exit the loop.
Here is the output:
Now let's see how this loop can turn into an infinite loop if the exit condition is never met!
var countdown = 10;
while (countdown > 0) {
console.log(countdown);
countdown--;
countdown++;
}
console.log("Happy New Year!");
The code above decrements the countdown variable which is what we used to test our expression then increments it back therefore not changing the value at all and the expression always evaluates to true. Hence, we have the countdown from hell! Stuck at 10! Never getting to "Happy New Year!" which is waiting patiently outside the while loop. Poor "Happy New Year!" , my heart bleeds for you.
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Excelent .. works! and simple. Thx!
ReplyDeleteAndrew Polworth
i am Like your blog is a greet info this artical
ReplyDeleteAndrew Polworth
Hey there! We are a reliable company https://essaysrescue.com/writemypapers-org-review/ to depend on if you have a literature review assignment because we work with highly qualified and experienced writers. Apart from that, our writers are competent to handle any order within a specified deadline. Therefore, it is possible to get prompt assistance for someone looking for urgent solutions with their assignments.
ReplyDeleteThe while loop is an iterative loop if the condition is true and the statements are executed, it starts at the top of the loop again and executes until the expression is false. casinoschileonline.com Therefore, there's a potential for an infinite loop.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete