for (int i = 0, n = strlen(my_str); i < n; i++)
{
char c = my_str[i];
}
Notice how they initialize two variables in the for loop declaration. They do this because it is more efficient than calling strlen every time around the loop. And they do a good job of explaining this.
But nevertheless, this still confuses new people, who are already overwhelmed with vanilla for-loop syntax.
Let's clear up the confusion by explaining that you could accomplish the same thing like this:
int n = strlen(my_str);
for (int i = 0; i < n; i++)
{
char c = my_str[i];
}
And then say, but as long as we're here, this is a good example of how you can do fancy things with for-loops. You can initialize multiple variables, you can do different things instead of i++, etc. Give some good examples of these.
need to clean this up a little bit. Maybe just add a README file linking to the .c file, or possibly move everything in .c file into the README and show code using Markdown
Issue by jesseilev Friday Nov 06, 2015 at 00:07 GMT Originally opened as https://github.com/Launch-Code/cs50x-live-2016/issues/8
CS50 iterates overs strings like this:
Notice how they initialize two variables in the
for loop
declaration. They do this because it is more efficient than callingstrlen
every time around the loop. And they do a good job of explaining this.But nevertheless, this still confuses new people, who are already overwhelmed with vanilla for-loop syntax.
Let's clear up the confusion by explaining that you could accomplish the same thing like this:
And then say, but as long as we're here, this is a good example of how you can do fancy things with for-loops. You can initialize multiple variables, you can do different things instead of
i++
, etc. Give some good examples of these.Put it in this README