LoginSignup
0

More than 3 years have passed since last update.

for文のbreakとcontinueの使用例

Last updated at Posted at 2020-01-18
class Program
{
    static void Main(string[] args)
    {
        //Program2のインスタンスを生成
        Program2 obj = new Program2();

        //Program2クラスのstarメソッドを実行
        obj.star();
    }
}

class Program2
{
    public void star()
    {
        //五回繰り返す
        for (int i=1;i<=5;i++)
        {
            //変数jが変数iの数以下になるまで繰り返す
            for (int j = 5; j >= i; j--)
            {
                //変数iが3の時はfor文を抜ける(Console.Write("☆")は実行されない)
                if (i == 3)
                {
                    break;
                }
                Console.Write("☆");
            }

            //変数iのが3の時はcontinue以下の処理は実行しない
            if (i == 3)
            {
                continue;
            }
            Console.WriteLine("");
        }
    }
}

【結果】

☆☆☆☆☆
☆☆☆☆
☆☆
☆

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0