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]
#2
Well done!

I'm currently researching more into C# and noticed there is one more loop style - the For-Each loop. Found in the System.Collections namespace, it is used to iterate through items in a list (array). This is a great implementation which allows easy processing or listing like attributes in said array.

Counter and algorithms can be set in place if certain elements need to be ignored! One can also break from the loop at any time.

Off the MSDN:

Code:
    class ForEachTest
    {
        static void Main(string[] args)
        {
            int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
            foreach (int i in fibarray)
            {
                System.Console.WriteLine(i);
            }
        }
    }
    /*
    Output:
    0
    1
    1
    2
    3
    5
    8
    13
    */
#3
Well done!
I'm going to post something about Arrays soon.
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]
#4
here's a more proper write up about the different loops.




For loop:
the for loop is used to execute code so long as the iteration condition is true, i.e. i < 10


infinite for loop has no condition.:
Code:
for(;;) { }


the structure of a for loop
Code:
for(counter initialization; condition; counter change) { code }


you can have multiple variables aswel
Code:
for(int i = 0, int k = 100; i <= 50 && k >= 50; i++, k--) { }




while loop:
a while loop doesn't have a counter variable, it executes so long as the condition is true.
Code:
while(condition) { code }
a while loop executes the condition first and tests then executes code.


do-while loop:
this is the same as a while loop except code execution happens first, then condition execution.
Code:
do {
code will always execute atleast once
} while (condition)
other than that the do-while and while are the same


for-each is used for iterating through an iteratable collection.
Code:
foreach(holderVariable in iterableObject) { code }
as it iterates through the collection the holder variable is filled with whatever is in the current location in the collection and can be used as a direct reference to the object (not a copy) so changing the holder changes the object in the collection.






the important thing to remember when using loops is to always have a way out, infinite loops are not your friend. in a for loop make sure your condition will eventually return false. in your while/do-while loop make sure your condition can either return false or your code has a breakout somewhere.
#5
Great work!
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Users browsing this thread: 1 Guest(s)