C
C#5mo ago
mxshiiツ

✅ help with basic c#

I am following the freeCodeCamp course for c# anyways, i dont understand how to differentiate between loops for, foreach, while, do-while can someone help?
13 Replies
canton7
canton75mo ago
"Differentiate" in what sense? They use different keywords, and they do (slightly) different things
Pobiega
Pobiega5mo ago
What are you actually asking for here? "can someone help" is very abstract. If you have questions, we can answer, but you'll need to ask the actual question 🙂
mxshiiツ
mxshiiツOP5mo ago
the thing is i dont know when to use for, foreach, while, do-while i am new here bare with me
canton7
canton75mo ago
while and do-while are pretty similar (and do-while is rare). That's when you want to loop as long as some boolean is true. That's the simplest sort of loop for loops simplify a common pattern, which is:
int counter = 0;
while (counter < limit)
{
// Do something with 'counter'
counter++;
}
int counter = 0;
while (counter < limit)
{
// Do something with 'counter'
counter++;
}
They just save some typing, and make it clearer that you're following this pattern, by putting those 3 parts onto one line:
for (int counter = 0; counter < limit; counter++)
{
// Do something with 'counter'
}
for (int counter = 0; counter < limit; counter++)
{
// Do something with 'counter'
}
mxshiiツ
mxshiiツOP5mo ago
i see but what are they used for like tell me an example when to use these bec tbh i am lost xd also while statement act as a "gate" if counter is < limit we will enter the while statement and iterate the code block till it evaluates to false
canton7
canton75mo ago
foreach is a slightly different beast. In C# (as with many languages), we have the concept of a collection which can be iterated over, but can't necessarily be accessed by index. This is described by the IEnumerable<T> interface. This has a single method, GetEnumerator(), which gives you an "enumerator" (implementing the IEnumerator<T> interface). An enumerator effectively points to an element in the collection, and there's a Current property which lets you access the current element, and it lets you move to the next element in the collection with the MoveNext method. So let's say you're using Enumerable.Range(0, 10), which returns a collection which gives you the numbers 0-9 one-by-one, without making an array to hold all of those numbers in memory at the same time. You could do:
IEnumerable<int> range = Enumerable.Range(0, 10);

IEnumerator<int> enumerator = range.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Current);
}
IEnumerable<int> range = Enumerable.Range(0, 10);

IEnumerator<int> enumerator = range.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Current);
}
This pattern is common, so we have the foreach loop to remove the boilerplate. This line does the same thing as the above:
IEnumerable<int> range = Enumerable.Range(0, 10);

foreach (int element in range)
{
Console.WriteLine(element);
}
IEnumerable<int> range = Enumerable.Range(0, 10);

foreach (int element in range)
{
Console.WriteLine(element);
}
Now, most collection types implement the IEnumerable<T> interface, even if they can be accessed by index. So you can use a foreach loop on things like lists and arrays, too. This means that the following 3 bits of code all effectively do the same thing:
int[] array = new int[] { 1, 2, 3 };

// 1
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}

// 2
IEnumerator<int> enumerator = array.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumertor.Current);
}

// 3
foreach (int element in array)
{
Console.WriteLine(element);
}
int[] array = new int[] { 1, 2, 3 };

// 1
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}

// 2
IEnumerator<int> enumerator = array.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumertor.Current);
}

// 3
foreach (int element in array)
{
Console.WriteLine(element);
}
mxshiiツ
mxshiiツOP5mo ago
like
Random random = new Random();
int current = random.Next(1, 11);

while (current >= 3)
{
Console.WriteLine(current);
current = random.Next(1, 11);
}
Console.WriteLine($"Last number: {current}");
Random random = new Random();
int current = random.Next(1, 11);

while (current >= 3)
{
Console.WriteLine(current);
current = random.Next(1, 11);
}
Console.WriteLine($"Last number: {current}");
so if the int current = 3 or bigger than 3 we will enter the while loop so while acts as a "gate" more like if statement but with loop
canton7
canton75mo ago
Yes, you can think of while that way. An if statement is more like a "gate", because you normally only enter a gate once, while a while loop keeps looping while the condition is true
mxshiiツ
mxshiiツOP5mo ago
yea also i still didnt reach the IEnumerator so i dont understand this msg
canton7
canton75mo ago
I probably mis-judged your level a bit, my bad. For now, think of a foreach loop as a way to loop over the values of a collection, as opposed to a for loop which loops over the indexes of a collection. So:
for (int index = 0; index < array.Length; index++)
{
int element = array[index];
Console.WriteLine(element);
}
for (int index = 0; index < array.Length; index++)
{
int element = array[index];
Console.WriteLine(element);
}
vs
foreach (int element in array)
{
Console.WriteLine(element);
}
foreach (int element in array)
{
Console.WriteLine(element);
}
mxshiiツ
mxshiiツOP5mo ago
ok ok got it thx @canton7 <3
canton7
canton75mo ago
👍 $close
MODiX
MODiX5mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server