·  C# – Syntax – Conditional Statements – while

 

- Info:

  - This tutorial shows how to use while conditional statements.

 

- Example:

  - Create C# Console Application Test.cs.

 

    Test.cs

    using System;

   

    class Test {

   

      static void Main(string[] args) {

   

        //SIMPLE.   Output is "1 2 3 4 5".

        int i = 0;

        while (i <= 4) {

          i++;

          Console.Write(i);

        }

   

        //BREAK.    Output is "1 2" since we break out of for loop before condition i<=4 is reached.

        i = 0;

        while (i <= 4) {

          i++;

          if (i == 3) { break; }

          Console.Write(i);

        }

   

        //CONTINUE. Output is "1 2 4 5" since display of number 3 is skipped.

        i = 0;

        while (i <= 4) {

          i++;

          if (i == 3) { continue; }

          Console.Write(i);

        }

       

      }

   

    }