Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Difference between JavaScript While and Do While loop

by suresh

Difference between JavaScript While and Do While loop with example?. Though Do While loop and While loop looks similar, they differ in their execution.

  • In While loop, the condition tested at the beginning of the loop, and if the condition is True, statements inside the loop will execute. It means the While loop executes the code block only if the condition is True.
  • At the end of the loop, the Do While loop tests the condition. So, Do While executes the statements in the code block at least once even if the condition Fails.

I think you will understand it completely when you see the example. So, let’s write the same program using While loop and Do While loop

JavaScript While Loop Example

In this JavaScript program, we are declaring an integer and then check whether One is greater than Ten or not. If the condition is True, it has to display the Output as “X is Greater Than 10”. There is one more statement outside the While loop, and it will run after the while loop.

<!DOCTYPE html>
<html>
<head>
    <title> Difference between JavaScript While and Do While loop </title>
</head>

<body>
    <h1> JavaScript While Loop </h1>
<script>
    var x = 1;
    while (x > 10)
    {
        document.write("<br\> <b> X Is greater Than 10 </b>");
    }

    document.write("<br\> <b> Statement Outside the while loop </b>");
</script>
</body>
</html>
Difference between JavaScript While and Do While loop 1

JavaScript Do While Loop Example

We are going to write the same JavaScript example using Do While loop

<!DOCTYPE html>
<html>
<head>
    <title> Difference between JavaScript While and Do While loop </title>
</head>

<body>
    <h1> JavaScript Do While Loop </h1>
<script>
    var x = 1;
    do
    {
        document.write("<br\> <b> X Is greater Than 10 </b>");
    }while (x > 10);
    
    document.write("<br\> <b> Statement Outside the while loop </b>");
</script>
</body>
</html>
Difference between JavaScript While and Do While loop 2

Although the Do while loop condition fails, the statement inside the loop is executed once. Because after the execution of the statements, the compiler tested the condition.

Hope you understood the difference :)

Placed Under: JavaScript

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy