Nov 29 2011, 01:10 PM
Now to Repeat a single line of Code, ore a whole block there are 3 types of Loops:
1st: For 2nd: While 3rd: Do_While
[glow=green,2,300]Basic Code Sample on (FOR):[/glow]
-How to combine 10 Numbers with only 7 lines of Code-
*Console App*
[glow=green,2,300]Basic Code Sample on (While):[/glow]
-I'll now write the same previous app but using "While"-
*Console App*
[glow=green,2,300]Basic Code Sample on (Do_While):[/glow]
-I'll now write the same previous app but using "Do_While"-
*Console App*
NOTE: All do the same Function which is to Repeat.
1st: For 2nd: While 3rd: Do_While
[glow=green,2,300]Basic Code Sample on (FOR):[/glow]
-How to combine 10 Numbers with only 7 lines of Code-
*Console App*
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DOSplus1
{
class Program
{
static void Main(string[] args)
{
int sum = 0;
for (int k = 1; k <= 10; k++)
{
int x = Int32.Parse(Console.ReadLine());
sum += x;
}
Console.WriteLine("The Sum = {0}", sum);
}
}
}
[glow=green,2,300]Basic Code Sample on (While):[/glow]
-I'll now write the same previous app but using "While"-
*Console App*
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DOSplus2
{
class Program
{
static void Main(string[] args)
{
int k = 1;
int sum = 0;
while (k<=10)
{
int x = Int32.Parse(Console.ReadLine());
sum += x;
k++;
}
Console.WriteLine("The Sum = {0}", sum);
}
}
}
[glow=green,2,300]Basic Code Sample on (Do_While):[/glow]
-I'll now write the same previous app but using "Do_While"-
*Console App*
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DOSplus3
{
class Program
{
static void Main(string[] args)
{
int k = 1;
int sum = 0;
do
{
int x = Int32.Parse(Console.ReadLine());
sum += x;
k++;
}
while (k<=10);
Console.WriteLine("The Sum = {0}", sum);
}
}
}
NOTE: All do the same Function which is to Repeat.