C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环

break、while

break是跳出当前循环,不是所有循环。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1;
            int j = 1;
            while (i <= 10)
            {
                while (j <= 10)
                {
                    Console.WriteLine("我是最里层");
                    j++;
                    break;
                }
                Console.WriteLine("我是最外层----");
                i++;
            }
            Console.ReadKey();
        }
    }
}

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第1张图片

但是,break不单独使用,而与if配合使用,当满足一定条件就不再循环了。如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            string answer = "n";
            int i = 1;
            while (answer == "n" && i <= 10)
            {
                Console.WriteLine("这是第{0}次讲解,你会了吗?y/n", i);
                answer = Console.ReadLine();
                if (answer == "y")
                {
                    Console.WriteLine("你终于会了!放学了。。。");
                    break;
                }

                i++;
            }
            if (i == 11)
            {
                Console.WriteLine("还不会,第{0}次了!放弃吧!", i-1);
            }
            Console.ReadKey();
        }
    }
}

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第2张图片

continue

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第3张图片

do-while循环

先执行一遍,再拿执行后的结果,再循环的,推荐do-while,即先做再判断:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "n";
            do
            {
                if (input != "n")
                {
                    Console.WriteLine("要输入n");
                }
                Console.WriteLine("y/n?");
                input = Console.ReadLine();

            } while (input == "n"||input!="y");
            Console.ReadKey();
        }
    }
}

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第4张图片

for循环

打for顺序循环双击tab,代码格式化输出。
打forr逆序循环双击tab,代码格式化输出。
变步长:for (int i = 0; i < 100; i+=2)
求水仙花数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            int hundred = 0;
            int decade = 0;
            int unit = 0;

            for (int i = 100; i <= 999; i++)
            {
                hundred = i / 100;
                decade = i % 100 / 10;
                unit = i % 10;
                if (Math.Pow(hundred, 3) + Math.Pow(decade, 3) + Math.Pow(unit, 3) == i)
                {
                    Console.WriteLine("水仙花数为:{0}", i);
                }
            }
            Console.ReadKey();
        }
    }
}

九九乘法表

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第5张图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <=9; j++)
                {
                    Console.Write("{0}*{1}={2}\t", i, j,i * j);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第6张图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <=i; j++)
                {
                    Console.Write("{0}*{1}={2}\t", i, j,i * j);
                    if (i == j)
                    {
                        Console.WriteLine();
                    }
                }

            }
            Console.ReadKey();
        }
    }
}

质数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            //怎么判断质数(素数)
            Console.WriteLine("输入大于2的数字:");
            int length = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("质数:1");
            for (int i = 2; i <= length; i++)
            {
                bool is_prime = true;
                for (int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        is_prime = false;
                        break;//不是质数
                    }
                }
                if (is_prime)
                {
                    Console.WriteLine("质数:{0}", i);
                }
            }

            Console.ReadKey();


        }
    }
}

C#基础(4)——break语句、continue语句、while循环、do-while循环、for循环、foreach循环_第7张图片

你可能感兴趣的:(C#)