Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#1
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*
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.
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Messages In This Thread
-C#- Loops - by MindHACKer - Nov 29 2011, 01:10 PM
RE: -C#- Loops - by Helices - Nov 29 2011, 06:07 PM
RE: -C#- Loops - by MindHACKer - Nov 29 2011, 06:10 PM
RE: -C#- Loops - by Leaky - Nov 30 2011, 05:17 PM
RE: -C#- Loops - by MindHACKer - Nov 30 2011, 05:26 PM

Users browsing this thread: 1 Guest(s)