C# Do While Loop Example

C# Do While Loop Example

C# Do While Loop If you want to execute certain task at least once, you have choice to use C# do while loop and for loop. Here we will see trick in VB.net and C#.net Do-while loop examples and syntax.

In do-while loop, condition is checked after execution of task. This ensures that the task is performed at least once.

Do while loop example [c#]
using System;
namespace Csharp.Do.While.Loop.Example
  class Program
    static void Main(string[] args)

    int index = 0;
    int total = 0;
    int []arrIntegers =  new int[] {100,150,150,130,500};
        total = total + arrIntegers[index];
      while(total <= 50);
Output of the code is 100. VB.net do-while loop example
Imports System
Namespace Csharp.Do.While.Loop.Example
	Class Program
		Private Shared Sub Main(args As String())

		Dim index As Integer = 0
		Dim total As Integer = 0
		Dim arrIntegers As Integer() = New Integer() {100, 150, 150, 130, 500}

			total = total + arrIntegers(index)
 			index += 1
 		Loop While total <= 50


		End Sub
	End Class
End Namespace

Description You can see the construction of the program above, without checking the length of an array we are iterating the array elements. After execution of the first loop we are checking the condition. This means in any way the total would be 100.

Do while loop exit when while condition gets failed. In above example when total reaches greater than 50.

do while loop is used in almost all programming languages.

Syntax of Do While loop in C#

Before executing the condition, you are performing the task at least once.

	  // do something.
	while( exit condition );


C# Do While Loop ExampleC# foreach loopC# For LoopC# looping through arrayJavaScript For LoopC# MemoryStream ExampleC# XML XmlReaderC# Recursion ExampleC# HtmlTextWriter ExampleDateTimeTryParse using C# and VB.net exampleOne dimensional array example C#.net VB.net


My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.

I'm Satalaj.