Javascript 1O1 series : Writing loops with For, While and Do while.

You’ll learn :
About loops statements and their usage, and how to chose the right loop for your code situation.

what are loops :

its a way to execute a certain code repeatedly based on some condition

Loops offer a quick and easy way to do some…


This content originally appeared on DEV Community and was authored by osama

from tutorialandexample.com

You’ll learn :
About loops statements and their usage, and how to chose the right loop for your code situation.

what are loops :

its a way to execute a certain code repeatedly based on some condition

Loops offer a quick and easy way to do something repeatedly. MDN

we have 3 types of loops in javascript, each of which has its own syntax, use cases, thought all of them do the same thing :execute certain code over and over.

for : loops through a block of code a number of times
while : loops through a block of code while a specified condition is true
do … while : also loops through a block of code while a specified condition is true

For:
its Syntax :

for (counter ; condition ; update the counter after every code execution) {
the code we’ll execute over and over;
}

condition is to decide when to stop the loop or to repeat it once again, most likely it’ll be a comparison between two values and if this condition is true, the loop will keep working.
for be like, hey repeat this code until the condition is met or happen
e.g.
for

guess what ? this simple code will print 5 different values! how is that ?

how it works and the steps our code will go through :
first define the counter be 0, and as long as counter value is less than 5, print its value and repeat.
and if the counter value is not less than 5, then stop
lets see the steps it goes to execute this code
1/ initiate a counter
2/ set the condition that allow the code to keep executing
3/ execute the code block
4/ update the counter after each execution

e.g.
Print the summation of numbers between 0 and 10 :
for loop

while :
The while statement is a more simple version of the for statement which checks if an expression evaluates to true and runs as long as it says true.
Syntax :

while (condition is true) {
execute this code;
}
its like you say, while this condition is true, keep the loop working.
while

while loop

If the condition becomes false, code within the loop will stop executing.

Do while :
syntax :
do {
our code block we want to execute when the condition is tru
} while (the condition)
do while

Do-while loops are useful when you want your code to output some sort of menu to a screen so that the menu is guaranteed to show once.

simply you say, repeat executing the code while the condition is true.

Differences between while and do…while :
in while statement, the condition will be checked first and if it true the code block will be executed.
but when it comes to do while, it execute the code block first and then check the condition whether it is true or not.
it means if our condition is false, in while statement the code block will not be executed, but in do while it will be executed only one.

References and useful Links :



Loops and iteration - JavaScript | MDN

Loops offer a quick and easy way to do something repeatedly. This chapter of the JavaScript Guide introduces the different iteration statements available to JavaScript.

favicon developer.mozilla.org

JavaScript Loops Explained: For Loop, While Loop, Do...while Loop, and More

Loops are used in JavaScript to perform repeated tasks based on a condition. Conditions typically return true or false. A loop will continue running until the defined condition returns false. for Loop Syntax for (initialization; condition; finalExpression) { // code } The for loop consists of three optional

favicon freecodecamp.org

Thanks for reading, feel free to ask any question about javascript or about this series, I appreciate any feedback or advises to improve My content.
find me on twitter, github and my portfolio.


This content originally appeared on DEV Community and was authored by osama


Print Share Comment Cite Upload Translate Updates
APA

osama | Sciencx (2022-04-17T05:00:07+00:00) Javascript 1O1 series : Writing loops with For, While and Do while.. Retrieved from https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/

MLA
" » Javascript 1O1 series : Writing loops with For, While and Do while.." osama | Sciencx - Sunday April 17, 2022, https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/
HARVARD
osama | Sciencx Sunday April 17, 2022 » Javascript 1O1 series : Writing loops with For, While and Do while.., viewed ,<https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/>
VANCOUVER
osama | Sciencx - » Javascript 1O1 series : Writing loops with For, While and Do while.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/
CHICAGO
" » Javascript 1O1 series : Writing loops with For, While and Do while.." osama | Sciencx - Accessed . https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/
IEEE
" » Javascript 1O1 series : Writing loops with For, While and Do while.." osama | Sciencx [Online]. Available: https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/. [Accessed: ]
rf:citation
» Javascript 1O1 series : Writing loops with For, While and Do while. | osama | Sciencx | https://www.scien.cx/2022/04/17/javascript-1o1-series-writing-loops-with-for-while-and-do-while/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.