Do .. While loop in C#?

后端 未结 8 1157
后悔当初
后悔当初 2020-12-18 18:21

How do I write a Do .. While loop in C#?

(Edit: I am a VB.NET programmer trying to make the move to C#, so I do have experience with .NET / VB syntax. Thanks!)

相关标签:
8条回答
  • 2020-12-18 18:38

    Here's a simple example that will print some numbers:

    int i = 0;
    
    do {
      Console.WriteLine(++i);
    } while (i < 10);
    
    0 讨论(0)
  • 2020-12-18 18:39

    Since you mentioned you were coming from VB.NET, I would strongly suggest checking out this link to show the comparisons. You can also use this wensite to convert VB to C# and vice versa - so you can play with your existing VB code and see what it looks like in C#, including loops and anything else under the son..

    To answer the loop question, you simple want to do something like:

    while(condition)
    {
       DoSomething();
    }
    

    You can also do - while like this:

    do
    {
       Something();
    }
    while(condition);
    

    Here's another code translator I've used with success, and another great C#->VB comparison website. Good Luck!

    0 讨论(0)
  • 2020-12-18 18:43

    Apart from the Anthony Pegram's answer, you can use also the while loop, which checks the condition BEFORE getting into the loop

    while (someCriteria)
    {
        if (someCondition)
        {
            someCriteria = false;
            // or you can use break;
        }
        if (ignoreJustThisIteration)
        {
            continue;
        }
    }
    
    0 讨论(0)
  • 2020-12-18 18:47
    //remember, do loop will always execute at least once, a while loop may not execute at all
    //because the condition is at the top
    do
    {
      //statements to be repeated
    } while (condition);
    
    0 讨论(0)
  • 2020-12-18 18:56
    using System;
    
    class MainClass
    {
        public static void Main()
        {
            int i = 0;
            do
            {
                Console.WriteLine("Number is {0}", i);
                i++;
            } while (i < 100);
        }
    }
    

    Another method would be

    using System;
    
    class MainClass
    {
        public static void Main()
        {
            int i = 0;
            while(i <100)
            {
                Console.WriteLine("Number is {0}", i);
                i++;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 19:01

    Quite surprising that no one has mentioned yet the classical example for the do..while construct. Do..while is the way to go when you want to run some code, check or verify something (normally depending on what happened during the execution of that code), and if you don't like the result, start over again. This is exactly what you need when you want some user input that fits some constraints:

    bool CheckInput(string input) { ... }
    ...
    string input;
    ...
    do {
      input=Console.ReadLine();
    } while(!CheckInput(input));
    

    That's quite a generic form: when the condition is simple enough, it's common to place it directly on the loop construct (inside the brackets after the "while" keyword), rather than having a method to compute it.

    The key concepts in this usage are that you have to request the user input at least once (in the best case, the user will get it right at the first try); and that the condition doesn't really make much sense until the body has executed at least once. Each of these are good hints that do..while is the tool for the job, both of them together are almost a guarantee.

    0 讨论(0)
提交回复
热议问题