C# 循环语句:while,for,foreach,do while,break,continue,嵌套循环,无限循环(学习心得 10)

满足一定条件下,多次执行同一块代码。

一、循环类型

1.1 while 循环

语法:

while(condition)
{
   statement(s);
}

condition 可以是任意的表达式,当为任意非零值时都为真。

例:

using System;

namespace Loops
{
   
    class Program
    {
        static void Main(string[] args)
        {
            /* 局部变量定义 */
            int a = 10;

            /* while 循环执行 */
            while (a < 12)
            {
                Console.WriteLine("a 的值: {0}", a);
                a++;
            }
            Console.ReadLine();
        }
    }
}

运行结果:

a 的值: 10
a 的值: 11

1.2 for/foreach 循环

语法:

for ( init; condition; increment )
{
   statement(s);
}

例:

using System;

namespace Loops
{
   
    class Program
    {
        static void Main(string[] args)
        {
            /* for 循环执行 */
            for (int a = 10; a < 12; a = a + 1)
            {
                Console.WriteLine("a 的值: {0}", a);
            }
            Console.ReadLine();
        }
    }
}

运行结果:

a 的值: 10
a 的值: 11

foreach 循环:迭代数组/集合对象。

例:

namespace ForEachTest
{
    class ForEachTest
    {
        static void Main(string[] args)
        {
            int[] fibarray = new int[] { 0, 1, 1, 2, 3 };
            foreach (int element in fibarray)
            {
                System.Console.WriteLine(element);
            }
            System.Console.WriteLine();


            // 类似 foreach 循环
            for (int i = 0; i < fibarray.Length; i++)
            {
                System.Console.WriteLine(fibarray[i]);
            }
            System.Console.WriteLine();


            // 设置集合中元素的计算器
            int count = 0;
            foreach (int element in fibarray)
            {
                count += 1;
                System.Console.WriteLine("Element #{0}: {1}", count, element);
            }
            System.Console.WriteLine("Number of elements in the array: {0}", count);
        }
    }
}

运行结果:

0
1
1
2
3

0
1
1
2
3

Element #1: 0
Element #2: 1
Element #3: 1
Element #4: 2
Element #5: 3
Number of elements in the array: 5

1.3 do while 循环

为尾部进行条件检查。

确保循环至少执行一次。

语法:

do
{
   statement(s);

}while( condition );

例:

using System;

namespace Loops
{
   
    class Program
    {
        static void Main(string[] args)
        {
            /* 局部变量定义 */
            int a = 10;

            /* do 循环执行 */
            do
            {
               Console.WriteLine("a 的值: {0}", a);
                a = a + 1;
            } while (a < 13);

            Console.ReadLine();
        }
    }
}

运行结果:

a 的值: 10
a 的值: 11
a 的值: 12

1.4 嵌套循环

嵌套 for 循环语法:

for ( init; condition; increment )
{
   for ( init; condition; increment )
   {
      statement(s);
   }
   statement(s);
}

嵌套 while 循环语法:

while(condition)
{
   while(condition)
   {
      statement(s);
   }
   statement(s);
}

嵌套 do while 循环语法:

do
{
   statement(s);
   do
   {
      statement(s);
   }while( condition );

}while( condition );

例:寻找2~100中的质数。

质数又称素数。

一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数。

using System;

namespace Loops
{

    class Program
    {
        static void Main(string[] args)
        {          
            int i, j; // 定义局部变量

            for (i = 2; i < 100; i++)
            {
                for (j = 2; j <= i ; j++)
                    if ((i % j) == 0) break; // 如果找到,则不是质数
                if (j >= i )
                    Console.WriteLine("{0} 是质数", i);
            }

            Console.ReadLine();
        }
    }
}

运行结果:

2 是质数
3 是质数
5 是质数
7 是质数
11 是质数
13 是质数
17 是质数
19 是质数
23 是质数
29 是质数
31 是质数
37 是质数
41 是质数
43 是质数
47 是质数
53 是质数
59 是质数
61 是质数
67 是质数
71 是质数
73 是质数
79 是质数
83 是质数
89 是质数
97 是质数

二、循环控制语句

2.1 break

两种用法:

  • 循环体内 break,循环终止。
  • switch 中 break,终止一个 case。
  • 如果是循环嵌套,break 停止最内层循环。

2.2 continue

用法:

  • 跳出当前循环,进入下一次循环。
  • for 循环,跳到执行条件测试和循环增量。
  • while 和 do while 循环,跳到条件测试。

例:

using System;

namespace Loops
{
   
    class Program
    {
        static void Main(string[] args)
        {
            /* 局部变量定义 */
            int a = 10;

            /* do 循环执行 */
            do
            {
                if (a == 15)
                {
                    /* 跳过迭代 */
                    a = a + 1;
                    continue;
                }
                Console.WriteLine("a 的值: {0}", a);
                a++;

            } while (a < 20);
 
            Console.ReadLine();
        }
    }
}

运行结果:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19

可见其中跳过了一次 a=15 的迭代。

三、无限循环

条件永远为真,则无限循环。

例:

using System;

namespace Loops
{
   
    class Program
    {
        static void Main(string[] args)
        {
            for (; ; )
            {
                Console.WriteLine("Hey! I am Trapped");
            }
 
        }
    }
}

或者用 while 也行:

using System;

namespace LoopsInfi
{
    class Program
    {
        static void Main(string[] args)
        {
            while(true)
            {
                Console.WriteLine("Hey! I am Trapped");
            }

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42067550/article/details/106769613