Print the pattern by using one loop | Set 2 (Using Continue Statement)

Given a number n, print triangular pattern. We are allowed to use only one loop.

Example:

Input: 7
Output:
*
* * 
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

We use single for-loop and in the loop we maintain two variables for line count and current star count. If current star count is less than current line count, we print a star and continue. Else we print a new line and increment line count.

C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print a pattern using single
// loop and continue statement
#include<bits/stdc++.h>
using namespace std;
  
// printPattern function to print pattern
void printPattern(int n)
{
    // Variable initialization
    int line_no = 1; // Line count
  
    // Loop to print desired pattern
    int curr_star = 0;
    for (int line_no = 1; line_no <= n; )
    {
        // If current star count is less than
        // current line number
        if (curr_star < line_no)
        {
           cout << "* ";
           curr_star++;
           continue;
        }
  
        // Else time to print a new line
        if (curr_star == line_no)
        {
           cout << "\n";
           line_no++;
           curr_star = 0;
        }
    }
}
  
// Driver code
int main()
{
    printPattern(7);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print a pattern using single
// loop and continue statement
import java.io.*;
  
class GFG {
  
    // printPattern function to print pattern
    static void printPattern(int n)
    {
        // Variable initialization
        // Line count
        int line_no = 1
      
        // Loop to print desired pattern
        int curr_star = 0;
        for ( line_no = 1; line_no <= n;)
        {
            // If current star count is less than
            // current line number
            if (curr_star < line_no)
            {
                System.out.print ( "* ");
                curr_star++;
                continue;
            }
      
            // Else time to print a new line
            if (curr_star == line_no)
            {
                System.out.println ("");
                line_no++;
                curr_star = 0;
            }
        }
    }
      
    // Driver code
    public static void main (String[] args)
    {
            printPattern(7);
    }
}
  
// This code is contributed by vt_m

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to print 
# a pattern using single
# loop and continue statement
  
# printPattern function
# to print pattern
def printPattern(n):
  
    # Variable initialization
    line_no = 1 # Line count
  
    # Loop to print 
    # desired pattern
    curr_star = 0
    line_no = 1
    while(line_no <= n ):
      
        # If current star count 
        # is less than current 
        # line number
        if (curr_star < line_no):
            print("* ", end = "")
            curr_star += 1
            continue
          
        # Else time to print
        # a new line
        if (curr_star == line_no):
            print("")
            line_no += 1
            curr_star = 0
          
# Driver code
printPattern(7)
  
# This code is contributed
# by Smitha

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a pattern using single
// loop and continue statement
using System;
  
class GFG {
  
    // printPattern function to print pattern
    static void printPattern(int n)
    {
        // Variable initialization
        // Line count
        int line_no = 1; 
      
        // Loop to print desired pattern
        int curr_star = 0;
        for ( line_no = 1; line_no <= n;)
        {
            // If current star count is less than
            // current line number
            if (curr_star < line_no)
            {
                Console.Write ( "* ");
                curr_star++;
                continue;
            }
      
            // Else time to print a new line
            if (curr_star == line_no)
            {
                Console.WriteLine ();
                line_no++;
                curr_star = 0;
            }
        }
    }
      
    // Driver code
    public static void Main ()
    {
            printPattern(7);
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to print a 
// pattern using single loop
// and continue statement
  
// printPattern function 
// to print pattern
function printPattern($n)
{
      
    // Variable initialization
    $line_no = 1; // Line count
  
    // Loop to print desired pattern
    $curr_star = 0;
    for ($line_no = 1; $line_no <= $n😉
    {
          
        // If current star count is less 
        // than current line number
        if ($curr_star < $line_no)
        {
            echo "* ";
            $curr_star++;
            continue;
        }
  
        // Else time to print
        // a new line
        if ($curr_star == $line_no)
        {
            echo "\n";
            $line_no++;
            $curr_star = 0;
        }
    }
}
  
    // Driver code
    $n=7;
    printPattern($n);
  
// This code is contributed by mits 
?>

chevron_right



Output:

*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *

Please refer below post for one more approach.
Print pattern using only one loop

This article is contributed by Ashish Varshney. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up