Ad Code

Foreach Loop - Programming Languages loop + usage

Foreach is a loop and support by numbers of language in different manner. Their syntax and usage are different but some how they work same in the manner that everybody can work with it.Languages includes PHP, C#, C++, JavaScript, jQuery,  Oracle. We have demonstrated some of the examples from different web pages which includes many sites. But lets look at different language foreach loop syntax and how to use it.

Foreach Loop in Different Languages 

PHP

<?php

$colors = array("red", "green", "blue", "yellow");



foreach ($colors as $value) {

    echo "$value <br>";

}

?>

C#

// You can maintain a count of the elements in the collection.

        int count = 0;

        foreach (int element in fibarray)

        {

            count += 1;

            System.Console.WriteLine("Element #{0}: {1}", count, element);

        }

JavaScript

var sum = 0;

var obj = {prop1: 5, prop2: 13, prop3: 8};



for each (var item in obj) {

  sum += item;

}


console.log(sum); // logs "26", which is 5+13+8

 jQuery

var obj = {

  "flammable": "inflammable",

  "duh": "no duh"

};

$.each( obj, function( key, value ) {

  alert( key + ": " + value );

});

C++

std::string strarr[] = {"ram", "mohan", "sita"};

for(int i = 0; i < 3; ++i) {

  listbox.items.add(strarr[i]);

}

Oracle

int sum(int[] a) {

    int result = 0;

    for (int i : a)

        result += i;

    return result;

}

These all are the syntax used by different languages if you know some of them please do share it here we will add it above and we will be thankful to you for your contribution. If you love this sharing the do care sharing with others or let us help it more grow by typing a helpful comment according to any loop specialty or anything you do care sharing.
Reactions

Post a Comment

0 Comments