♣ Whenever we want to execute a single statement or a block of statements multiple times based on some condition then we can go for loops
♣ In C#.Net we can implement loops in 4 ways
1. For Loop
2. While Loop
3. Do While Loop
4. For Each Loop
Syntax :
for(<initialization>; <condition>;<++ or –>)
{
Statement 1;
Statement 2;
}
♣ Write a Program to Print 1 to 10 numbers by using For loop
Note : Executing For Block once is called as one iteration, the no of iterations are deciding by condition
Syntax :
While (<condition>)
{
statement 1;
statement 2;
++ or --
}
♣ Write a Program to Print 1 to 10 Numbers by using While loop
Syntax :
Do
{
statement 1;
statement 2;
+ + or - -;
}
while (<condition>)
♣ Even though condition is false first time but still do while loop will execute one iteration
♣ Whenever we want to execute at least one iteration irrespective of condition then we have to implement Do-While.
♣ In FOR and While Loop pre condition checking but in do while loop post condition checking.
♣ Example to Print 1 to 10 Numbers by using Do-While
♣ In C#.Net we can implement loops in 4 ways
1. For Loop
2. While Loop
3. Do While Loop
4. For Each Loop
For Loop :
for(<initialization>; <condition>;<++ or –>)
{
Statement 1;
Statement 2;
}
♣ Write a Program to Print 1 to 10 numbers by using For loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ForLoop
{
class Program
{
static void Main(string[] args)
{
/*eexample of for loop*/
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ForLoop
{
class Program
{
static void Main(string[] args)
{
/*eexample of for loop*/
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Output :
Note : Executing For Block once is called as one iteration, the no of iterations are deciding by condition
While Loop :
While (<condition>)
{
statement 1;
statement 2;
++ or --
}
♣ Write a Program to Print 1 to 10 Numbers by using While loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WhileLoop
{
class Program
{
static void Main(string[] args)
{
/*example while loop print 12345678910*/
int i = 1;
while (i <= 10)
{
Console.Write(i);
i++;
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WhileLoop
{
class Program
{
static void Main(string[] args)
{
/*example while loop print 12345678910*/
int i = 1;
while (i <= 10)
{
Console.Write(i);
i++;
}
Console.ReadLine();
}
}
}
Output :
Do While :
Do
{
statement 1;
statement 2;
+ + or - -;
}
while (<condition>)
♣ Even though condition is false first time but still do while loop will execute one iteration
♣ Whenever we want to execute at least one iteration irrespective of condition then we have to implement Do-While.
♣ In FOR and While Loop pre condition checking but in do while loop post condition checking.
♣ Example to Print 1 to 10 Numbers by using Do-While
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoWhileLoop
{
class Program
{
static void Main(string[] args)
{
/* do while loop to print 1,2,3,4,5,6,7,8,9,10.*/
int i = 1;
do
{
if (i < 10)
Console.Write(i + ",");
else
Console.Write(i + ".");
i++;
} while (i <= 10);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoWhileLoop
{
class Program
{
static void Main(string[] args)
{
/* do while loop to print 1,2,3,4,5,6,7,8,9,10.*/
int i = 1;
do
{
if (i < 10)
Console.Write(i + ",");
else
Console.Write(i + ".");
i++;
} while (i <= 10);
Console.ReadLine();
}
}
}