Wednesday, April 23, 2025

JavaScript For Loop

A for loop is a control flow statement that allows you to repeatedly execute a block of code a certain number of times. It provides a compact way to iterate over a range of values, such as the elements of an array, the digits of a number, or any sequence of numbers.

The syntax of a for loop typically includes an initialization statement, a condition for termination, and an update statement.

For loops are useful in situations where we need to iterate over a sequence of values or perform a task a specific number of times. Some examples of their use cases include:

  1. Processing arrays or lists: For loops can be used to iterate over the elements of an array or a list and perform a certain operation on each element.

  2. Displaying repetitive patterns: For loops can be used to display a repetitive pattern on a web page or a graphical user interface.

  3. Generating sequences: For loops can be used to generate a sequence of numbers or characters based on a given pattern or formula.

  4. Performing mathematical computations: For loops can be used to perform mathematical computations, such as calculating the sum of a series of numbers or finding the minimum or maximum value in a set of numbers.

  5. Reading and writing files: For loops can be used to read and write files, where each iteration of the loop processes a line or a block of data in the file. 


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

<script>

	for (x = 0; x < 100; x = x + 1) {
		document.write(x + "<br>");
	}
		
</script>
	
</body>
</html>

This script will output the numbers from 0 to 99, each number on a new line, using a for loop.

The for loop is used to execute a block of code a certain number of times. In this case, it starts by initializing the x variable to 0.

Then, it checks if x is less than 100. If x is less than 100, the code inside the loop is executed, which prints the value of x on a new line using the document.write statement.

After that, the loop updates the value of x by adding 1 to it, and the loop starts over, checking again if x is less than 100. This process repeats until x is no longer less than 100, at which point the loop ends.

In the context of a for loop, x = x + 1 means that the value of the x variable is incremented by 1 in each iteration of the loop.


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

<script>

	var x = 0;

	for (x; x < 100; x = x + 5) {
		document.write("x in moment of time: " + x + "<br>");
	}
		
</script>
	
</body>
</html>

Result: 

x in moment of time: 0
x in moment of time: 5
x in moment of time: 10
x in moment of time: 15
x in moment of time: 20
x in moment of time: 25
x in moment of time: 30
x in moment of time: 35
x in moment of time: 40
x in moment of time: 45
x in moment of time: 50
x in moment of time: 55
x in moment of time: 60
x in moment of time: 65
x in moment of time: 70
x in moment of time: 75
x in moment of time: 80
x in moment of time: 85
x in moment of time: 90
x in moment of time: 95

This code sets the initial value of variable x to 0, then uses a for loop to execute a block of code repeatedly as long as x is less than 100, incrementing the value of x by 5 with each iteration.

During each iteration of the loop, the code within the curly braces is executed, which in this case is a call to document.write() to display the current value of x. The output shows the value of x after each iteration, starting from 0 and increasing by 5 each time, until it reaches 100 or greater and the loop stops.

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 .  ...