DEV Community

ViGnEsH
ViGnEsH

Posted on

JavaScript While Loop Tutorial (With Examples)

πŸ“_ While _Loop in JavaScript
🧠 What is a While Loop?

πŸ‘‰ A while loop is use a repeat same console output show .
πŸ‘‰ A long condition is true only show outout.

Syntax

while (condition) {
  // code to run
}
Enter fullscreen mode Exit fullscreen mode

  • Same value in repect in again

Explain:
-> 1 variable Declaration & initalization
-> Set a while loop condition
-> print statement console.log("1")
-> " " always print same value because string value no change
-> a++ increase value
-> { } Curly brackets inset process in all loop.


  • Basic Example 1 2 3 4 5

change: console.log
frist program console in string this program console condition

Explain:
-> This also same but different for print statement
-> console.log (a) - variable name = a
-> a print changing value. variable value change.


  • Reverse Loop

Explain:
-> This also same but change variable value ,loop value & ...
-> 10 variable Declaration & initalization
-> Set a while loop condition 1
-> top & bottom.
-> print statement console.log(a)
-> a print change values

-> a-- decrease value minus a values

Odd Number Example

Even Numbers Example

Explain:
-> 1 variable Declaration & initalization
-> Set a while loop condition
-> small Diffent while loop inset if condition
-> if condition checktrue go inside , false not allowed inside
-> % Modulus
-> i modulus 2 == 0
-> i divide 2 remainder value 0 true that is even
-> i modulus 2 != 0 != change oppsite value odd.

-> print statement console.log("1")
-> " " always print same value because string value no change

-> a++ increase value

*Multiples of 3 and 5 *

% β†’ remainder
== 0 β†’ divisible
&& β†’ both conditions

% β†’ gives remainder
a % 3 == 0 β†’ divisible by 3
a % 5 == 0 β†’ divisible by 5
&& β†’ BOTH conditions must be true

Explain:
-> 1 variable Declaration & initalization
-> Set a while loop condition
-> if condition

-> modulus two condition true only give output because && (and) Logical operation.
-> print statement console.log("1")
-> " " always print same value because string value no change

-> a++ increase value

  • β€œPrint numbers from 1 to 100
  • that are NOT multiples of 3 and NOT multiples of 5”

Meaning:

  • i % 3 != 0 β†’ NOT divisible by 3
  • i % 5 != 0 β†’ NOT divisible by 5
  • && β†’ both must be true

Multiples 3 or 5

OR is a 3 or 5 Decimal values

Explain:
-> 1 variable Declaration & initalization
-> Set a while loop condition
-> if condition

-> modulus two condition true only give output because ||(or) Logical operation.
-> print statement console.log("1")
-> " " always print same value because string value no change
-> a++ increase value



Divisors of given number

Meaning
if 20 divsible by i
then i is a factor


Meaning
if i divsible by 20
then 20 is a factor


Count of Diviors of given number

Explain:
-> three variables n,i,count.
-> Set a while loop condition
-> if condition checktrue go inside , false not allowed inside
-> % Modulus
-> i modulus 2 == 0
-> i divide 2 remainder value 0 true that is even
-> i modulus 2 != 0 != change oppsite value odd.

-> print statement console.log("1")
-> " " always print same value because string value no change
-> a++ increase value

Top comments (0)