メインコンテンツへスキップ
見出し画像

【15言語比較】foreachの書き方まとめ(備忘録)

    色々なプログラミング言語に携わっていると、似たような構文でも言語ごとの書き方を混同しやすい。

    その都度調べるのも面倒なので、備忘録としてまとめておく。

    JavaScript

    const items = [1, 2, 3];
    
    items.forEach(x => console.log(x));
    
    for (const x of items) {
        console.log(x);
    }

    TypeScript

    const items: number[] = [1, 2, 3];
    
    items.forEach(x => console.log(x));
    
    for (const x of items) {
        console.log(x);
    }

    jQuery

    const items = [1, 2, 3];
    
    $.each(items, (i, x) => console.log(x));

    C#

    var items = new[] { 1, 2, 3 };
    
    foreach (var x in items) 
    {
        Console.WriteLine(x);
    }
    
    Array.ForEach(items, x => Console.WriteLine(x));

    PHP

    $items = [1, 2, 3];
    
    foreach ($items as $x) {
        echo $x;
    }

    Java

    // Java 10以降
    var items = List.of(1, 2, 3);
    items.forEach(x -> System.out.println(x));
    
    // Java 9以降
    List<Integer> items = List.of(1, 2, 3);
    items.forEach(x -> System.out.println(x));
    
    // Java 8
    List<Integer> items = Arrays.asList(1, 2, 3);
    items.forEach(x -> System.out.println(x));
    
    // Java 5以降
    List<Integer> items = Arrays.asList(1, 2, 3);
    for (Integer x : items) {
        System.out.println(x);
    }


    Python

    items = [1, 2, 3]
    
    for x in items:
        print(x)

    VB.NET

    Dim items = {1, 2, 3}
    
    For Each x In items
        Console.WriteLine(x)
    Next

    C

     #include  <stdio.h>
    
    int items[] = {1, 2, 3};
    size_t count = sizeof(items) / sizeof(items[0]);
    
    for (size_t i = 0; i < count; i++) {
        printf("%d\n", items[i]);
    }

    Perl

    my @items = (1, 2, 3);
    
    foreach my $x (@items) {
        print "$x\n";
    }

    PowerShell

    $items = @(1, 2, 3)
    
    foreach ($x in $items) {
        Write-Output $x
    }
    
    $items | ForEach-Object { Write-Output $_ }

    Bash

    items=(1 2 3)
    
    for x in "${items[@]}"; do
        echo "$x"
    done

    PL/SQL(Oracle)

    DECLARE
        TYPE t_items IS TABLE OF NUMBER;
        items t_items := t_items(1, 2, 3);
    BEGIN
        FOR i IN 1 .. items.COUNT LOOP
            DBMS_OUTPUT.PUT_LINE(items(i));
        END LOOP;
    END;

    T-SQL(SQL Server)

    INSERT INTO @items VALUES (1), (2), (3);
    
    DECLARE @i INT = 1;
    
    WHILE @i <= 3
    BEGIN
        PRINT (SELECT id FROM @items WHERE id = @i);
        SET @i = @i + 1;
    END;

    MySQL(Stored Procedure)

    CREATE PROCEDURE loop_items()
    BEGIN
        DECLARE items JSON DEFAULT '[1, 2, 3]';
        DECLARE i INT DEFAULT 0;
    
        WHILE i < JSON_LENGTH(items) DO
            SELECT JSON_EXTRACT(items, CONCAT('$[', i, ']'));
            SET i = i + 1;
        END WHILE;
    END;

    #プログラミング #JavaScript #TypeScript #jQuery #C #CSharp #PHP #Java #Python #VB .NET #Perl #PowerShell #Bash #PLSQL #SQLServer #MySQL #備忘録

    更新履歴
    2026-11-08 3言語追加しました(#PLSQL #SQLServer #MySQL )

    あなたへのおすすめ