Basics of Looping Statements in c#
This is a sample code of how to use looping statements in .Net, using C#, its almost the same in VB.NET
Software used: Microsoft Visual Studio 2010
Software used: Microsoft Visual Studio 2010
Looping Statements Index
--------------------------------------
1) while statement
2) do statement
3) for statement
4) foreach statement
1) While statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
int count = 0;
while (count <= 10)
{
Console.WriteLine("Current number: " + count.ToString());
count++;
}
Console.ReadKey();
}
}
}
2) Do statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
string name = string.Empty;
do
{
name = Console.ReadLine();
Console.WriteLine("Name = " + name);
}
while(name.Trim() != "exit");
Console.ReadKey();
}
}
}
3) For statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine("Current i = " + i.ToString());
}
Console.ReadKey();
}
}
}
4) Foreach statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
string[] names = { "John", "Nick", "Pedro" };
foreach (string name in names)
{
Console.WriteLine(name);
}
// Its similar when we are using list instead of an array
List<string> namesList = new List<string>();
namesList.Add("John");
namesList.Add("Nick");
namesList.Add("Pedro");
foreach (string name in namesList)
{
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}
--------------------------------------
1) while statement
2) do statement
3) for statement
4) foreach statement
1) While statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
int count = 0;
while (count <= 10)
{
Console.WriteLine("Current number: " + count.ToString());
count++;
}
Console.ReadKey();
}
}
}
2) Do statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
string name = string.Empty;
do
{
name = Console.ReadLine();
Console.WriteLine("Name = " + name);
}
while(name.Trim() != "exit");
Console.ReadKey();
}
}
}
3) For statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine("Current i = " + i.ToString());
}
Console.ReadKey();
}
}
}
4) Foreach statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LoopingStatements
{
class Program
{
static void Main(string[] args)
{
string[] names = { "John", "Nick", "Pedro" };
foreach (string name in names)
{
Console.WriteLine(name);
}
// Its similar when we are using list instead of an array
List<string> namesList = new List<string>();
namesList.Add("John");
namesList.Add("Nick");
namesList.Add("Pedro");
foreach (string name in namesList)
{
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}