.NET C#基础:While & do-while

介绍

do-while 和 while 语句通过重复代码块直到满足条件来控制代码执行流。

学习目标

  • 利用 do-while 循环循环循环访问代码块。

  • 实现 while 循环以遍历代码块。

开发人员的先决条件

  • 熟悉使用语句。if

  • 熟练使用和迭代语句。foreach for

  • 写作表达的能力。Boolean

  • 使用类和方法生成随机数的知识。System.RandomRandom.Next()

开始

什么是do-while循环?

只要指定的布尔表达式保持 true,do-while 语句就会运行语句或语句块。由于此表达式在每次循环执行后计算,因此 do-while 循环至少执行一次。

示例:do-while

让我们创建连续生成从 1 到 10 的随机数的代码,直到生成数字 7。数字 7 可以在一次迭代中生成,也可以在多次迭代后生成。

首先,在控制台应用程序中创建一个名为“”的静态类文件。将提供的代码片段插入到此文件中。WhileLoop.cs

/// <summary>
/// Outputs
/// 2
/// 5
/// 8
/// 2
/// 7
/// </summary>
public static void DoWhileLoopExample()
{
    Random random = new Random();
    int current = 0;

    do
    {
        current = random.Next(1, 11);
        Console.WriteLine(current);
    } while (current != 7);
}

从 main 方法执行代码,如下所示

#region Day 5 - While & do-while  
  
WhileLoop.DoWhileLoopExample();  
  
#endregion

控制台输出

2  
5  
8  
2  
7

示例:while

该语句将基于布尔表达式进行迭代。为此,请将另一个方法添加到同一个静态类中,如下所示while

/// <summary>
/// Outputs
/// 9
/// 7
/// 5
/// Last number: 1
/// </summary>
public static void WhileLoopExample()
{
    Random random = new Random();
    int current = random.Next(1, 11);

    while (current >= 3)
    {
        Console.WriteLine(current);
        current = random.Next(1, 11);
    }
    Console.WriteLine($"Last number: {current}");
}

从 main 方法执行代码,如下所示

#region Day 5 - While & do-while  
  
WhileLoop.WhileLoopExample();  
  
#endregion

控制台输出

9  
7  
5  
Last number: 1

使用 do-while 继续语句

有时,开发人员需要跳过代码块中的其余代码,然后继续进行下一次迭代。为此,请将另一个方法添加到同一静态类中,如下所示

/// <summary>
/// Outputs
/// 5
/// 1
/// 6
/// 7
/// </summary>
public static void ContinueDoWhileLoopExample()
{
    Random random = new Random();
    int current = random.Next(1, 11);

    do
    {
        current = random.Next(1, 11);

        if (current >= 8) continue;

        Console.WriteLine(current);
    } while (current != 7);
}

从 main 方法执行代码,如下所示

#region Day 5 - While & do-while  
  
WhileLoop.ContinueDoWhileLoopExample();  
  
#endregion

控制台输出

5  
1  
6  
7

相关推荐

  1. .NET C#基础While & do-while

    2024-06-18 11:30:02       8 阅读
  2. Python循环语句——while循环的基础应用

    2024-06-18 11:30:02       32 阅读
  3. do whilewhile

    2024-06-18 11:30:02       37 阅读
  4. while循环

    2024-06-18 11:30:02       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 11:30:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 11:30:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 11:30:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 11:30:02       18 阅读

热门阅读

  1. ncnn 和 rknn 自定义算子对比实现

    2024-06-18 11:30:02       7 阅读
  2. 如何安全的进行数据获取!!

    2024-06-18 11:30:02       10 阅读
  3. Scala入门介绍

    2024-06-18 11:30:02       9 阅读
  4. vue 弹出消息框

    2024-06-18 11:30:02       8 阅读
  5. Hadoop Namenode节点迁移

    2024-06-18 11:30:02       7 阅读
  6. 面向对象编程基本概念

    2024-06-18 11:30:02       9 阅读
  7. 543. 二叉树的直径

    2024-06-18 11:30:02       7 阅读
  8. leetcode56 合并区间

    2024-06-18 11:30:02       7 阅读
  9. Android Intent的几种用法全面总结

    2024-06-18 11:30:02       6 阅读
  10. css3多列布局

    2024-06-18 11:30:02       6 阅读