SEBA Class 10 Computer Science Chapter 5-Nested Loops in C Notes PDF


📘 Go to Exercise PDF

SEBA Class 10 Computer Science Chapter 5-Nested Loops in C (Complete Notes with PDF)

Learn Nested Loops, Pattern Programs, C Examples, Outputs and HSLC Exam Questions in Simple English

Welcome to the complete notes of SEBA Class 10 Computer Science Chapter 5 – Nested Loops in C. In this chapter, you will learn how to use one loop inside another loop to solve complex programming problems. Nested loops are widely used for pattern printing, matrix operations, multiplication tables, and many other programming tasks. These notes explain every topic in simple English with examples and programs to help you prepare for examinations.


What is a Nested Loop?

A nested loop is a loop placed inside another loop. The outer loop controls how many times the inner loop will execute. Every time the outer loop runs once, the inner loop completes all of its iterations.

Nested loops are mainly used when repeated operations must be performed inside another repeated operation.

Example

Suppose a teacher wants every student in a class to write the numbers from 1 to 5.

  • The outer loop represents each student.
  • The inner loop represents writing the numbers from 1 to 5.

For every student, the numbers are written again from the beginning. This is exactly how a nested loop works.


Structure of a Nested Loop

The basic structure of a nested loop is shown below.

Outer Loop
{
      Inner Loop
      {
            Statements
      }
}

The inner loop always finishes completely before the outer loop starts its next iteration.


Working of Nested Loops

Let us understand the working with a simple example.

for(i=1;i<=3;i++)
{
    for(j=1;j<=2;j++)
    {
        printf("*");
    }

    printf("\n");
}

Output

**
**
**

Explanation:

  • The outer loop executes 3 times.
  • During each iteration of the outer loop, the inner loop executes 2 times.
  • After completing the inner loop, the cursor moves to the next line.

Outer Loop

The outer loop is the first loop that controls the number of rows or repetitions. It decides how many times the inner loop will execute.

Characteristics of Outer Loop

  • Controls the total number of repetitions.
  • Usually represents rows in pattern programs.
  • Executes once before the inner loop starts.

Inner Loop

The inner loop is written inside the outer loop. It executes completely every time the outer loop runs once.

Characteristics of Inner Loop

  • Controls the number of columns.
  • Executes repeatedly for every outer loop iteration.
  • Usually prints symbols or numbers.

Flow of Execution

The execution of a nested loop follows these steps:

  1. The outer loop starts.
  2. The inner loop executes completely.
  3. The outer loop moves to the next iteration.
  4. The inner loop executes again.
  5. This continues until the outer loop finishes.

Syntax of Nested for Loop

for(initialization; condition; update)
{

     for(initialization;condition;update)
     {

          Statements;

     }

}

Nested While Loop

A while loop can also be placed inside another while loop. The inner while loop completes all its iterations before the outer while loop continues.

Syntax

while(condition)
{

      while(condition)
      {

            Statements;

      }

}

Nested Do-While Loop

Similarly, a do-while loop can be placed inside another do-while loop.

do
{

      do
      {

            Statements;

      }

      while(condition);

}

while(condition);

Important Points

  • A loop can be written inside another loop.
  • The inner loop always completes first.
  • Any type of loop can be nested inside another loop.
  • The outer and inner loops do not need to be the same type.
  • Nested loops are mainly used for pattern programs.

Advantages of Nested Loops

  • Reduce program length.
  • Make complex problems easier to solve.
  • Useful for pattern printing.
  • Useful for tables and matrices.
  • Improve programming efficiency.

Real-Life Applications

  • Printing star patterns.
  • Printing number patterns.
  • Creating multiplication tables.
  • Working with matrices.
  • Game development.
  • Calendar generation.
  • Chessboard design.

Exam Tip

Remember that the inner loop finishes completely before the outer loop moves to its next iteration. This is one of the most frequently asked concepts in HSLC examinations.


Coming in Part 2
  • Nested for loop programs
  • Star Pattern Programs
  • Number Pattern Programs
  • Step-by-step program explanation
  • Output of every program

5.2 Problem Solving Using Nested Loops

Nested loops are mainly used to solve problems where one repetitive task must be performed inside another repetitive task. They are especially useful for displaying different types of patterns using stars, numbers, or alphabets.

In the previous chapter, we used several individual loops to print patterns. Although the programs worked correctly, they became lengthy because we had to write a separate loop for each line. Nested loops solve this problem by reducing the number of loops and making the program shorter and easier to understand.


Example 5.1 – Printing an Inverted Star Pattern

Write a C program to display the following pattern using nested loops:

X X X X X
X X X X
X X X
X X
X

Observe the pattern carefully.

Line Number Number of X
1 5
2 4
3 3
4 2
5 1

