C Program for Print the pattern by using one loop

In C programming, printing patterns using a single loop is an interesting challenge that demonstrates the creative use of loop control statements like continue. This approach combines the functionality of nested loops into one loop by manipulating loop variables and using conditional statements.

Syntax

for (initialization; condition; ) {
    // Logic with continue statement
    // Manual increment of loop variable
}

Algorithm

START
Step 1 ? Declare variables i and j to 0 with number of rows n = 6
Step 2 ? Loop For i=1 and i<=n
   IF j<i
      Print *
      Increment j by 1
      Continue
   End IF
   IF j==i
      Print newline
      Set j=0
      Increment i by 1
   End IF
Step 3 ? End For Loop
STOP

Example

Here's how to print a star pattern using only one loop with the continue statement −

#include <stdio.h>

int main() {
    int i, j = 0;
    int n = 6;
    
    for (i = 1; i <= n; ) {
        if (j < i) {
            printf("*");
            j++;
            continue;
        }
        if (j == i) {
            printf("<br>");
            j = 0;
            i++;
        }
    }
    return 0;
}
*
**
***
****
*****
******

How It Works

  • The outer loop variable i represents the current row number
  • The inner counter j tracks how many stars have been printed in the current row
  • When j < i, we print a star and use continue to skip the rest of the loop body
  • When j == i, we've printed enough stars for the current row, so we print a newline and move to the next row

Conclusion

Using a single loop with the continue statement effectively simulates nested loops for pattern printing. This technique demonstrates advanced loop control and is useful for optimizing code structure in specific scenarios.

Updated on: 2026-03-15T11:24:59+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements