home > 写経編 > 柴田望洋『明解C言語 入門編』 > 5. 配列 >

ForNext

Only Do What Only You Can Do

031. 配列の各要素に先頭から順に 1,2,3,4,5 を代入して表示 (for文)

VBScript

JScript

Perl

更新日 : 2010.10.18
for (0..4)
{
    $score[$_] = $_ + 1;
}

for (0..4)
{
    printf("点数%d = %d\n", $_ + 1, $score[$_]);
}
L:\>perl lesson_05_031.pl
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

PHP

更新日 : 2010.11.03
<?php
for ($i = 0; $i < 5; $i++)
{
    $score[$i] = $i + 1;
}

for ($i = 0; $i < 5; $i++)
{
    printf("点数%d = %d\n", $i + 1, $score[$i]);
}
?>
L:\>php lesson_05_031.php
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

Python

更新日 : 2010.11.17
# coding: Shift_JIS

score = range(5)

for i in range(0, 5, 1):
    score[i] = i + 1

for i in range(0, 5, 1):
    print "点数%d = %d" % (i + 1, score[i])
N:\>python lesson_05_031.py
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

Ruby

更新日 : 2010.11.01
score = []

for i in 0..4
    score[i] = i + 1
end

for i in 0..4
    printf("点数%d = %d\n", i + 1, score[i])
end
L:\>ruby  l:\lesson_05_031.rb
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

PowerShell

Scala

F#

C

更新日 : 2010.10.08
#include <stdio.h>
int main(int argc, char* argv[])
{
    int i;
    int score[5];

    for (i = 0; i < 5; i++)
        score[i] = i + 1;

    for (i = 0; i < 5; i++)
        printf("点数%d = %d\n", i + 1, score[i]);

    return 0;
}
R:\>lesson031\project1.exe
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

C++

C++Builder

VC++

C#

Java

更新日 : 2010.11.05
class Lesson031 {
    public static void main(String[] args) {
        int[] score = new int[5];

        for (int i = 0; i < 5; i++)
            score[i] = i + 1;

        int j = 1;
        for(int s : score) {
            System.out.printf("点数%d = %d\n", j++, s);
        }
    }
}
L:\>java Lesson031
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

Objective-C

D

VB

VB.NET

Delphi

更新日 : 2010.09.24
program Project1;

{$APPTYPE CONSOLE}

uses
    SysUtils;
var
    i: Integer;
    score: array[1..5] of Integer;
begin
    for i := 1 to 5 do
        score[i] := i;

    for i := 1 to 5 do
        writeln(format('点数%d = %d', [i, score[i]]));
end.
S:\>lesson031\project1.exe
点数1 = 1
点数2 = 2
点数3 = 3
点数4 = 4
点数5 = 5

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system