We notice that in every new line, the number of X decreases by one. Therefore, the outer loop controls the line number, while the inner loop prints the required number of X characters. :contentReference[oaicite:1]{index=1}


Algorithm

  1. Start the program.
  2. Run the outer loop from 1 to 5.
  3. For every line, run the inner loop from 1 to (6 − k).
  4. Print X inside the inner loop.
  5. Move the cursor to the next line.
  6. Stop the program.

C Program

#include<stdio.h>

int main()
{
    int k,i;

    for(k=1;k<=5;k++)
    {

        for(i=1;i<=(6-k);i++)
        {

            printf("X ");

        }

        printf("\n");

    }

    return 0;

}

Output

X X X X X
X X X X
X X X
X X
X

Program Explanation

  • The outer loop controls the rows.
  • The inner loop prints the characters.
  • When k = 1, five X characters are printed.
  • When k = 2, four X characters are printed.
  • The process continues until only one X is printed.

Remember

The expression (6 − k) determines how many times the inner loop executes.


Example 5.2 – Dynamic Pattern Program

Instead of displaying only five rows, we can ask the user to enter the number of rows. The program then prints the pattern according to the user's input. This is called a dynamic pattern. :contentReference[oaicite:2]{index=2}


Example

If the user enters 7, the output becomes:

X X X X X X X
X X X X X X
X X X X X
X X X X
X X X
X X
X

C Program

#include<stdio.h>

int main()
{

    int N,k,i;

    printf("Enter number of lines : ");

    scanf("%d",&N);

    for(k=1;k<=N;k++)
    {

        for(i=1;i<=(N+1-k);i++)
        {

            printf("X ");

        }

        printf("\n");

    }

    return 0;

}

Program Explanation

  • The user enters the number of rows.
  • The outer loop runs from 1 to N.
  • The inner loop prints N+1−k characters.
  • The pattern automatically changes according to the user's input.

Why Dynamic Programs are Better?

Fixed Pattern Dynamic Pattern
Number of rows cannot be changed. User can enter any number of rows.
Less flexible. More flexible.
Suitable only for one pattern. Suitable for many different patterns.

Common Mistakes

  • Using the wrong loop variable.
  • Forgetting to print \n after each row.
  • Incorrect loop condition.
  • Writing the inner loop outside the outer loop.
  • Missing braces { }.

Exam Tip

Whenever you see a pattern question, first determine:

  1. How many rows are there?
  2. What does the outer loop control?
  3. How many characters are printed in each row?
  4. What formula controls the inner loop?

Coming in Part 3
  • Increasing star patterns
  • Pyramid patterns
  • Number patterns
  • Multiple nested loops
  • Advanced nested loop examples
  • Pattern logic explained step by step

More Pattern Programs Using Nested Loops

Nested loops can generate many types of patterns. By changing the number of rows, columns, spaces, or symbols printed by the inner loop, we can create different star, number, and alphabet patterns. Pattern programming improves logical thinking and helps students understand how nested loops work. :contentReference[oaicite:0]{index=0}


Example 5.3 – Increasing Star Pattern

Write a C program to display the following pattern:

X
X X
X X X
X X X X
X X X X X

Algorithm

  1. Start the program.
  2. Run the outer loop from 1 to 5.
  3. Run the inner loop from 1 to the current row number.
  4. Print X in each iteration.
  5. Move to the next line.

C Program

#include<stdio.h>

int main()
{

    int i,j;

    for(i=1;i<=5;i++)
    {

        for(j=1;j<=i;j++)
        {

            printf("X ");

        }

        printf("\n");

    }

    return 0;

}

Output

X
X X
X X X
X X X X
X X X X X

Explanation

  • The outer loop controls the rows.
  • The inner loop prints the number of X equal to the row number.
  • The number of X increases by one in every row.

Example 5.4 – Number Pattern

Nested loops can also print numbers instead of symbols.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

C Program

#include<stdio.h>

int main()
{

    int i,j;

    for(i=1;i<=5;i++)
    {

        for(j=1;j<=i;j++)
        {

            printf("%d ",j);

        }

        printf("\n");

    }

    return 0;

}

Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern Analysis

Outer Loop Inner Loop
Controls rows Controls columns
Runs once for every line Runs several times in one line
Changes row number Prints symbols or numbers

Can We Use Different Types of Loops?

Yes. The outer loop and the inner loop do not have to be the same type. A for loop may contain a while loop, a while loop may contain a do-while loop, or any other valid combination can be used. The choice depends on the problem being solved. :contentReference[oaicite:1]{index=1}


Example

for(i=1;i<=3;i++)
{

    j=1;

    while(j<=3)
    {

        printf("* ");

        j++;

    }

    printf("\n");

}

Output

* * *
* * *
* * *

