制御文

if

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
 
            string a;
            Console.Write("データを入力->");
            a = Console.ReadLine();
 
            if (a == "a")
            {
                Console.WriteLine("a入力");
            }
            else if (a == "b")
            {
                Console.WriteLine("b入力");
            }
            else
            {
                Console.WriteLine("a,b以外入力");
            }
 
            Console.ReadKey();
        }
    }
}
 
if elseif else文を記述

switch

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
 
            Console.Write("データを入力->");
            str = Console.ReadLine();
 
            switch (str)
            {
                case "1":
                    Console.WriteLine("1が入力されました");
                    break;
                case "2":
                    Console.WriteLine("2が入力されました");
                    break;
                case "3":
                    Console.WriteLine("3が入力されました");
                    break;
                default:
                    Console.WriteLine("以外が入力されました");
                    break;
            }
 
            Console.ReadKey();
        }
    }
}
 

for

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
 
            // 回数指定
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("for Test" + (i+1));
            }
            Console.WriteLine();
 
            // 永久ループなので終了条件を指定
            int j = 0;
            for (; ; )
            {
                if (j > 10)
                {
                    break;
                }
                Console.WriteLine("for Test" + (j + 1));
                j++;
            }
 
 
            Console.ReadKey();
 
        }
    }
}
 
 
for(;;)で永久ループになるので終了条件を内部に指定する必要があります

while

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication21
{
    class Program
    {
        static void Main(string[] args)
        {
            // 継続条件を指定
            int i = 0;
            while (i < 10)
            {
                Console.WriteLine("while test " + i);
                i++;
            }
            Console.WriteLine();
 
            // 永久ループ系は終了条件を内部に設定
            int j = 0;
            while (true)
            {
                if (j > 10)
                {
                    break;
                }
                Console.WriteLine("while test " + j);
                j++;
            }
 
            Console.ReadKey();
        }
    }
}
 
while(True)で永久ループになるので終了条件を内部に指定する必要があります

do while

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication22
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 11;
            do{
                Console.WriteLine("do while test " + i);
                i++;
            }while(i < 10);
            Console.ReadKey();
        }
    }
}
 
do whileは一度は内部の処理が一度は実行されます

goto

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication23
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
 
            Console.Write("データを入力->");
            str = Console.ReadLine();
 
            if (str == "1")
            {
                goto jump1;
            }
            else if (str == "2")
            {
                goto jump2;
            }
            Console.WriteLine("Sample ");
 
        jump1:
            Console.WriteLine("Sample Test1");
 
        jump2:
            Console.WriteLine("Sample Test2");
 
        jump3:
            Console.ReadKey();
 
        }
    }
}
 
「ラベル名:」でラベルを貼った場所にジャンプしますが、以降の処理は実行することに注意
※上記の例ではjump1へ飛んでもjump2以降の処理を実行します
※この機能は非推奨とします

break、continue

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication24
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            for (int i = 0; i < 10; i++)
            {
                a = i % 2;
                if (a == 0)
                {
                    continue;
                }
                if (i > 7)
                {
                    break;
                }
                Console.WriteLine("Sample Test " + i);
            }
            Console.ReadKey();
        }
    }
}
 
breakはforやwhileなどのブロック処理を中断
continueは処理を先頭へ戻す


最終更新:2011年03月06日 19:45