while loop
While loop is probably the easiest, so we'll start with it. Unless the loop executes a block of code, then until this situation is giving you that is true.
Example -
Example -
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 0;
while(number < 5)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();
}
}
}
do loop
It is similar to a while statement, except that it tests the condition at the end of the loop body
Example -
Example -
do
{
Console.WriteLine(number);
number = number + 1;
} while(number < 5);
for loop
It executes the sequence of statements many times and conducts the code that manages the loop variable.
Example -
Example -
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int number = 5;
for(int i = 0; i < number; i++)
Console.WriteLine(i);
Console.ReadLine();
}
}
}
foreach loop
The last loop, which we will see, is the foreach loop. It operates on the collection of objects, for example arrays or other underlying list types. In our example, we will use one of the simple lists, which is called an ArrayList. It works a lot like an array, but do not worry, we will look in the later chapters.
Example -
Example -
using System;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add("John Doe");
list.Add("Jane Doe");
list.Add("Someone Else");
foreach(string name in list)
Console.WriteLine(name);
Console.ReadLine();
}
}
}
0 comments:
Post a Comment