Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Functional Programming in C Loops

I'm having trouble understanding loops. In the "While and Do While Loops Tutorial," what does the line "Int w = 0;" mean

Also have trouble understanding the statement:

printf("letter %d is %c \n", w, letters[w]);

2 Answers

Hi Ben,

The w is a counter used to access the array at each element. So, the letters array contains three letters, one at index zero, the next at 1, then the last at 2.

Setting w to zero means that accessing the array like letters[w] will give you the first element of the array.

The printf statement prints a formatted string. This means you can insert flags in your string (the things with a % sign) and then the code will insert values of variables into your string. %d lets you insert a number and %c lets you insert a char.

So,

printf("letter %d is %c \n", w, letters[w]);

inserts the number of the index w at point %d and the contents of letters[w] at point %c. Your output will be something like "Letter 1 is a", then "Letter 2 is b" as you carry on looping through the code. The \n inserts a new line so it doesn't all follow on the same one.

Does that make sense?

Steve.

Steve, thanks for the explanation. I understand most of the concept now, but what do you mean when you say "the w is a counter used to access the array at each element?" Also, from my understanding now, "w" is used only to show how many times the loop is run through, correct?

Hi Ben,

No problem; glad to help.

The w is being used for two things, yes. It is being used to terminate the loop after three iterations, yes. But it is also being used to access the array and to print out each element in that array one at a time. The part of the printf statement that says letters[w] is accessing the letters array one element at a time. As the loop lops, w is first 0, then 1, then 2. The loop is told to continue only when w is less than 3, so these are the three values of w we are dealing with.

At first pass, w is zero, the printf prints out (among other things) letters[w] which is the same as letters[0] which is the char 'a'. The next loop, because we increment w with ++w, w is now 1 so letters[w] is the same as letters[1] which is the next char, 'b'. And so on.

So, w is being used in two ways. One to control the termination of the loop (else we'd be here all day!) and secondly to access the array at each element, one-at-a-time.

I hope that's a little clearer!

Steve.

Yes, thanks for the explanation. Finally have a clear idea as to what for and while loops are.

Excellent. For loops are best used when you know how many times you want to iterate (or can easily calculate that). While loops and do while loops are best used when looping is conditional upon other stuff that changes - they can loop once, not at all, or forever.

Happy coding!

The i is an iterator, i starts out at an initial value of 0 and continues until it reaches the value count (condition becomes false).

for (int i = 0; i < 4; i++) {}

As is w in the video before the quiz regarding do ... while loops. Exactly the same principle; using a variable as an index counter.

Traditionally, the letters i, j and k are used for this. For some reason the video regarding while loops uses w. Exactly the same concept, however.

The question came from the tutorial video, not the linked quiz. ;-)

Steve.