SlideShare a Scribd company logo
While Loop And For Loop
While writing a program, you may encounter a situation where you need to perform an action over and over again. In
such situations, you would need to write loop statements to reduce the number of lines.
JavaScript supports all the necessary loops to ease down the pressure of programming.
The while Loop:
The most basic loop in JavaScript is the while loop which would be discussed in this chapter. The
purpose of a while loop is to execute a statement or code block repeatedly as long as an expression is
true. Once the expression becomes false, the loop terminates.
Flow Chart
The flow chart of while loop looks as follows −
Syntax
The syntax of while loop in JavaScript is as follows −
Example
Try the following example to implement while loop.
Output
The do...while Loop
The do...while loop is similar to the while loop except that the condition check happens at the end of
the loop. This means that the loop will always be executed at least once, even if the condition is false.
Flow Chart
The flow chart of a do-while loop would be as follows −
Syntax
The syntax for do-while loop in JavaScript is as follows −
Note − Don’t miss the semicolon used at the end of the do...while loop.
Example:
Try the following example to learn how to implement a do-while loop in JavaScript.
Output
For Loop
The 'for' loop is the most compact form of looping. It includes the following three
important parts −
•The loop initialization where we initialize our counter to a starting value. The
initialization statement is executed before the loop begins.
•The test statement which will test if a given condition is true or not. If the condition is
true, then the code given inside the loop will be executed, otherwise the control will
come out of the loop.
•The iteration statement where you can increase or decrease your counter.
You can put all the three parts in a single line separated by semicolons.
Flow Chart:
The flow chart of a for loop in JavaScript would be as follows −
Syntax
The syntax of for loop is JavaScript is as follows −
Example:
Try the following example to learn how a for loop works in JavaScript.
Output

More Related Content

What's hot (11)

PDF
Javascript internals
Ayush Sharma
 
PDF
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
PPTX
types of loops and what is loop
waheed dogar
 
PPTX
Do...while loop structure
Jd Mercado
 
PDF
22 Jop Oct 08
Ganesh Samarthyam
 
PPTX
Types of loops in c language
sneha2494
 
PPTX
Looping statements
AbhishekMondal42
 
PPTX
Loops in C
Kamal Acharya
 
PPTX
Loops
SAMYAKKHADSE
 
PPTX
Asynchronous Programming in .NET
Pierre-Luc Maheu
 
Javascript internals
Ayush Sharma
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
 
types of loops and what is loop
waheed dogar
 
Do...while loop structure
Jd Mercado
 
22 Jop Oct 08
Ganesh Samarthyam
 
Types of loops in c language
sneha2494
 
Looping statements
AbhishekMondal42
 
Loops in C
Kamal Acharya
 
Asynchronous Programming in .NET
Pierre-Luc Maheu
 

Similar to While loop and for loop 06 (js) (20)

DOCX
Janakiram web
MARELLA CHINABABU
 
DOCX
loops and iteration.docx
JavvajiVenkat
 
PPTX
LOOPING IN C- PROGRAMMING.pptx
AFANJIPHILL
 
PPTX
prt123.pptx
ASADKS
 
PPTX
Loop in C Properties & Applications
Emroz Sardar
 
PPTX
A Complete Guide on While Loop in MATLAB
matlabassignmenthelp1
 
PPTX
Presentation on nesting of loops
bsdeol28
 
PPTX
Cse lecture-7-c loop
FarshidKhan
 
PPTX
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PDF
Cpp loop types
Rj Baculo
 
PPTX
FOR LOOP TOPIC(syntax, flow diagram and examples).pptx
arconflame15
 
PDF
Matlab practical ---3.pdf
Central university of Haryana
 
PPTX
Cordova training : Day 3 - Introduction to Javascript
Binu Paul
 
PPTX
C Programming: Looping Statements in C Pgm
Navya Francis
 
PPT
C++ programming
viancagerone
 
PDF
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
PPTX
Loop structures
tazeem sana
 
Janakiram web
MARELLA CHINABABU
 
loops and iteration.docx
JavvajiVenkat
 
LOOPING IN C- PROGRAMMING.pptx
AFANJIPHILL
 
prt123.pptx
ASADKS
 
Loop in C Properties & Applications
Emroz Sardar
 
A Complete Guide on While Loop in MATLAB
matlabassignmenthelp1
 
Presentation on nesting of loops
bsdeol28
 
Cse lecture-7-c loop
FarshidKhan
 
Computer programming 2 Lesson 8
MLG College of Learning, Inc
 
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Cpp loop types
Rj Baculo
 
