优秀的编程知识分享平台

网站首页 > 技术文章 正文

【2.C#基础】6.循环语句

nanyue 2025-02-27 16:09:06 技术文章 7 ℃

6.循环语句

当需要多次执行同一个处理时,就需要用到循环语句。一般情况下,循环的流程图如下:

6.1 while循环

C# 中的 while 循环语句在给定的条件为真的情况下会重复执行目标语句。 格式如下:

while(条件表达式)
{
   处理;
}

当条件为真时执行循环,当条件为假时,程序将继续执行紧接着循环的下一条语句。

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int i = 10;
        while(i<20)
        {
            Debug.Log("i的值:" + i);
            i++;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

控制台输出:

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

6.2 for循环

for 循环是允许编写一个执行特定次数的循环的重复控制结构。 格式如下:

for(初始化语句; 判断条件; 迭代器){ 
    处理;
} 

首先执行初始化语句(通常是一个变量),并且只执行一次,在某些情况下初始化语句可以省略,只保留后面的分号即可;接下来进行条件判断,如果为 true,则执行循环主体,如果为假,则跳出 for 循环,执行 for 循环以外的代码;循环主体执行完成后,更新迭代器的值(增加或减少),然后再进行条件判断,如果为真则再次执行循环主体,重复执行此步骤,直至判断条件为假,跳出循环。

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        for(int i=10; i<20; i++)
        {
            Debug.Log("i的值:" + i);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

控制台输出与上面相同。

在上面的 for 循环语句中:

接下来,再举几个例子:

示例:输出1-100之间的偶数。

for(int i=2; i<=100; i+=2)
{
    Debug.Log("i的值:" + i);
}

示例:计算1到100的和。

int s = 0;
for(int i=1; i<=100; i++)
{
    s += i;
}
Debug.Log("s的值:" + s);

6.3 do while 循环

do while 循环的语法格式如下:

do{
    处理;        // 要执行的代码
}while(表达式);

do while 循环中,程序会先执行do{ }中的循环主体,执行完成后再去判断 while( ) 中的表达式,如果表达式为真,则继续执行 do{ } 中的循环主体,如果表达式为假,则跳出 do while 循环。

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int s = 0, i = 1;
        do {
            s += i++;
        } while(i<=100);
        Debug.Log("s的值:" + s);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

控制台输出:

s的值:5050
UnityEngine.Debug:Log(Object)
Do:Start() (at Assets/Do.cs:14)

6.4 跳出循环

6.4.1 break

在 C# 中, break 语句有以下两种用法:

  1. break 语句出现在一个循环内时,循环会立即终止,且程序流将继续执行紧接着循环的下一条语句。
  2. 它可用于终止 switch 语句中的一个 case。

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int s = 0, i = 1;
        while (true) {
            s += i++;
            if (i > 100) break;
        }
        Debug.Log("s的值:" + s);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

控制台输出与上例相同。在上面的 while 语句中,条件表达式是 true,在执行循环的过程中,通过 if (i > 100) break; 退出循环。

6.4.2 continue

C# 中的 continue 语句有点像 break 语句。但它不是强迫终止,continue 会跳过当前循环中的代码,强迫开始下一次循环。

对于 for 循环,continue 语句会导致执行条件测试和循环增量部分。对于 whiledo...while 循环,continue 语句会导致程序控制回到条件测试上。

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int s = 0, i = 1;
        while (i <= 100) {
            if (i % 2 == 1) 
            {
                i++;
                continue;
            }          
            s += i++;
        }
        Debug.Log("s的值:" + s);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

控制台输出:

s的值:2550
UnityEngine.Debug:Log(Object)
Do:Start() (at Assets/Do.cs:19)

其中:

            if (i % 2 == 1) 
            {
                i++;
                continue;
            }

语句的作用就是当 i 的值为奇数时,跳入下一次循环,% 运算符为求余运算符。使得上面程序实现了计算偶数的和。

6.5 goto语句

C# 中的 goto 语句也称为跳转语句,使用它可以控制程序跳转到指定的位置执行。不过并不建议在程序中多次使用 goto 语句,因为它会使程序变得更加复杂。goto 语句的格式如下所示:

goto 标签名;

标签名:
    语句块;

示例:通过 goto 语句实现 1 ~ 100 之间整数的求和。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Do : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int s = 0, i = 1;
        sum:
            s += i++;
            if (i>100) goto end;
        goto sum;
        end:
            Debug.Log("s的值:" + s);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

控制台输出:

s的值:5050
UnityEngine.Debug:Log(Object)
Do:Start() (at Assets/Do.cs:16)



最近发表
标签列表