LaunchCode-Education-Archived / cs50x-live

The generic template version of Launchcode's CS50x curriculum. Fork this to create a repo for a particular city.
Creative Commons Zero v1.0 Universal
2 stars 5 forks source link

[CLOSED] Resource: Module 2 / More for-loops #8

Closed jesseilev closed 8 years ago

jesseilev commented 8 years ago

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:

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.

Put it in this README

jesseilev commented 8 years ago

Comment by stlou Friday Dec 04, 2015 at 17:39 GMT


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