Nested control structures in C language

Introduction

Programming में simple conditions और loops से basic problems solve की जा सकती हैं, लेकिन real-world problems अक्सर ज्यादा complex होती हैं।

ऐसी situations में हमें multiple conditions check करनी पड़ती हैं या एक process के अंदर दूसरी process को repeat करना पड़ता है।

उदाहरण के लिए:

  • एक number positive है या नहीं, और अगर है तो even है या odd
  • pattern printing जहाँ rows और columns दोनों control करने होते हैं
  • prime number या matrix related problems

इन सभी problems को solve करने के लिए हमें एक control structure के अंदर दूसरा control structure use करना पड़ता है।

इसी concept को C language में Nested Control Structures कहा जाता है।

यह concept programming logic को मजबूत बनाता है और exam में पूछे जाने वाले कई output-based programs का आधार होता है।

यह complex problems को solve करने में बहुत useful होता है, खासकर patterns, matrices और decision-making programs में।

Definition

जब एक control structure (if, loop आदि) के अंदर दूसरा control structure use किया जाता है, तो उसे Nested Control Structure कहा जाता है।

Types of Nested Control Structures

  1. Nested if
  2. Nested loops
  3. Combination (if + loop)

Nested if

Example 1: Number Positive और Even Check करना

#include <stdio.h>
int main()
{
int num = 8;
if (num > 0)
{
if (num % 2 == 0)
{
printf("Positive Even Number");
}
} return 0;
}

Example 2: Largest of Two Numbers (Nested)

#include <stdio.h>int main()
{
int a = 10, b = 20;
if (a != b)
{
if (a > b)
{
printf("a is greater");
}
else
{
printf("b is greater");
}
} return 0;
}

Nested Loops

Example 1: Simple Pattern

#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
printf("* ");
}
printf("\n");
} return 0;
}

Output:

* * *  
* * *
* * *

Example 2: Number Pattern

#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
} return 0;
}

Output:

1  
1 2
1 2 3

Combination (Loop + if)

Example 1: Even Numbers Print (1 to 10)

#include <stdio.h>int main()
{
int i;
for (i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
}
} return 0;
}

Example 2: Prime Number Check

#include <stdio.h>int main()
{
int num = 7, i, flag = 1;
for (i = 2; i < num; i++)
{
if (num % i == 0)
{
flag = 0;
}
} if (flag == 1)
printf("Prime");
else
printf("Not Prime");
return 0;
}

Real Understanding

Nested control structures का use इन problems में होता है:

  • Pattern printing (stars, numbers)
  • Matrix operations
  • Prime number checking
  • Complex decision making

Exam Points

  • Nested means “one inside another”
  • if के अंदर if → Nested if
  • loop के अंदर loop → Nested loop
  • Pattern programs में nested loops बहुत important हैं
  • Output-based questions अक्सर nested loops से आते हैं

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top