C while circular statement usage example detail

  • 2020-12-05 17:20:10
  • OfStack

This article illustrates the use of while loops in C#. Share to everybody for everybody reference. Specific implementation methods are as follows:

In C#, while loop is a kind of loop statement that we often use. The characteristic of while loop is that it does not jump out of the loop until the condition is zero. Of course, other functions can be used to jump out directly in the middle.

The difference between Foreach and For is that Foreach traverses objects without defining the number of cycles. However, Foreach traverses read-only data and cannot add or delete objects in Foreach, whereas For loops can. The code to change this to while loop is as follows:

int i=0;while(i<ds.Table["userreg"].Rows.Count){i++;}

Examples are as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Interrupt cycle
{
    class Program
    {
        static void Main(string[] args)
        {
            //100 Taken within 7
            int i = 0;
            while (i < 100)
            {
                i++;
                if(i%7==0 || i%10==7||i/10==7)
                {
                    continue;
                }
                Console.WriteLine("{0}",i);
            }
            Console.ReadKey();
        }
    }
}

Supplement:

The differences between return,continue and break in while:
return: Exit the main function
continue: Go straight to the next cycle
break: Jump straight out of the loop

Hopefully this article has helped you with your C# programming.


Related articles: