Friends, I wanted to teach pattern programs from Java Loops. But most people don't know much about loops. So I have taught you all kinds of loops in Java (do-while loop, while loop, for loop, for-each loop). If you do not know about it, you can find out about it by clicking on the link below. Only if you read everything carefully will you understand the loop.
- Java Do While Loop with Simple Example
- Java While Loop with Simple Example
- Java For Loop with Simple Example
- Java For-each Loop with Simple Example
I told you about the loop, but based on that loop only we can do basic pattern programs from Java. If you want to achieve a lot, you have to rotate the loop a lot. This requires writing a loop inside the loop. So today I am going to teach you how to write a loop inside a loop. This concept is called nested loop in the world of programming.
In this post, I will just talk about Nested for Loop. All the rest were the same in all the loops. But if there are more than 5 comments on this post, of course, I will teach on the rest of the loop as well.
Friends, to put it simply, when you want to do something related to the matrix, you have to do it nested for loop. You will understand everything as the programs come later.
In a few days, I will teach you Star Pattern Programs and Number Pattern Programs from Java Programming. That will require all these types of loops. So learn all the concepts. Let us now learn Nested for Loop in JAVA.
What is a loop in java?
Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.
What is a Nested loop in java?
We can write a loop within another loop. Such loops are called Nested Loops or Inner Loops.
Types of Nested loops in java?
In Java, there are 3 kinds of loops which are – the nested for loop, the nested while loop, and the nested do-while loop. All these three-loop constructs of Java execute a set of repeated statements as long as a specified condition remains true. This particular condition is generally known as loop control.
4 Easy Ways to Become an Expert in Java Nested Loops?
- To know the syntax of loops.
- Understanding the flow of the loop.
- To do Dry Run.
- To practice.
I am going to teach these four options below. Read everything carefully and practice. Comment if you have any problems.
Java Nested For Loop Overview
We can write a for loop within another for a loop. Such loops are called Nested For Loops.
Syntax of a nested for loop in java
for(expression1; expression2; expression3){
statements;
for(expression1; expression2; expression3){
statements;
}
}
Java Nested For Loop with Simple Example
Example
for(int i=1; i<=2; i++){
statements 1; //these are executed 2 times
}
- The preceding for loop gets executed for 2 times by changing i values from 1 to 2. Let us take another for loop as.
for(int j=1; j<=4; j++){
statements 2; //these are executed 4 times
}
- This loop is executed 4 times by changing j values from 1 to 4. If we write this loop inside the preceding for loop, it looks like this.
public class MyClass {
public static void main(String args[]) {
for(int i=1; i<=2; i++){
//these are executed 2 times
System.out.println("Outer : "+i+" Time Executed");
for(int j=1; j<=4; j++){
//these are executed 8 times
System.out.println("Inner : "+j+" Time Executed");
}
}
}
}
Output:
Outer : 1 Time Executed
Inner : 1 Time Executed
Inner : 2 Time Executed
Inner : 3 Time Executed
Inner : 4 Time Executed
Outer : 2 Time Executed
Inner : 1 Time Executed
Inner : 2 Time Executed
Inner : 3 Time Executed
Inner : 4 Time Executed
Java Nested For Loop Program Dry Run
In this case, the execution starts from the outer for loop and hence i=1. Then statement 1 will be executed once. Now, the execution enters the second for loop and the j value will be 1. Now statements 2 will be executed once. After this, the j value will be 2, and statements 2 will be executed once. After this, the j value will be 2, and statement 2 will be executed again. Like this, the inner for loop is executed 4 times, with j values changing from 1 to 4. This means statement 2 will be executed 4 times.
When the inner for loop is completed, then the execution goes to the outer for loop and i value will be 2. This time, the execution again comes into the inner for loop, and statement 2 will be executed 4 times. Then the execution goes to outer for loop and i value will be 3. The condition is false then the outer for loop breaks the program.
The preceding sequence represents that the outer for loop is executed totally 2 times and hence statement 1 will be executed 2 times. The inner for loop is executed 4 times for each i value and hence statement 2 will be executed 8 times.
In the same way, it is also possible to write a while loop or do-while loop inside a for loop and vice-versa. These are called nested loops.
Java Nested For Loop Examples for Practice
Now your Java Nested For Loop problem will be solved. If there is still any problem, read the post carefully.
Now you want to practice Nested For Loop. Here are some Nested For Loop examples, you want to solve. Many of these examples also ask the interviewer. Download the following file for its solution.
Program 1: Write a Program t display the following Pattern.
* * * *
* * * *
* * * *
* * * *
Program 2: Write a Program to display that starts in a triangular form - single star in the first line, two stars in the second line, three in the third line, and so on.
*
* *
* * *
* * * *
- Download Popular PC Games
IBPS Clerk Recruitment 2021 | Government Jobs 2021
ReplyDeleteApply Now