- do
- while
- for
- foreach, in
1.do
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int n = 0;
do
{
Console.WriteLine(n);
n++;
} while (n < 5);
}
}
}
// The example displays the following output:
// 0
// 1
// 2
// 3
// 4
2.while
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
int n = 0;
while (n < 5)
{
Console.WriteLine(n);
n++;
}
}
}
}
// The example displays the following output:
// 0
// 1
// 2
// 3
// 4
3.for
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
}
}
}
// The example displays the following output:
// 0
// 1
// 2
// 3
// 4
4.foreach, in
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var fibNumbers = new List<int> { 0, 1, 1, 2, 3, 5, 8, 13 };
int count = 0;
foreach (int element in fibNumbers)
{
count++;
Console.WriteLine("Element #{0}: {1}", count, element);
}
Console.WriteLine("Number of elements: {0}", count);
}
}
}
// The example displays the following output:
// Element #1: 0
// Element #2: 1
// Element #3: 1
// Element #4: 2
// Element #5: 3
// Element #6: 5
// Element #7: 8
// Element #8: 13
// Number of elements: 8