C# 入门之循环

在 C# 中,支持三类循环:

  • do...while 循环
  • while 循环
  • for/foreach 循环

do...while 循环

do...while 循环的语法格式:

do
{
    code to loop
} while (<condition>);

循环体内的代码,是重复执行的代码,每次执行完代码都会检查一下条件,是否满足退出循环的条件,只要条件满足,即退出循环,执行循环后面的代码。

让我们来看一下 do...while 循环的示例:计算 1 到 100 之间所有整数的和,

using System;

namespace doWhile
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0, i = 0;
            do
            {
                sum = sum + i;
                i = ++i;
            } while (i <= 100);
            Console.WriteLine($"The sum is {sum}.");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210906/20099494Y1KRxNTwwI.png

while 循环

while 循环的语法格式:

while (<condition>)
{
    code to loop
}

while 和 do...while 很类似,除了循环测试条件的位置,其他的几乎没有区别;

while 循环和 do...while 循环很类型,区别在于结束循环的条件,在循环体前面。
下面我们来看一下 while 循环的示例:100以内所有偶数的个数。

using System;

namespace whileTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 1, j = 0;
            while (i < 100)
            {
                if (i % 2 == 0)
                {
                    j = ++j;
                }
                i = ++i;
            }     
            Console.WriteLine($"{j}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210906/20099494QCZDWJs8Xy.png

for/foreach 循环

for 循环也被称为遍历循环,它可以遍历一个序列内的所有内容。由于它可以遍历一个序列的所有内容,我们可以使用 for 循环来遍历一组有序的数字,从而实现指定次数的循环。

for 循环的语法格式:

for ( <initialization>; <condition>; <operation>)
{
    code to loop
}

下面看一个 for 循环的示例: 通过 for 循环的方式求 1 到 100 之间的整数和

using System;

namespace forTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            for (int a = 1; a <= 100; a++)
            {
                sum = sum + a;
            }
            Console.WriteLine($"{sum}");
        }
    }
}

运行结果:
https://ithelp.ithome.com.tw/upload/images/20210906/20099494CUVTexu2YB.png

我们再来看看 for 的另外一种用法:
下面我们来看一个示例:找出一个数组中的所有偶数(关于数组,可以查看最后的补充说明。)

using System;

namespace forTest2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };   // 声明一个数组
            for (int i = 0; i < num.Length; i++)
            {
                if (num[i] % 2 == 0)
                {
                    Console.WriteLine(num[i]);
                }
            }
        }
    }
}

运算结果:
https://ithelp.ithome.com.tw/upload/images/20210906/20099494jRvFsXWOAS.png

其实我们还可以通过 foreach 来实现这种遍历,并且更加简单:

using System;

namespace foreachTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            foreach (int i in num)
            {
                if (i % 2 == 0)
                {
                    Console.WriteLine($"{i}");
                }
            }           
        }
    }
}

运行结果
https://ithelp.ithome.com.tw/upload/images/20210906/20099494rMwsFMuCKk.png

补充说明:

数组是一个存储相同类型元素的有序的集合。
数组的声明格式:

type[] arrayName;

https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/arrays/

C# 中的注释:

// 单行注释

/*
多行
注释
/*

<<:  JAVA - Windows 10 建立 Maven 专案并执行

>>:  Day 6 ( 入门 ) 抽奖轮盘

Day 04:金鱼记忆力太短暂,交给外挂记吧!autosuggestions 与 substring-search

更新 我把从第一天到现在每天的 Home 目录都放上 GitHub 了,README.md 里面有...

Day 24【Random Picture Blending in Python】return bool;

【前言】 在 Project 之中刚好有一份工作是要把每个部件合成,虽然跟主题没有关联不过因为篇幅...

Day2-"基本介绍+基本运算"

#include的功能用於引入标头档 标头档就是包含某些函式内容的函式库档案 这些标头档可能是由编译...

Day 09: Creational patterns - Prototype

目的 实践物件的 Deep Copy,避免 Shallow Copy 的问题以及省去重新制作物件。 ...

[C 语言笔记--Day15] 如何清空终端机

// clear.c #include <stdio.h> int main() { p...