Saturday, October 27, 2007

How to use loops statement in VB.NET & C# ?

Loops statement

In vb.net & c# language Loops statement are differ.Code Loops statement of vb.net & c# language.

____________________________________________________________________

VB.NET

Pre-test Loops:

While c <>End While

Do Until c = 10
c += 1
Loop

Do While c <>Loop

For c = 2 To 10 Step 2
Console.WriteLine(c)
Next


Post-test Loops:

Do
c += 1
Loop While c <>

Do
c += 1
Loop Until c = 10



' Array or collection looping
Dim names As String() = {"Fred", "Sue", "Barney"}
For Each s As String In names
Console.WriteLine(s)
Next

' Breaking out of loops
Dim i As Integer = 0
While (True)
If (i = 5) Then Exit While
i += 1
End While

' Continue to next iteration
For i = 0 To 4
If i <>Continue For
Console.WriteLine(i) ' Only prints 4
Next

-------------------------------------------------------------------------------------------------

C#

Pre-test Loops:

// no "until" keyword
while (c <>for (c = 2; c <= 10; c += 2) Console.WriteLine(c);



Post-test Loop:

do
c++;
while (c < class="comment">// Array or collection looping
string[] names = {"Fred", "Sue", "Barney"};
foreach (string s in names)
Console.WriteLine(s);

// Breaking out of loops
int i = 0;
while (true) {
if (i == 5)
break;
i++;
}

// Continue to next iteration
for (i = 0; i <>continue;
Console.WriteLine(i); // Only prints 4
}


-------------------------------------------------------------------------------------------------

Note: This code applicable for both desktop and web application.

-------------------------------------------------------------------------------------------------

No comments: