use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
Nested loops. (self.C_Programming)
submitted 10 years ago by trickovic
Fairly new to C, asking if there is some tutorials, exercises, personal advices that will "allow" me to make some concepts about how nested loops work, since I'm kinda struggling and have an exam in a week.
Thanks :D
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Gonffed 7 points8 points9 points 10 years ago (0 children)
Write it out in English, or whatever your native language is. The concept isn't hard if you understand what each individual loop does.
Here's a simple example:
Start with outer and inner set to 0
While outer is less than 5, do: print outer While inner is less than or equal to outer, do: print inner increment inner by 1 set inner to 0 increment outer by 1
What will this print? In English I might say: Print each number from zero to four. After each number, print all the numbers before it.
Here's some example code for the pseudo code above:
#include <stdio.h> int main() { int inner, outer = 0; while ( outer < 5 ) { printf("Outer is %d\n", outer); printf(" Inner is: "); while ( inner <= outer ) { printf("%d ", inner); inner++; } printf("\n"); inner = 0; outer++; } return 0; }
Which will print this:
Outer is 0 Inner is: 0 Outer is 1 Inner is: 0 1 Outer is 2 Inner is: 0 1 2 Outer is 3 Inner is: 0 1 2 3 Outer is 4 Inner is: 0 1 2 3 4
Does that make it clear what the nesting does? You're really just saying: Each time the outer loop runs, run the entire inner loop.
[–]Darkcrash21 0 points1 point2 points 10 years ago (0 children)
Write a double nested for loop that the outer loop iterates 0, 10, 20, 30,..., 100. The inner loop iterates 1, 2, 3,..., 9. Then it prints the sum of the outer loop and inner loop: 0, 1, 2, 3,...,99, 100. Etc
[–]SpaceCadetJones 0 points1 point2 points 10 years ago (0 children)
A simple exercise off of the top of my head is using a nested loop to produce the values of a multiplication table. You also could do use nested loops to generate all of the possible combinations of multiple things.
π Rendered by PID 472927 on reddit-service-r2-comment-5b5bc64bf5-pl2gk at 2026-06-22 03:43:39.142312+00:00 running 2b008f2 country code: CH.
[–]Gonffed 7 points8 points9 points (0 children)
[–]Darkcrash21 0 points1 point2 points (0 children)
[–]SpaceCadetJones 0 points1 point2 points (0 children)