Java For Loop Programs
In this tutorial, we will learn how to implement a for loop in different scenarios. But before moving further, if you are not familiar with the concept of for loop, then do check the article on Loops in Java.
Program 1: Java For Loop Program
In this program, we will see how to implement for loop program in java. Here, we will consider a scenario where we will find the multiplication table of a particular number. Instead of writing the multiplication table for each element, we will use a for loop for the same. We will write the statement once and it will be implemented multiple times.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a number
- Ask the user to initialize the number.
- Use a for loop to print the multiplication table of that number.
- Display the result.
- Stop.
Below is the code example of for loop in Java.
//Java Program to find the multiplication table of a given number
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt(); //Declare and initialize the number
System.out.println("The multiplication table of "+n+" is: ");
//Print the multiplication table
for(int i=1;i<=10;i++)
{
System.out.println(n+" * "+i+" = "+ (n*i));
}
}
}
Enter a number: 5
The multiplication table of 5 is:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Program 2: Java For Loop Program
In this program, we will see how to implement a for each loop program in java.
Algorithm
-
Start
-
Create an instance of the Scanner class.
-
Declare the array size.
-
Ask the user to initialize the array size.
-
Declare an array.
-
Ask the user to initialize the array.
-
Use a for loop to initialize the array.
-
Use a for each loop to print the array elements.
-
Print the array elements.
-
Stop.
Below is the code example of for loop in Java.
//Java Program to see the implementation of for and for-each loop
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n=sc.nextInt(); //Declare and initialize the number
int arr[]=new int[n]; //Declare an Array
System.out.println("Enter the array elements: ");
//Initialize the array
for(int i=0; i<n; i++)
{
arr[i]=sc.nextInt();
}
System.out.println("The array elements are: ");
//Print the number infinite times
for(int t:arr)
{
System.out.println(t);
}
}
}
Enter the size of the array: 5
Enter the array elements: 1 2 3 4 5
The array elements are:
1
2
3
4
5
Program 3: Java Program to Implement For Loop
In this program, we will see how to implement an infinite for loop. A point to be noted here is that, in order to exit from an infinite loop, you need to press ctrl+c.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a number.
-
Ask the user to initialize the number.
-
Print the number infinite times using a for loop.
-
Give infinite conditions in the for loop.
-
Display the result.
-
Stop.
Below is the java code example for the for loop.
//Java Program to see the implementation of infinite for loop
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");
int n=sc.nextInt(); //Declare and initialize the number
//Print the number infinite times
for(int i=1;i<=10;i--)
{
System.out.println(i + " The entered number is "+n);
}
}
Enter a number: 5
1 The entered number is 5
0 The entered number is 5
-1 The entered number is 5
-2 The entered number is 5
-3 The entered number is 5
-4 The entered number is 5
ctrl+c
Program 4:Java Program to Implement For Loop
In this program, we will see how to print a number infinite times using a for loop. A point to be noted here is that, in order to exit from an infinite loop, you need to press ctrl+c.
Algorithm:
-
Start
-
Create an instance of the Scanner class.
-
Declare a number.
-
Ask the user to initialize the number.
-
Print the number infinite times using a for loop.
-
Give infinite conditions in the for loop.
-
Display the result.
-
Stop.
Below is the code for the same.
//Java Program to see the implementation of infinite for loop
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
int n=sc.nextInt(); //Declare and initialize the number
//Infinite Loop Example
for(; ;)
{
System.out.println("Number is "+n);
}
}
}
Enter the number: 3
Number is 3
Number is 3
Number is 3
Number is 3
Number is 3
Number is 3
Number is 3
Number is 3
Number is 3
ctrl+c