IfElse - natukikazemizo/Sedna1.0 GitHub Wiki

色々な言語で If Else

本ページの作成目的

2018年現在,私がコーディングの際に,If Elseの書きかたがどれだったか混乱することが多くなったので,知識を整理した。

制限事項

Googleで調査しながら記述しました。
コードの動作確認は未実施です。

ASP

<%
    a = 1
    If a = 0 Then
        Response.Write "a is 0."
    ElseIf a = 1 Then
        Response.Write "a is 1."
    Else
        Response.Write "a is not in (0,1)."
    End If
%>

bash

declare -i a=0
if [ $a -eq 0]; then
    echo 'a is 0.'
elif [ $a -eq 1]; then
    echo 'a is 1.'
else
    echo 'a is not in (0,1).'
fi

BAT

SET a = 0
IF %a%==0 (
    echo "a is 0."
) else if %a%==1 (
    echo "a is 1."
) else (
    echo "a is not in (0,1)."
)

C言語

int a;
a = 0;
if (a == 0) {
    printf("a is 0.");
} else if (a == 1) {
    printf("a is 1.");
} else {
    printf("a is not in (0,1).");
}

Excel 数式

=IF($A$1=0, "a is 0.", IF($A$1=1, "a is 1.", "a is not in (0,1)."))

Java

int a = 0;
if (a == 0) {
    System.out.println("a is 0.");
} else if (a == 1) {
    System.out.println("a is 1.");
} else {
    System.out.println("a is not in (0,1).");
}

JavaScript

var a = 0
if (a == 0) {
    document.write("a is 0.");
} else if (a == 1) {
    document.write("a is 1.");
} else {
    document.write("a is not in (0,1).");
}

Perl

my $a = 0;
if ($a == 0) {
    print "a is 0.";
} elsif ($a == 1) {
    print "a is 1.";
} else {
    print "a is not in (0,1).";
}

PL/SQL

DECLARE
    a NUMBER := 0;
BEGIN
    IF a = 0 THEN
        DBMS_OUTPUT.PUT_LINE('a is 0.');
    ELSIF a = 1 THEN
        DBMS_OUTPUT.PUT_LINE('a is 1.');
    ELSE
        DBMS_OUTPUT.PUT_LINE('a is not in (0,1).');
    END IF;
END;
/ 

Power Shell

$a = 0
if ( $a -eq 0) {
    Write-Host("a is 0.")
} elseif ($a -eq 1) {
    Write-Host("a is 1.")
} else {
    Write-Host("a is not in (0,1).")
}

Python 3系

a = 0
if a == 0:
    print('a is 0.')
elif a == 1:
    print('a is 1.')
else:
    print('a is not in (0,1).')

Oracle SQL 文

SELECT
    CASE
    WHEN A = 0 THEN 'a is 0.'
    WHEN A = 1 THEN 'a is 1.'
    ELSE 'a is not in (0,1).' END "RESULT"
FROM
(SELECT 1 AS A FROM DUAL);

VBA

Dim a As Integer = 0
If a = 0 Then
    MsgBox "a is 0."
ElseIf a = 1 Then
    MsgBox "a is 1."
Else
    MsgBox "a is not in (0,1)."
End If

VB.Net

Dim a As Integer = 0
If a = 0 Then
    Console.WriteLine("a is 0.")
ElseIf a = 1 Then
    Console.WriteLine("a is 1.")
Else
    Console.WriteLine("a is not in (0,1).")
End If