📜  C#中的循环

📅  最后修改于: 2021-05-30 00:41:07             🧑  作者: Mango

以编程语言循环是一种根据要评估执行语句的条件的结果多次执行一条语句或一组语句的方法。结果条件应为true,才能在循环内执行语句。

循环主要分为两类:
入口控制回路:在回路主体开始处存在要测试条件的回路称为入口控制回路while循环for循环是入口控制的循环。

1. while循环测试条件是在循环的开始给出的,所有语句都将执行,直到当条件变为false时给定的布尔条件满足为止,控件将退出while循环。

句法:

while (boolean condition)
{
   loop statements...
}

流程图:
while循环

例子:

// C# program to illustrate while loop
using System;
  
class whileLoopDemo
{
    public static void Main()
    {
        int x = 1;
   
        // Exit when x becomes greater than 4
        while (x <= 4)
        {
            Console.WriteLine("GeeksforGeeks");
   
            // Increment the value of x for
            // next iteration
            x++;
        }
    }
}

输出:

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

2. for循环
for循环与while循环具有相似的功能,但语法不同。当预先知道要执行循环语句的次数时,首选for循环。在for循环的一行中完成了循环变量的初始化,要测试的条件以及循环变量的递增/递减,从而提供了一种较短,易于调试的循环结构。

for (loop variable initialization ; testing condition; 
                              increment / decrement)
{    
    // statements to be executed
}

流程图:
Java中的循环

1.循环变量的初始化:在此处初始化控制循环的表达式/变量。它是for循环的起点。可以使用已经声明的变量,也可以声明一个变量,仅局部于循环。
2.测试条件:执行循环语句的测试条件。用于测试循环的退出条件。它必须返回布尔值true或false 。当条件变为假时,控件将退出循环,for循环结束。
3.递增/递减:循环变量根据要求递增/递减,然后控制再次移至测试条件。

注意: for循环启动时,初始化部分仅被评估一次。

例子:

// C# program to illustrate for loop.
using System;
  
class forLoopDemo
{
    public static void Main()
    {
        // for loop begins when x=1
        // and runs till x <=4
        for (int x = 1; x <= 4; x++)
            Console.WriteLine("GeeksforGeeks");
    }
}

输出:

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

出口受控循环:循环体末端存在测试条件的循环称为出口受控循环do-while是退出控制的循环。
注意:在退出受控循环中,由于循环主体末尾存在测试条件,因此至少要评估一次循环主体。

1. do-while循环
do while循环与while循环类似,唯一的区别是它在执行语句后检查条件,即它将肯定执行一次循环主体,因为它在执行语句后检查条件。

句法 :

do
{
    statements..
}while (condition);

流程图:
在做

例子:

// C# program to illustrate do-while loop
using System;
  
class dowhileloopDemo
{
    public static void Main()
    {
        int x = 21;
        do
        {
            // The line will be printed even
            // if the condition is false
            Console.WriteLine("GeeksforGeeks");
            x++;
        }
        while (x < 20);
    }
}

输出:

GeeksforGeeks

无限循环:
测试条件不评估为假的循环永远倾向于永远执行语句,直到使用外力将其终止为止,因此它们被称为无限循环。

例子:

// C# program to demonstrate infinite loop
using System;
  
class infiniteLoop
{
    public static void Main()
    { 
        // The statement will be printed
        // infinite times
        for(;;)
        Console.WriteLine("This is printed infinite times");
    }
}

输出:

This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
..........

嵌套循环:
如果其他循环中存在循环,则称为嵌套循环。

例子:

// C# program to demonstrate nested loops
using System;
  
class nestedLoops
{
    public static void Main()
    {
        // loop within loop printing GeeksforGeeks
       for(int i = 2; i < 3; i++)
             for(int j = 1; j < i; j++)
                 Console.WriteLine("GeeksforGeeks");
    }
}

输出:

GeeksforGeeks

继续声明:
continue语句用于在特定条件下跳过循环的执行部分,并将流程移至下一个更新部分。
流程图:

例子:

// C# program to demonstrate continue statement
using System;
  
class demoContinue
{
    public static void Main()
    {    
        // GeeksforGeeks is printed only 2 times
        // because of continue statement
        for(int i = 1; i < 3; i++)
        {
            if(i == 2)
              continue;
              
            Console.WriteLine("GeeksforGeeks"); 
        }
    }
}  

输出:

GeeksforGeeks