✅ 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
"Differentiate" in what sense? They use different keywords, and they do (slightly) different things
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 🙂
the thing is i dont know when to use
for, foreach, while, do-while
i am new here bare with me
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:
They just save some typing, and make it clearer that you're following this pattern, by putting those 3 parts onto one line:
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
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:
This pattern is common, so we have the foreach
loop to remove the boilerplate. This line does the same thing as the above:
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:
like
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
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 trueyea
also i still didnt reach the IEnumerator
so i dont understand this msg
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:
vs
ok ok got it
thx @canton7
<3
👍
$close
If you have no further questions, please use /close to mark the forum thread as answered