やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

C#クイックスタート(分岐とループ)

 基本構文。

成果物

情報源

プロジェクト作成

dotnet new console -o BranchesAndLoops
cd BranchesAndLoops

コード抜粋

条件分岐

if

if (true) Console.WriteLine("if");
if (false) Console.WriteLine("if");
else Console.WriteLine("else");
if (false) Console.WriteLine("if");
else if (true) Console.WriteLine("else if");
else Console.WriteLine("else");

三項演算子

(true) ? Console.WriteLine("true") : Console.WriteLine("false");

switch

int v = (true) ? 1 : 2;
(条件式) ? 真のときの値 : 偽のときの値

ループ

 以下のキーワードでループを抜けることができる。今回これらについては省略する。

while

int counter = 0;
while (counter < 10)
{
    Console.WriteLine($"Hello World! The counter is {counter}");
    counter++;
}

 以下のように書くこともできる。if文がなくなれば無限ループになってしまう。

int counter = 0;
while (true)
{
    if (3 < counter) break;
    Console.WriteLine($"Hello World! The counter is {counter}");
    counter++;
}

do while

int counter = 0;
do
{
    Console.WriteLine($"Hello World! The counter is {counter}");
    counter++;
} while (counter < 10);

for

for (int i=0; i < 3; i++)
    Console.WriteLine($"{i}");
}

 以下のように書くこともできる。if文がなくなれば無限ループになってしまう。

int index = 0;
for (;;)
{
    if (3 < index) break;
    Console.WriteLine($"Hello World! The index is {index}");
    index++;
}

foreach

using System.Collections.Generic;

foreach (int i in new List<int>(){1,3,5})
{
    Console.WriteLine($"foreach {i}");
}

コード

Program.cs

using System;

namespace BranchesAndLoops
{
    class Program
    {
        static void Main(string[] args)
        {
            new If().Run();
            new Loop().Run();
        }
    }
}

If.cs

using System;

namespace BranchesAndLoops
{
    class If
    {
        public void Run()
        {
            code0();
            code1();
            code2();
            code3();
            code4();
            code5();
            code6();
            code7();
            code8();
            code9();
        }
        private void code0() { if (true) Console.WriteLine("if"); }
        private void code1()
        {
            if (false) Console.WriteLine("if");
            else Console.WriteLine("else");
        }
        private void code2()
        {
            if (false) Console.WriteLine("if");
            else if (true) Console.WriteLine("else if");
            else Console.WriteLine("else");
        }
        private void code3()
        {
            int a = 5;
            int b = 6;
            if (a + b > 10)
                Console.WriteLine("The answer is greater than 10.");
            b = 3;
            if (a + b > 10)
                Console.WriteLine("The answer is greater than 10.");
        }
        private void code4()
        {
            int a = 5;
            int b = 3;
            if (a + b > 10)
                Console.WriteLine("The answer is greater than 10");
            else
                Console.WriteLine("The answer is not greater than 10");
        }
        private void code5()
        {
            int a = 5;
            int b = 3;
            if (a + b > 10)
            {
                Console.WriteLine("The answer is greater than 10");
            }
            else
            {
                Console.WriteLine("The answer is not greater than 10");
            }
        }
        private void code6()
        {
            int a = 5;
            int b = 3;
            int c = 4;
            if ((a + b + c > 10) && (a == b))
            {
                Console.WriteLine("The answer is greater than 10");
                Console.WriteLine("And the first number is equal to the second");
            }
            else
            {
                Console.WriteLine("The answer is not greater than 10");
                Console.WriteLine("Or the first number is not equal to the second");
            }
        }
        private void code7()
        {
            int a = 5;
            int b = 3;
            int c = 4;
            if ((a + b + c > 10) || (a == b))
            {
                Console.WriteLine("The answer is greater than 10");
                Console.WriteLine("Or the first number is equal to the second");
            }
            else
            {
                Console.WriteLine("The answer is not greater than 10");
                Console.WriteLine("And the first number is not equal to the second");
            }
        }

        private void code8()
        {
            int v = (true) ? 1 : 2;
            Console.WriteLine($"v = {v}");
        }

        private void code9()
        {
            switch (5) {
                case 4:
                    Console.WriteLine("4");
                    break;
                case 5:
                    Console.WriteLine("5");
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
        }
    }
}

Loop.cs

using System;
using System.Collections.Generic;

namespace BranchesAndLoops
{
    class Loop
    {
        public void Run()
        {
            code0();
            code1();
            code2();
            code3();
            code4();
            code5();
        }
        private void code0()
        {
            int counter = 0;
            while (counter < 10)
            {
                Console.WriteLine($"Hello World! The counter is {counter}");
                counter++;
            }
        }
        private void code1()
        {
            int counter = 0;
            do
            {
                Console.WriteLine($"Hello World! The counter is {counter}");
                counter++;
            } while (counter < 10);
        }
        private void code2()
        {
            for (int index = 0; index < 10; index++)
            {
                Console.WriteLine($"Hello World! The index is {index}");
            }
        }
        private void code3()
        {
            foreach (int i in new List<int>(){1,3,5})
            {
                Console.WriteLine($"foreach {i}");
            }
        }
        private void code4()
        {
            int counter = 0;
            while (true)
            {
                if (3 < counter) break;
                Console.WriteLine($"Hello World! The counter is {counter}");
                counter++;
            }
        }
        private void code5()
        {
            int index = 0;
            for (;;)
            {
                if (3 < index) break;
                Console.WriteLine($"Hello World! The index is {index}");
                index++;
            }
        }
    }
}

実行結果

$ dotnet run
If.cs(23,24): warning CS0162: 到達できないコードが検出されました [/tmp/work/CSharp.If.Loop.20191017084732/src/BranchesAndLoops/BranchesAndLoops.csproj]
If.cs(28,24): warning CS0162: 到達できないコードが検出されました [/tmp/work/CSharp.If.Loop.20191017084732/src/BranchesAndLoops/BranchesAndLoops.csproj]
If.cs(30,18): warning CS0162: 到達できないコードが検出されました [/tmp/work/CSharp.If.Loop.20191017084732/src/BranchesAndLoops/BranchesAndLoops.csproj]
If.cs(107,21): warning CS0162: 到達できないコードが検出されました [/tmp/work/CSharp.If.Loop.20191017084732/src/BranchesAndLoops/BranchesAndLoops.csproj]
If.cs(113,21): warning CS0162: 到達できないコードが検出されました [/tmp/work/CSharp.If.Loop.20191017084732/src/BranchesAndLoops/BranchesAndLoops.csproj]
if
else
else if
The answer is greater than 10.
The answer is not greater than 10
The answer is not greater than 10
The answer is not greater than 10
Or the first number is not equal to the second
The answer is greater than 10
Or the first number is equal to the second
v = 1
5
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
Hello World! The index is 0
Hello World! The index is 1
Hello World! The index is 2
Hello World! The index is 3
Hello World! The index is 4
Hello World! The index is 5
Hello World! The index is 6
Hello World! The index is 7
Hello World! The index is 8
Hello World! The index is 9
foreach 1
foreach 3
foreach 5
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The index is 0
Hello World! The index is 1
Hello World! The index is 2
Hello World! The index is 3
120のうち3で割り切れる数の合計は63である。

 いくつかwarningが出ているが問題ない。

初めて知ったこと

 C#7.0以降はswitch文でパターンマッチを表現できるようになったらしい。rust言語のmatch文と同様。

 今回は放置。そのうち学習しよう。

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux