loops in Java
![]() |
Image Source - Google | Image by - data-flair.training |
loops in a programming language is a feature which gives us the functionality to repeat the set of instructions when some condition is true. there are types of loops in Java
- for loop
- while loop
- do while loop
for loop- It is a control flow statement which consists of three parts initialization, condition checking, increament/decreament. The syntax of for loop is given below:
for(initialization;condition checking;increament/decreament)
{
statement;
}
Control flow in for loop-
![]() |
Image Source - Google - Image by - geeksforgeeks.com |
Working procedure of for loop-
First control starts from initializing a variable then it checks the condition if the condition is true control goes inside the for loop to execute the statement written inside the for loop after executing control again goes to the condition checking part while going towards the condition checking part it passes through the increment/decrement part of for loop do accordingly whether increment or decrement and then again goes to check the condition repeatedly this cycle occurs till the condition becomes false, when condition becomes false control comes out of the loop.
program 1- "print numbers from one to ten "using for loop
problem statement: print numbers from 1 to 10 using for loop
public class ForloopSyntax {
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
System.out.println();//for a gap of one line after each number
}
}
}
1
2
3
4
5
6
7
8
9
10
program 2- "print even numbers from 1 to 100" using for loop.
problem statement: print the even numbers from 1 to 100 not all the even numbers from 1 to 100 but numbers like 10, 30, 50, 70.....in this sequence using for loop with one line gap after each number.
public class ForloopSyntax {
public static void main(String[] args)
{
for(int i=1;i<=10;i+=2) // i+=2 means i=i+2 simply
{
System.out.println(i*10);
System.out.println();//for a gap of one line after each number
}
}
}
10
30
50
70
90
Imp. Note: It is not necessary to obtain the desired output this is the only code which works to produce this output. There can be some other pieces of code or different logics which also produce this output so try to build your own logic to produce this output if you want to become master in programming.
while loop- It is a control flow statement which consists of only one part in which the boolean condition is given. While loop can be used when you want to repeat some instructions infinitely. The syntax of while loop is given below-
while(boolean condition)
{
statement;
}
Control flow in while loop-
![]() |
Image Source - Google - Image by - geeksforgeeks.com |
Working procedure of while loop-
First control starts from checking the condition if it is true control goes inside the while loop to execute the statement again control goes to the check the condition repeatedly this cycle will occurs and when the condition becomes false control comes out of the loop.
program 3- "print blogging is fun infinitely" using while loop.
problem statement: print "blogging is fun infinitely" using while loop.
public class ForloopSyntax {
public static void main(String[] args)
{
while(true)
{
System.out.println("blogging is fun");
System.out.println();//for one line gap
}
}
}
blogging is fun
blogging is fun
blogging is fun
................
...............
............
............
do while loop- It is a control flow statement in which there are two parts to execute the statement first is do and second is condition. The syntax of do while loop is given below:
do
{
System.out.println("first time this will print without checking the condition");
}
while(condition check);
Control flow in do while loop-
![]() |
Image Source - Google - Image by - geeksforgeeks.com |
Working procedure of do while loop-
First control starts from executing the statement without checking the condition but after then the statement will be executed only when the condition becomes true if not control comes out of the loop.
program 4- " print the value of any number when only it is less than 9" using do while loop.
problem statement: print the value of number when only it is smaller than 9 for every time except for the first time. Initially the value of number could be anything just print it but then you have to check whether it is less than 9 or not.
public class ForloopSyntax {
public static void main(String[] args)
{
int number =10;
do
{
System.out.println("the number value is"+number+"\n");
number-=2;
//value becomes 8
}
while(number>0 && number<9);
}
}
the number value is10
the number value is8
the number value is6
the number value is4
the number value is2
program 5- " print the value of any number when it is between 10 and 30" using do while loop.
problem statement: print the value of number when only it is between than 10 and 30 for every time except for the first time. Initially the value of number could be anything just print it but then you have to check whether it lies between 10 and 30 or not.
public class AnotherExample {
public static void main(String[] args) {
int n=7;
do
{
System.out.println(n+"\n");
n+=5;
}
while(n>10 && n<30);
}
}
Output:
7
12
17
22
27
Imp.note- There is one more type of for loop is also there called for each loop to know about it in detail click on it.
Hello, visitor your comments are so helpful for us so do not hesitate just write the comment we read all your comments so don't think your comment goes waste.