Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialObed Yeboah
224 PointsCan someone please please help me understand control flow for loops. I am quite confuse with the video explanation
I just need someone to break down the statements used to decimals for me to understand better. please
Obed Yeboah
224 Pointsthank you so much richard.
can you give me the simplest breakdown of this code. i want to compare it with what i have written down for it.
include <stdio.h>
int main() {
int many[] = {2, 4, 8};
int sum = 0;
for(int i=0; i < 3; i++) {
sum +=many[i];
printf("sum %d\n", sum);
}
return 0;
}
1 Answer
Richard Lu
20,185 PointsI just realized that this question was asked in the language c, haha. Alright, I'll break down that code for you.
int many[] = {2, 4, 8}; // So you have an array called many, and it consist of the following numbers: 2, 4, 8.
int sum = 0; // We have a variable named sum, and we're going to use it to keep track of the sum of the numbers in the array.
// This is where the loop starts.
// The loop says, I want to declare a counter called i and start the counter at the number 0. (int i=0)
// while the counter i is less than 3 (i < 3), increase the counter by 1 (i++) and
// execute the code inside the loop (inside '{' and '}').
for(int i=0; i < 3; i++) {
sum +=many[i]; // This line simply uses the variable we mentioned earlier to keep track of the sum
printf("sum %d\n", sum); //print out the sum with format specifiers (in this case %d for decimal)
}
return 0;
An example output would look like this:
2 - first loop (sum += many[0] -> sum += 2 -> sum = 0 + 2)
6 - second loop (sum += many[1] -> sum += 4 -> sum = 2 + 4)
14 - third loop (sum += many[2] -> sum += 8 -> sum = 6 + 8)
I hope I've helped! :)
Obed Yeboah
224 PointsThank you so much richard i have finally understood it. wow great explanation!!!!!!!
Richard Lu
20,185 PointsRichard Lu
20,185 Pointsok a break down of the control flows of a:
for loop
//This tells the program, I want to create a variable called i and assign it the value 0. While the variable i is less than 10, run the code inside the curly braces ({}) and add 1 to i (i++).
for in loop
var anArray = [5, 4, 3, 2, 1] for i in anArray { println("(i) ") // prints 5 4 3 2 1 }
//This tells the program, whatever is in "anArray", assign it to the temporary variable i. Each time it goes through the loop, it goes through the index values of the array.
while loop
//Same thing, but this time the structure of the loop is different. It assigns the variable i to the value 0, and it enters the loop if i is less than 10 (0 < 10 is the initial test case). Inside the while loop you can see that i is increased by 1 (i++) while also printing out variable i.
I hope I've helped out!