Looping Issues

Started by
11 comments, last by ShadowValence 11 years, 7 months ago
Yeah thanks, I'm slowly getting the hang of it all but when I get stuck I really do get stuck. Like you said at least now I know what problems I can identify using the debugger.
Advertisement
Note that there are other ways to rewrite the loop that might make it more obvious what's going on. First, you can avoid the somewhat confusing SubString() call in favor of an string index.

for (int i = 0; i < strText.Length; i++) {
if (strText == 'g') {
LetterCount++;
}
}

Also C# supports foreach loops that look at each element of a collection without needing to manually extract the elements via an index or whatever.

foreach (char letter in strText) {
if (letter == 'g') {
LetterCount++;
}
}

Of course, there are a lot of other ways to write this, but many of them would only constitute showing off rather than efficient solutions.

... you can avoid the somewhat confusing SubString() call in favor of an string index.


I'm so glad you wrote this, SiCrane. I had considered adding it to my previous post but figured it'd be a bit too long. :D

This topic is closed to new replies.

Advertisement