do while loop treats same as while loop but only differences between
them is that, do while executes at least one time. The code executes
first then check for specified loop condition. But in while loop, the
specified loop condition evaluated first then executes the code. A
simple demonstration will help you to figure out do while loop more
clearly.
Example:
Note: You must put semi-colon(;) at the end of while condition in do…while loop.
Output
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 92
12 x 9 = 108
12 x 10 = 120
Example:
using System;
namespace do_while
{
class Program
{
static void Main(string[] args)
{
int table,i,res;
table=12;
i=1;
do
{
res = table * i;
Console.WriteLine("{0} x {1} = {2}", table, i,res);
i++;
}
// must put semi-colon(;) at the end of while condition in do...while loop.
while (i <= 10);
Console.ReadLine();
}
}
}
{
class Program
{
static void Main(string[] args)
{
int table,i,res;
table=12;
i=1;
do
{
res = table * i;
Console.WriteLine("{0} x {1} = {2}", table, i,res);
i++;
}
// must put semi-colon(;) at the end of while condition in do...while loop.
while (i <= 10);
Console.ReadLine();
}
}
}
Note: You must put semi-colon(;) at the end of while condition in do…while loop.
Output
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 92
12 x 9 = 108
12 x 10 = 120
Comments
Post a Comment