home > さまざまな言語で数値計算 > 手続き型プログラミングの基本 >

さまざまな言語で数値計算

Only Do What Only You Can Do

10未満の自然数 (繰返し)

VBScript

Option Explicit

Dim i
For i = 1 To 9
    WScript.StdOut.Write i & ", "
Next
WScript.StdOut.WriteLine
Z:\>cscript //nologo 0103.vbs
1, 2, 3, 4, 5, 6, 7, 8, 9,

JScript

for (var i = 1; i < 10; i++) {
    WScript.StdOut.Write(i + ", ");
}
WScript.StdOut.WriteLine();
Z:\>cscript //nologo 0103.js
1, 2, 3, 4, 5, 6, 7, 8, 9,

PowerShell

foreach ($i in 1..9)
{
    Write-Host "$i, " -nonewline
}
Z:\>powershell -file 0103.ps1
1, 2, 3, 4, 5, 6, 7, 8, 9,

Perl

for (1..9)
{
    print "$_, ";
}
print "\n";
Z:\>perl 0103.pl
1, 2, 3, 4, 5, 6, 7, 8, 9,

PHP

<?php
foreach (range(1, 9) as $i)
{
    echo "$i, ";
}
echo "\n";
?>
Z:\>php 0103.php
1, 2, 3, 4, 5, 6, 7, 8, 9,

Python

for i in range(1, 10):
    print i, ",",
Z:\>python 0103.py
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ,

Ruby

(1..9).each do |i|
    print "#{i}, "
end
print "\n"
Z:\>ruby 0103.rb
1, 2, 3, 4, 5, 6, 7, 8, 9,

Groovy

Pascal

Program Pas0103(arg);
uses
    SysUtils;
var
    i:integer;
begin
    for i := 1 to 9 do
    begin
        write(format('%d, ', [i]));
    end;
    writeln();
end.
Z:\>fpc Pas0103.pp -v0
Free Pascal Compiler version 2.6.2 [2013/02/12] for i386
Copyright (c) 1993-2012 by Florian Klaempfl and others

Z:\>Pas0103
1, 2, 3, 4, 5, 6, 7, 8, 9,

Ada

VB.NET

Module VB0103
    Public Sub Main()
        For i As Integer = 1 To 9
            Console.Write(i & ", ")
        Next
        Console.WriteLine()
    End Sub
End Module
Z:\>vbc -nologo VB0103.vb

Z:\>VB0103
1, 2, 3, 4, 5, 6, 7, 8, 9,

C#

public class CS0103
{
    public static void Main()
    {
        for (int i = 1; i < 10; i++)
        {
            System.Console.Write(i + ", ");
        }
        System.Console.WriteLine();
    }
}
Z:\>csc -nologo CS0103.cs

Z:\>CS0103
1, 2, 3, 4, 5, 6, 7, 8, 9,

Java

public class Java0103 {
     public static void main(String []args) {
        for (int i = 1; i < 10; i++) {
            System.out.print(i + ", ");
        }
        System.out.println();
     }
}
Z:\>javac Java0103.java

Z:\>java Java0103
1, 2, 3, 4, 5, 6, 7, 8, 9,

C++

#include <iostream.h>

int main()
{
    for (int i = 1; i < 10; i++)
    {
        cout << i << ", ";
    }
    cout << endl;;

    return 0;
}
Z:\>bcc32 CP0103.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
CP0103.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Z:\>CP0103
1, 2, 3, 4, 5, 6, 7, 8, 9,

Objective-C

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    int i;
    for (i = 1; i < 10; i++)
    {
        printf("%d, ", i);
    }
    printf("\n");

    return 0;
}
Compiling the source code....
$gcc `gnustep-config --objc-flags` -L/usr/GNUstep/System/Library/Libraries -lgnustep-base main.m -o demo -lm -pthread -lgmpxx -lreadline 2>&1

Executing the program....
$demo
1, 2, 3, 4, 5, 6, 7, 8, 9, 

D

Go

Scala

object Scala0103 {
    def main(args:Array[String]) {
        for (i <- 1 to 9) {
            print(i + ", ")
        }
        println()
    }
}
Z:\>scala Scala0103.scala
1, 2, 3, 4, 5, 6, 7, 8, 9,

F#

module Fs0103

for i in [1..9] do
    printf "%d, " i
printfn ""

exit 0
Z:\>fsi Fs0103.fs --nologo --quiet
1, 2, 3, 4, 5, 6, 7, 8, 9,

Clojure

(loop [n 1]
    (if (< n 10)
        (do 
            (printf "%d, " n)
            (recur (inc n)))
        (println "")))
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clj0103.clj
1, 2, 3, 4, 5, 6, 7, 8, 9,

Haskell

import Text.Printf 
import Control.Monad

main = do
    forM [1..9::Int] $ \i -> printf "%d, " i
Z:\>runghc Hs0103.hs
1, 2, 3, 4, 5, 6, 7, 8, 9,
inserted by FC2 system