FOR LOOP TOPIC(syntax, flow diagram and examples).pptx
arconflame15
 
Matlab practical ---3.pdf
Central university of Haryana
 
Cordova training : Day 3 - Introduction to Javascript
Binu Paul
 
C Programming: Looping Statements in C Pgm
Navya Francis
 
C++ programming
viancagerone
 
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
Loop structures
tazeem sana
 
Ad

More from AbhishekMondal42 (20)

PPTX
Oss evaluation-certification-oss-financial-advantages
AbhishekMondal42
 
PPTX
Word press 01
AbhishekMondal42
 
PPTX
Word press posts(preview & publish)
AbhishekMondal42
 
PPTX
Word press posts(add , edit , delete post)
AbhishekMondal42
 
PPTX
Word press pages(edit and delete)
AbhishekMondal42
 
PPTX
Word press pages(add)
AbhishekMondal42
 
PPTX
Word press media(add,insert,delete)
AbhishekMondal42
 
PPTX
Word press media library
AbhishekMondal42
 
PPTX
Word press widget management
AbhishekMondal42
 
PPTX
Word press view plugins
AbhishekMondal42
 
PPTX
Word press user roles
AbhishekMondal42
 
PPTX
Word press theme management
AbhishekMondal42
 
PPTX
Word press personal profile
AbhishekMondal42
 
PPTX
Word press moderate comments
AbhishekMondal42
 
PPTX
Word press install plugins
AbhishekMondal42
 
PPTX
Word press edit users
AbhishekMondal42
 
PPTX
Word press edit tags
AbhishekMondal42
 
PPTX
Word press edit links
AbhishekMondal42
 
PPTX
Word press edit comments
AbhishekMondal42
 
PPTX
Word press delete users
AbhishekMondal42
 
Oss evaluation-certification-oss-financial-advantages
AbhishekMondal42
 
Word press 01
AbhishekMondal42
 
Word press posts(preview & publish)
AbhishekMondal42
 
Word press posts(add , edit , delete post)
AbhishekMondal42
 
Word press pages(edit and delete)
AbhishekMondal42
 
Word press pages(add)
AbhishekMondal42
 
Word press media(add,insert,delete)
AbhishekMondal42
 
Word press media library
AbhishekMondal42
 
Word press widget management
AbhishekMondal42
 
Word press view plugins
AbhishekMondal42
 
Word press user roles
AbhishekMondal42
 
Word press theme management
AbhishekMondal42
 
Word press personal profile
AbhishekMondal42
 
Word press moderate comments
AbhishekMondal42
 
Word press install plugins
AbhishekMondal42
 
Word press edit users
AbhishekMondal42
 
Word press edit tags
AbhishekMondal42
 
Word press edit links
AbhishekMondal42
 
Word press edit comments
AbhishekMondal42
 
Word press delete users
AbhishekMondal42
 
Ad

Recently uploaded (20)

PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Dimensions of Societal Planning in Commonism
StefanMz
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 

While loop and for loop 06 (js)

  • 1. While Loop And For Loop
  • 2. While writing a program, you may encounter a situation where you need to perform an action over and over again. In such situations, you would need to write loop statements to reduce the number of lines. JavaScript supports all the necessary loops to ease down the pressure of programming. The while Loop: The most basic loop in JavaScript is the while loop which would be discussed in this chapter. The purpose of a while loop is to execute a statement or code block repeatedly as long as an expression is true. Once the expression becomes false, the loop terminates. Flow Chart The flow chart of while loop looks as follows −
  • 3. Syntax The syntax of while loop in JavaScript is as follows − Example Try the following example to implement while loop.
  • 5. The do...while Loop The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false. Flow Chart The flow chart of a do-while loop would be as follows −
  • 6. Syntax The syntax for do-while loop in JavaScript is as follows − Note − Don’t miss the semicolon used at the end of the do...while loop. Example: Try the following example to learn how to implement a do-while loop in JavaScript.
  • 8. For Loop The 'for' loop is the most compact form of looping. It includes the following three important parts − •The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. •The test statement which will test if a given condition is true or not. If the condition is true, then the code given inside the loop will be executed, otherwise the control will come out of the loop. •The iteration statement where you can increase or decrease your counter. You can put all the three parts in a single line separated by semicolons.
  • 9. Flow Chart: The flow chart of a for loop in JavaScript would be as follows − Syntax The syntax of for loop is JavaScript is as follows −
  • 10. Example: Try the following example to learn how a for loop works in JavaScript.