Can We Use More Than Two Loops?

Yes. A program may contain three or more nested loops. However, more loops increase the complexity of the program and should be used only when necessary.


Example of Three Nested Loops

for(i=1;i<=2;i++)
{

    for(j=1;j<=2;j++)
    {

        for(k=1;k<=2;k++)
        {

            printf("* ");

        }

    }

    printf("\n");

}

Applications of Nested Loops

  • Printing star patterns
  • Printing number patterns
  • Creating multiplication tables
  • Working with matrices
  • Game programming
  • Calendar generation
  • Chessboard design
  • Graphics programming

Advantages of Nested Loops

  • Reduce program length.
  • Improve code readability.
  • Make pattern programming easier.
  • Useful for solving complex problems.
  • Increase programming efficiency.

Limitations of Nested Loops

  • Programs become difficult to understand if too many loops are used.
  • Execution time increases for large numbers of iterations.
  • Debugging becomes harder.
  • Incorrect loop conditions may produce infinite loops.

Remember

Whenever you solve a pattern problem, first identify the number of rows. Then determine how many times the inner loop should execute for each row. This is the key to solving all nested loop pattern questions.


HSLC Exam Tips
  • Know the difference between outer and inner loops.
  • Practice star and number patterns regularly.
  • Understand the logic before memorizing programs.
  • Trace each iteration using a table.
  • Write proper indentation while writing nested loops.

Coming in Part 4
  • Important Keywords
  • Chapter Summary
  • Frequently Asked Questions
  • Important Viva Questions
  • Exercise PDF Section
  • Related Posts Section
  • End of Chapter

Important Keywords

Keyword Meaning
Nested Loop A loop written inside another loop.
Outer Loop The first loop that controls the number of rows or repetitions.
Inner Loop The loop inside the outer loop that controls columns or repeated operations.
Iteration One complete execution of a loop.
Pattern Programming Printing symbols, numbers or characters using loops.
Dynamic Pattern A pattern whose size depends on user input.
Loop Variable A variable used to control loop execution.
printf() C function used to display output.

Quick Revision

  • Nested loop means one loop inside another loop.
  • The outer loop controls the rows.
  • The inner loop controls the columns.
  • The inner loop always finishes first.
  • Nested loops are mainly used for pattern programming.
  • Different types of loops can be nested together.
  • Three or more nested loops can also be used if required.
  • Dynamic patterns allow users to choose the number of rows.

Common Examination Questions

  1. What is a nested loop?
  2. Differentiate between the outer loop and inner loop.
  3. Can we use different types of loops together? Explain.
  4. Can we use three nested loops? Justify your answer.
  5. Write the syntax of a nested for loop.
  6. Write a C program to print an increasing star pattern.
  7. Write a C program to print a decreasing star pattern.
  8. Explain dynamic pattern programming.

Frequently Asked Questions (FAQ)

1. What is a nested loop?

A nested loop is a loop placed inside another loop.


2. Which loop executes first?

The outer loop starts first, but the inner loop completes all of its iterations before the outer loop moves to the next iteration.


3. Can we use different types of loops in nested loops?

Yes. A for, while, or do-while loop can be placed inside another type of loop.


4. Can we use more than two loops?

Yes. Three or more nested loops can be used whenever required.


5. Why are nested loops mainly used?

Nested loops are mainly used for pattern programming, matrix operations, tables, and repeated calculations.


6. What is a dynamic pattern?

A dynamic pattern is a pattern whose number of rows depends on the value entered by the user.


Chapter Summary

In this chapter, we learned the concept of nested loops in C programming. We studied the structure of nested loops, understood the roles of outer and inner loops, solved pattern programming problems, created dynamic patterns, and learned how nested loops reduce program length while improving code efficiency. These concepts form the foundation for solving advanced programming problems and developing logical thinking skills.


💡 Exam Tip

Before writing any pattern program, always identify:

  1. Number of rows.
  2. What the outer loop controls.
  3. What the inner loop prints.
  4. How many times the inner loop should execute.

📌 Remember

  • Nested loops reduce the number of repeated statements.
  • Always trace the loop using rows and columns.
  • Practice different pattern questions regularly.
  • Proper indentation makes programs easier to read.

📘 Chapter 5 Exercise PDF

Read the complete Chapter 5 exercise directly on the website.


End of Chapter 5 – Nested Loops



Conclusion

Nested loops are one of the most powerful concepts in C programming. They help programmers solve complex problems with simple logic. By understanding how the outer loop and inner loop work together, students can easily solve pattern-based questions, matrix problems, and many other programming tasks. Regular practice of nested loop programs will improve programming skills and prepare students for the HSLC examination.


🎉 Congratulations!
You have successfully completed SEBA Class 10 Computer Science Chapter 5 – Nested Loops in C.