Bitsmore360 - Top trends, Tech News, Tips and Tricks: Java Ques 101: For Loop in Java with examples

Java Ques 101: For Loop in Java with examples

In this article, "Java Ques 101: For Loop in Java with examples", we will learn the basics of for loop in Java. We will also see some of the examples of for loops. Loop in java is used to repetitively execute a block of statements for a specified number of times.


Java Ques 101: For Loop in Java with examples

Java Ques 101: For Loop in Java with examples


Java For Loop

The Java for loop is a control statement that allows you to repeat a block of code for a specific number of times. It is also known as an iterative statement. It is used when you know in advance the number of times the loop should be run.

Syntax:


for(Initialization; Test Expression; Update Expression)
{
//Statement
}


Flow Chart: A flow chart is a diagrammatic representation of an algorithm. It represents the workflow or process in a sequential manner. Given Below is a flow chart of "For loop".












Example: Write a simple program in java to print Hello using For loop.


1
2
3
4
5
6
7
8
public class Demo1 {  
public static void main(String[] args) {  

for(int i=1;i<=5;i++){  
System.out.println("Hello");  
}  
}  
}  




















  • In First Iteration, the value of i will be 1, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.
  • In Second Iteration, the value of i will be 2, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.
  • In Third Iteration, the value of i will be 3, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.
  • In Fourth Iteration, the value of i will be 4, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.
  • In Fifth Iteration, the value of i will be 5, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.
  • As soon as the condition become falls i.e i becomes greater than 6 loop will stop executing the command.


Java Nested For loop


A For loop that is defined inside another For loop is called a nested for loop. The outer For loop will execute first. You can define any number of nested loops in a single for loop. A for loop can also contain another type of loop like a while loop.

Syntax:

for(InitializationTest ExpressionUpdate Expression)
{
for(
InitializationTest ExpressionUpdate Expression)
{
// inner loop statements.
}
// outer loop statements.
}

 

Example: Write a simple program in java to print Hello using Nested For loop.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Demo1 {  
public static void main(String[] args) {  

for(int i=1;i<=2;i++)
{  
for(int j=1;j<=2;j++)
{  
System.out.println("Hello");  
}  

}  
}
}  

Output:- 




Did you find this article helpful? OR Have any doubt, write in the comment section. To learn more about Bitsmore360, check out the rest of our Blog, and visit our Pinterest site and follow us on Instagram.

No comments:

Post a Comment