Wednesday, April 23, 2025

JavaScript Do While Loop

A do-while loop is similar to a while loop, but it will always execute at least once even if the condition is initially false. The loop will continue to execute until the condition becomes false. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	var number = 0;
	
	do {
		document.write("Number atm: " + number + "<br>");
		number = number + 1;
	} while (number < 50);	
		
</script>
	
</body>
</html>

This code initializes a variable called number to 0, and then enters a do-while loop. The loop body consists of a block of code enclosed in curly braces that writes the current value of number to the document and increments it by 1.

The loop will continue to execute as long as number is less than 50.

The main difference between a do-while loop and a while loop is that a do-while loop guarantees that the loop body will be executed at least once, regardless of the initial condition.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...