How do we use continue statement in a while loop in C#?

CsharpProgrammingServer Side Programming

The continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

The continue statement in C# works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.

For the while loop, continue statement causes the program control passes to the conditional tests.

The following is the complete code to use continue statement in a while loop.

Example

 Live Demo

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 10;
         /* loop execution */
         while (a < 20) {
            if (a == 15) {
               /* skip the iteration */
               a = a + 1;
               continue;
            }
            Console.WriteLine("value of a: {0}", a);
            a++;
         }
         Console.ReadLine();
      }
   }
}

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
raja
Published on 19-Sep-2018 10:17:53
Advertisements