0% found this document useful (0 votes)
2 views2 pages

For Loop

The document explains the structure and components of a 'for' loop in programming, highlighting loop initialization, test statements, and iteration statements. It includes an HTML example demonstrating a 'for' loop that counts from 0 to 9 and prints the current count, as well as another example that prints numbers divisible by 3 or 5 from 1 to 500. The document also includes JavaScript code snippets to illustrate these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

For Loop

The document explains the structure and components of a 'for' loop in programming, highlighting loop initialization, test statements, and iteration statements. It includes an HTML example demonstrating a 'for' loop that counts from 0 to 9 and prints the current count, as well as another example that prints numbers divisible by 3 or 5 from 1 to 500. The document also includes JavaScript code snippets to illustrate these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

For Loop

Friday, August 19, 2022 9:21 AM

The 'for' loop is the most compact form of looping. It includes the following 3
important parts-
○ The 'loop initialization' where we initialize our counter to a starting value.
The initialization statement is executed before the loop begins.
○ The 'test statement' which will test if a given condition is true or not. If
the condition is true, then the code given inside the loop will be executed,
otherwise the control comes out of the loop.
○ The 'iteration statement' where you can increase or decrease your
counter.
You can put all the three parts in a single line separated by semicolons.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>For Loop Demo</title>
</head>
<body>
<h1>For Loop Demo</h1>
<script> var count ;
var count ; [Link]('Starting Loop<br>');
[Link]('Starting Loop<br>');
for(count = 0; count < 10; count++) { for(count = 0; count < 10; count++) {
[Link]('current count: ' + count);
[Link]('<br>'); [Link]('current count: ' + count);
} [Link]('<br>');
[Link]('Loop stopped!<br>');
</script> }
</body>
</html>

/**
* This code will print the numbers which are divisible by
* either 3 or 5, from 1 to 500.
*/
for (var count = 0, i = 1; i <= 500; i++) {
if (i % 3 == 0 || i % 5 == 0){
[Link](i+' ');
count++;
if(count%10 == 0){
[Link]('<br>')
}
}
}

Java Script Page 1


}
}

/**
* This code will print the numbers which are divisible by
* either 3 or 5, from 1 to 500.
*/
for (var count = 0, i = 1; i <= 500; i++) {
// if the value of i is completely divisible by 3 or 5 then this statement
is true
if (i % 3 == 0 || i % 5 == 0){
[Link](i+' ');
count++;
if(count%10 == 0){
[Link]('<br>')
}
}
}
/**
*
*
*
*
*
*
*
*/

Java Script Page 2

You might also like