I think the best way is to step through the code manually and write down the line number of the executing code and how the variables change.

Like

L01:    int k,i,j,s;                // k=?, i=?, j=?, s=?
L02:    scanf("%d", &k);            // k=18, i=?, j=?, s=?
L03:    for(i=1;                    // k=18, i=1, j=?, s=?
L03:             i<=k;              // TRUE
L04:        s=0;                    // k=18, i=1, j=?, s=0
L05:        for(j=1;                // k=18, i=1, j=1, s=0
L05:                 j<i;           // FALSE
L03:                      i++)      // k=18, i=2, j=1, s=0
L03:             i<=k;              // TRUE
L04:        s=0;                    // k=18, i=2, j=1, s=0
L05:        for(j=1;                // k=18, i=2, j=1, s=0
L05:                 j<i;           // TRUE
L06:            if(i%j==0)          // TRUE
L07:                s=s+j:          // k=18, i=2, j=1, s=1
L05:                      j++)      // k=18, i=2, j=2, s=0
L05:                  j<i;          // FALSE
and so on ....

I takes quite some time but you should soon see the pattern and there by understand how for-loops works.

Another thing that might help you understanding for-loops are to realize that

for(i=0; i<N; i++)
{
    code...
}

is equivalent to

i=0;
while (i<N)
{
    code...

     i++;
}

BTW:

Always check the return value from scanf - like:

if (scanf("%d", &k) != 1)
{
    printf("Input error! Program terminates.\n");
    exit(1);
}
Answer from 4386427 on Stack Overflow
🌐
W3Schools
w3schools.com › c › c_for_loop_nested.php
C Nested Loops
This is called a nested loop. The "inner loop" will be executed one time for each iteration of the "outer loop":
🌐
GeeksforGeeks
geeksforgeeks.org › c language › nested-loops-in-c-with-examples
Nested Loops in C - GeeksforGeeks
If the outer loop is running from ... the inner loop will run from j = 0 to 3. Nested for loop refers to any type of loop that is defined inside a 'for' loop....
Published   November 7, 2025
Discussions

Nested for loops in C language - Stack Overflow
An abundant number is a natural number that is less than the sum of its proper divisors. For example 12 1+2+4+8=15 is not an abundant nu... More on stackoverflow.com
🌐 stackoverflow.com
c++ - How nested for loops work? - Stack Overflow
I need help understanding how to nest for loops in C++ and how to understanding what is going on? Can someone explain to me what is going on with these lines of code: #include us... More on stackoverflow.com
🌐 stackoverflow.com
I can write nested loops and they work, but I feel like I'm missing the deeper intuition. I’m hoping to hear from others who’ve had that “aha!” moment with nested loops.
What a weird question, so articulate, almost ChatGPT like. Maybe you need to learn about for and while loops better before trying to address nested loops as if they are the next level of learning. They are used then THEY NEED to be used really. Nothing else. One use case would be lets say a two dimensional array, they can be looked as a grid. If you wanted to print each member of the array you'd need two loops, one that has one variable that targets a row and another loop inside that one that targets a column, printing the x,y value on the array. You can read about multi dimensional arrays on many webpages just by googling them. This one has information about it including your very dreaded "nested loop" as an example https://www.w3schools.com/c/c_arrays_multi.php More on reddit.com
🌐 r/C_Programming
12
0
August 1, 2025
Can someone please explain nested loops / loops in general, like I'm five? (C++)
Loops like you're 5: A loop is a way to tell the computer to do something specific repeatedly. For instance, if you were a (human) computer and I told you to walk to the end of the street and back until you have done so three times; this is a loop. It's a specific instruction done repeatedly. Loops don't always have an end. If I told you (as a computer) to walk to the end of the street and back until I tell you to stop; if suddenly I decide to move to France while you're walking back and forth, this is an example of a potential infinite loop. I may come back to visit and tell you to stop. You may keep going until you keel over. Nested loops like you're 5: If a loop is a set of specific instructions done repeatedly, what happens if you put a loop in a loop? Well, one of those loops has to finish entirely before the other can continue again. If I told you to walk down to the end of the street 3 times and back, but when you get halfway back, stop and touch your nose 5 times before you start again; this is a nested loop. You may say to yourself "but wait, I can touch my nose while I'm walking, why do I need to stop?" and you would be right, but what we need to realize about computers is that because of how they work, they can only do one thing at a time. (see below before any objections) Computers work in steps. Do A, then do B, then do C. Loops allow us to say "but do X over and over again." You may then say "but how come I can browse reddit, listen to music, and download stuff at the same time on my computer?" and this moves into the concepts of concurrency, or in simpler terms, making the computer do lots of different things at the same time. Things like this exist because we may want a loop to be indefinite, or to end when we tell it to end. Lots of things in the computer world depend on infinite loops, including browsing Reddit (for server side web services, some web browsing features) and video games (rendering and game logic loops). This is where concurrency and asynchronous operations come into play, but this is beginning to get out of the scope of the question, so I'll stop it here (unless you're interested). tl;dr More on reddit.com
🌐 r/learnprogramming
17
4
March 20, 2014
🌐
Reddit
reddit.com › r/learnprogramming › can someone please explain nested loops / loops in general, like i'm five? (c++)
r/learnprogramming on Reddit: Can someone please explain nested loops / loops in general, like I'm five? (C++)
March 20, 2014 -

I'm in a beginner class at my high school, and I'm trying to learn programming. I find loops sort of easy to understand, but we're supposed to create a multiplication table using a nested loop and I just can't get it. Help! Thank you!

Top answer
1 of 5
7
Loops like you're 5: A loop is a way to tell the computer to do something specific repeatedly. For instance, if you were a (human) computer and I told you to walk to the end of the street and back until you have done so three times; this is a loop. It's a specific instruction done repeatedly. Loops don't always have an end. If I told you (as a computer) to walk to the end of the street and back until I tell you to stop; if suddenly I decide to move to France while you're walking back and forth, this is an example of a potential infinite loop. I may come back to visit and tell you to stop. You may keep going until you keel over. Nested loops like you're 5: If a loop is a set of specific instructions done repeatedly, what happens if you put a loop in a loop? Well, one of those loops has to finish entirely before the other can continue again. If I told you to walk down to the end of the street 3 times and back, but when you get halfway back, stop and touch your nose 5 times before you start again; this is a nested loop. You may say to yourself "but wait, I can touch my nose while I'm walking, why do I need to stop?" and you would be right, but what we need to realize about computers is that because of how they work, they can only do one thing at a time. (see below before any objections) Computers work in steps. Do A, then do B, then do C. Loops allow us to say "but do X over and over again." You may then say "but how come I can browse reddit, listen to music, and download stuff at the same time on my computer?" and this moves into the concepts of concurrency, or in simpler terms, making the computer do lots of different things at the same time. Things like this exist because we may want a loop to be indefinite, or to end when we tell it to end. Lots of things in the computer world depend on infinite loops, including browsing Reddit (for server side web services, some web browsing features) and video games (rendering and game logic loops). This is where concurrency and asynchronous operations come into play, but this is beginning to get out of the scope of the question, so I'll stop it here (unless you're interested). tl;dr
2 of 5
2
A nested loop will perform the inner loop in its entirety for each iteration of the outer loop. So if you had an inner loop of 10 with an outer loop of 3, it's going to go 1-10, 3 times.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_nested_loops.htm
Nested Loops in C
That means you can put a while loop inside a for loop, a for loop inside a do-while loop, or any other combination. The general behaviour of nested loops is that, for each iteration of the outer loop, the inner loop completes all the iterations.
🌐
Medium
medium.com › @Dev_Frank › nested-loop-in-c-for-loop-8e095615ae7c
NESTED LOOP IN C (for loop). Mastering Nested Loops for Efficient C… | by Dev Frank | Medium
January 21, 2024 - NESTED LOOP IN C (for loop) In C programming, a nested loop is a loop inside another loop. This allows you to iterate over a set of elements in a more complex pattern. The inner loop runs multiple …
🌐
ScholarHat
scholarhat.com › home
Nested Loops in C - Types of Expressions in C ( With Examples )
Nested loops in C are a powerful ... repeatedly for each cycle of the outer loop when there is a nested loop, which is a loop inside another loop....
Published   July 31, 2025
Find elsewhere
🌐
CodeChef
codechef.com › blogs › loops-in-c
Loops in C - For, While, Nested Loops
August 6, 2024 - Learn how to use C loops effectively. This guide covers for loops, while loops, nested loops, and practical coding examples for beginners.
🌐
DataFlair
data-flair.training › blogs › nested-for-loop-in-c
Nested For Loop in C with Examples - DataFlair
January 3, 2024 - A nested for loop is a loop nested inside another for loop. The inner loop runs in its entirety during each iteration of the outer loop. This means the inner loop cycle controls the total number of times the inner loop body runs.
🌐
Unstop
unstop.com › home › blog › nested loop in c | all types, break & continue (+examples)
Nested Loop In C | All Types, Break & Continue (+Examples)
June 11, 2024 - A nested loop is when you have loops within loops, sort of like fitting a piece of code inside another. We will explore the three types of nested loops, i.e., for, while, and do-while, along with some best practices to consider when working ...
🌐
Scribd
scribd.com › document › 711093586 › 5-Nested-Loop
Nested Loops in C Programming Guide | PDF | Control Flow
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Algor Education
cards.algoreducation.com › en › content › rvJInF8M › nested-loops-c-programming
Nested Loops in C Programming | Algor Cards
Nested loops are a core concept ... structures. A nested loop is defined as a loop within another loop, where the inner loop is executed completely for each iteration of the outer loop....
🌐
Source Code Tester
sourcecodester.com › book › 7601 › nested-loops.html
Nested For LOOPS | SourceCodester
July 14, 2014 - So we declare them first and we also declare the loop counters. ... scanf("%d",&height); Now we are taking inputs from the user, The user will enter width and height of the rectangle. We will use these variables to pass values to the for loop.
🌐
Real Python
realpython.com › nested-loops-python
Nested Loops in Python – Real Python
February 22, 2025 - A while loop runs as long as a specified condition remains true, making it useful when the number of iterations isn’t known in advance. You create a nested loop by placing one loop inside another.
🌐
Codefinity
codefinity.com › courses › v2 › bdb08f7f-6657-40df-926d-6e2970a85b07 › 52f0ee02-2e08-41fc-91a1-038887f477c8 › 23bea561-0b70-4c90-b709-e2333844a1e0
Learn How to Work with Nested While Loops in C | Nested Loops
Nested loops allow you to perform more complex iteration patterns in your C programs. When you nest while loops, you place one while loop inside another. The outer loop controls the number of times the inner loop runs, and the inner loop completes ...
🌐
NxtWave
ccbp.in › blog › articles › nested-loop-in-c
How Nested Loop in C Work: A Complete Guide
April 24, 2025 - This C program demonstrates how nested for loops work. The outer loop runs with variable c from 1 to 4, and for each c, the inner loop runs with d from 1 to 4. During each inner loop cycle, it prints the values of c and d.
🌐
Programtopia
programtopia.net › home › c programming › nested loop in c
Nested loop in C - Programtopia
January 15, 2021 - Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m. ... Note: There can be mixed ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit4-Iteration › topic-4-4-nested-loops.html
4.4. Nested For Loops — CSAwesome v1
A nested loop has one loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns as shown below. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, ...
🌐
EMB Blogs
blog.emb.global › home › emerging-tech › nested loops in c programming: examples and use cases
Nested Loops in C Programming: Examples and Use Cases
July 8, 2024 - The outer loop runs through its sequence, and for each iteration, the inner loop completes its entire sequence. This process continues until both loops have executed their respective iterations. Understanding and utilizing nested loops effectively can lead to efficient and powerful code, especially when dealing with multi-dimensional data structures or complex algorithms.