Loops in Java - for, while and do-while

Introduction to for,while and do while loops in Java

for loop

The for loop in Java is a control flow statement that allows you to execute a block of code repeatedly until a condition is met. For loops are often used to iterate over arrays or other collections of data. The syntax for the for loop is as follows:


  for (initialization; condition; increment) {
    // code to execute
  }
 
while loop

The while loop in Java is a control flow statement that allows you to execute a block of code repeatedly until a condition is met. While loops are often used to iterate over arrays or other collections of data, but they can also be used to perform other tasks, such as waiting for user input or polling a sensor. The syntax for the while loop is as follows:


  while (condition) {
    // code to execute
  } 
do while loop

The do-while loop in Java is a control flow statement that allows you to execute a block of code at least once, and then repeatedly until a condition is met. Do-while loops are often used to iterate over arrays or other collections of data, but they can also be used to perform other tasks, such as waiting for user input or polling a sensor. The syntax for the do-while loop is as follows:


  do {
    // code to execute
  } while (condition);
  
Here are some tips for using loops effectively:
  • Use loops to iterate over arrays, collections, or ranges of numbers.
  • Use proper indentation and spacing to make your loops easier to read.
  • Add comments to your code to explain what your loops are doing.
  • Be careful not to create infinite loops.

Exercises

Using for loop print "hello" three times


package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
  public static void main(String args[]) 
  {
    for(int i=0;i < 3;i++)
    {
      System.out.println("hello");
    }
  }
}

Program Output
hello
hello
hello 

Print "hello" three times using a while loop


  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      int i=0;
      while(i<3)
      {
        System.out.println("hello");
        i++;
      }
    }
  }
  
  Program Output
  hello
  hello
  hello 

Print "hello" three times using a do while loop


  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      int i=0;
      do
      {
        System.out.println("hello");
        i++;
      }while(i<3);
    }
  } 

Print the Sum of first N Natural Numbers. Get the value of N from the user


  package javaprogrammingdemo;
  import java.util.Scanner;
  public class javalabclass{
    public static void main(String args[]) 
    {
      System.out.println("Enter the value of N");
    Scanner input= new Scanner(System.in);
    int n=input.nextInt();
    int sum=0;
    for(int i=1;i<=n;i++)
    {
      sum+=i;
    }
    System.out.println("The sume of N Natural Numbers is " +sum);
    }
  }
  
  Program Output
  Enter the value of N
  3
  The sume of N Natural Numbers is 6 

Write a Java program to print all natural numbers in reverse (from n to 1)


  package javalabdemo;
  import java.util.Scanner;
  
  public class javalabclass {
  
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    //print the natural numbers in the reverse
    System.out.println("Enter the number");
    Scanner input = new Scanner(System.in);
    int N = input.nextInt();
    for(int i=N;i>=1;i--)
    {
      System.out.println(i);
    }	 
  }
  } 

Write a Java program to print multiplication table of any number.


  package javalabdemo;
  import java.util.Scanner;
  
  public class javalabclass {
  
    public static void main(String[] args) {
      // TODO Auto-generated method stub
      //Print the multiplication table of any number 
      System.out.println("Enter the number");
      Scanner input = new Scanner(System.in);
      int N = input.nextInt();
      for(int i=1;i<=10;i++)
      {
        System.out.println(N+" x "+i+" = "+ N*i);
      }	 
    }
  } 

Write a Java program to find first and last digit of a number


  package javalabdemo;
  import java.util.Scanner;
  
  public class javalabclass {
  
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Determine the first and last digit of a number 
    System.out.println("Enter the number");
    Scanner input = new Scanner(System.in);
    int N = input.nextInt();
    int last_digit = N%10;
    int first_digit=0;
    while(N!=0)
    {
      first_digit=N;
      N=N/10;
      
    }
    System.out.println(first_digit + " and " +last_digit);
  }
  } 

Write a Java program to find sum of digits of a number


  package javalabdemo;
  import java.util.Scanner;
  public class javalabclass {
  
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Determine the first and last digit of a number 
    System.out.println("Enter the number");
    Scanner input = new Scanner(System.in);
    int N = input.nextInt();
    int last_digit=0,sum=0;
    while(N!=0)
    {
      last_digit=N%10;
      sum+=last_digit;
      N=N/10;
      
    }
    System.out.println("The sum of all digits is "+ sum);
    
  }
  }  

Provide the user the choice of adding two numbers until the user wants to exit


  package javalabdemo;
  import java.util.Scanner;
  public class javalabclass {
  
    public static void main(String[] args) {
      
      boolean choice=true;
      do
      {
        System.out.println("Enter the numbers");
        Scanner input = new Scanner(System.in);
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        System.out.println("Addition result is"+ (num1+num2));
        System.out.println("Enter your choice 1-Add two numbers,2-Exit");
        int decision = input.nextInt();
        if(decision==2) {choice=false;}
      }while(choice==true);
      System.out.println("Thanks for using our Software");
    }
  }