Nested Loop in Java

Introduction to nested loops in Java

Nested loops in Java are loops that are placed inside other loops. This can be useful for performing complex repetitive tasks. For example, you could use nested loops to iterate over a two-dimensional array, or to print a pattern to the console. The syntax for nested loops is as follows:


  for (outer loop) {
    for (inner loop) {
      // code to execute
    }
  }
 

Nested loops can be a powerful tool for writing efficient and concise code, but it is important to use them carefully. Nested loops can make your code difficult to read and maintain if they are not used correctly.

Here are some tips for using nested loops effectively:
  • Use nested loops to perform complex repetitive tasks.
  • Use proper indentation and spacing to make your nested loops easier to read.
  • Add comments to your code to explain what your nested loops are doing.
  • Be careful not to create infinite loops.

Exercises

Write a Java Program to print the following pattern by getting the number of lines from the user
*
**
***
****
*****


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      //Reading input from the user 
          System.out.println("Enter the number of lines");
          Scanner input = new Scanner(System.in);
          int num_lines = input.nextInt();
          
          for(int i=1;i<=num_lines;i++)
          {
            for(int j=1;j<=i;j++)
            {
              System.out.print("*");
            }
            System.out.println();
          }	 
    }
  } 

Write a Java Program to print the following number pattern by getting the number of lines from the user
1
12
123
1234
12345


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
  public static void main(String args[]) 
  {
        //Reading the number of lines from the user
        System.out.println("Enter the number of lines");
        Scanner input = new Scanner(System.in);
        int num_lines = input.nextInt();
        for(int i =1;i<=num_lines;i++)
        {
          for(int j=1;j<=i;j++)
          {
            System.out.print(j);
          }
          System.out.println();
        }
  }
  } 

Write a Java Program to print the following inverted number pattern by getting the number of lines from the user
12345
1234
123
12
1


package morningsession;
import java.util.*;
public class first 
{
  public static void main(String[] args) 
  {
    //Reading the number of lines from the user
    System.out.println("Enter the number of lines");
    Scanner input = new Scanner(System.in);
    int num_lines = input.nextInt();
    for(int i=num_lines;i>=0;i--)
    {
      for(int j=1;j<=i;j++)
      {
        System.out.print(j);
      }
      System.out.println();
    }
  }
  
} 

Write a Java Program to print the following number pattern by getting the number of lines from the user
1
12
123
1234
12345
1234
123
12
1


package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[]) 
{
  //Reading the number of lines from the user
      System.out.println("Enter the number of lines");
      Scanner input = new Scanner(System.in);
      int num_lines = input.nextInt();
      for(int i =1;i<=num_lines;i++)
      {
        for(int j=1;j<=i;j++)
        {
          System.out.print(j);
        }
        System.out.println();
      }
      for(int i =num_lines-1;i>=0;i--)
      {
        for(int j=1;j<=i;j++)
        {
          System.out.print(j);
        }
        System.out.println();
      }
}
} 

Read five numbers from the user. If the user enters 13 then stop getting input and display the message that "you are lucky". If the user does not enter 13 then at the end display message "you are not lucky"


  import java.util.*;
  public class first 
  {
  public static void main(String[] args) 
  {
    
    Scanner input = new Scanner(System.in);
    int lucky_number=0;
    int lucky_flag=0;
      for(int i=0;i<5;i++)
      {
        System.out.println("Enter a number");
        lucky_number=input.nextInt();
        if(lucky_number==13)
        {
          System.out.println("you are lucky");
          lucky_flag=1;
          break;
        }
      }
      if(lucky_flag==0)
      {
        System.out.println("You are unlucky");
      }
  }
  }