C++: The for loop

This entry is part 15 of 61 in the series C++

The for loop allows one to execute a set of the instructions until a condition is met.

It has the following syntax:

for (initialization; test-condition; increment/decrement)
{
    Statement;
}

The test condition is similar to the test condition that we use in an if statement.

Consider the following example where we will perform an operation similar to below.

1+2+3+4+5+6+7+8+9+10=55

#include <iostream>
using namespace std;

int main()
{
	int sum = 0;

	for (int i = 1; i <= 10; ++i)
		sum += i;

	cout << "Sum of 1 to 10 inclusive is " << sum << endl;

	return 0;

}

The initialization is done within the for statement setting i = 1.  On each loop, a sequence of number will be added to the variable sum.

Notice we have been able to define i within the for loop itself.

forloop

 

 

Java: For loop

This entry is part 9 of 54 in the series Learn Java

The for loop is used to go through a code block a specific number of times. It uses three parameters.

The first parameter initializes a counter and is always executed once, before the loop.

The second parameter holds the condition for the loop and is checked before each iteration.

The third parameter contains the increment of the counter and is executed at the end of each iteration.

package javaapplication8;

public class JavaApplication8 {

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            System.out.print(i);
        }
    }
}

for

 

 

Javascript: Processing radio buttons using for loop

This entry is part 22 of 27 in the series Javascript Tutorial

In the earlier example, we have used many if else loops to process the radio buttons.

A more elegant way is to use for loop.

<!DOCTYPE html>
<html>
<head>
    <title>Form with radio buttons</title>
    <script>
        function submitForm(form) {

            var item;
            var i;
            var length;

            var radio_button = document.getElementsByName('radio_button');

            length = radio_button.length;

            for (i = 0; i < length; i++) {
                if (radio_button[i].checked) {
                    item = radio_button[i].value;
                    document.getElementById("buy_id").innerHTML = item;
                }
            }
        }
    </script>
</head>
<body>
<form>
    <p>What would you like to buy this year?</p>
    <input type="radio" name="radio_button" value="iPad" checked="checked">iPad<br>
    <input type="radio" name="radio_button" value="Camera">Camera<br>
    <input type="radio" name="radio_button" value="iPhone">iPhone<br>
    <input type="radio" name="radio_button" value="Car">Car<br>
    <input type="radio" name="radio_button" value="House">House<br>
    <input type="radio" name="radio_button" value="Aeroplane">Aeroplane<br>
    <input type="button" value="Submit" onclick="submitForm(this.form)">
</form>
<p>What I have bought</p>

<h3 id="buy_id"></h3>
</body>
</html>

 

The codes used in this example are a lot shorter and the number of lines needed in this for loop is almost equivalent to one if loop in the earlier example.

 

Javascript: For loop

This entry is part 20 of 27 in the series Javascript Tutorial

Let’s step aside with form and look at for loop statement in Javascript.  We will apply for loop to radio button form in the next article.

In the example below, we have an array of 3 items.

To access the number of items, we can use item.length.

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>For Loop</title>
    <script>
        function submitButton() {
            var item = ['one', 'two', 'three'];
            var i;
            var l = item.length;
            alert('No of item is ' + l);
            for (i = 0; i < l; i++) {
                alert(item[i]);
            }
        }
    </script>

</head>
<body>

<input type='button' value='Submit' onclick='submitButton()'>

</body>
</html>

button

Using for loop, it will cycle through the array and print out each item.

The above for statement starts by initializing i to 0. It checks that i is less than l, performs the alert statement, and increments i by 1 after each pass through the loop.

 

PHP: For loop

This entry is part 28 of 54 in the series PHP Tutorial

For loops are the most complex loops in PHP.  The syntax of a for loop is:

for (expr1; expr2; expr3)
    statement

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed.

If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).

<?PHP
for ($i = 1; $i <= 10; $i++) {
    echo $i;
    echo "\n";
}
?>

In this simple example, $i is first initialized to 1.  The condition $i <= 10 is checked and if evaluated to true, the 2 nested statements are executed.