Nested do while in java

Nested do while loop in Java programming language

Nested do while loop in Java programming language

initially, the initialization statement is executed only once and statements execute only one. Then, the flow of control evaluates the test expression .

When the test expression is true , the flow of control enters the inner loop and codes inside the body of the inner loop is executed and updating statements are updated. This process repeats until the test expression is false (inner while loop)

Then, when the test expression is false , loop exit from the inner loop and flow of control comes to the outer loop. Again, the flow of control evaluates test condition .

If test condition for outer loop returns as true , the flow of control executes the body of while loop statements and return as false . The flow of control stops execution and goes to rest.

Examples for the nested do-while loop

class NestedDoWhile< public static void main(String args[])< int row=1,column=1; int x; do< x=4; do< System.out.print(""); x--; >while(x>=row); column=1; do< System.out.print(column+" "); column++; >while(column<=5); System.out.println(" "); row++; >while (row

This program displays a square number pattern in Java

When the above code is executed, it produces the following results:

12345 12345 12345 12345 12345
class NestedDoWhile< public static void main(String args[])< int row=1,column=1; int x; do< x=4; do< System.out.print(""); x--; >while(x>=row); column=1; do< System.out.print("*"+" "); column++; >while(column<=5); System.out.println(" "); row++; >while (row <=5); >>

When the above code is executed, it produces the following results:

The above program displays a square star pattern in Java using nested while loop

This program displays a Floyd triangle number pattern using the nested do-while loop in Java

class NestedDoWhile< public static void main(String args[])< int row=1,column=1; int x; do< x=4; do< System.out.print(""); x--; >while(x>=row); column=1; do< System.out.print(row+" "); column++; >while(column<=row); System.out.println(" "); row++; >while (row <=5); >>

When the above code is executed, it produces the following results:

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

This program displays Floyd’s triangle number pattern using the nested do-while loop in Java

class NestedDoWhile< public static void main(String args[])< int row=1,column=1; int x; do< x=4; do< System.out.print(""); x--; >while(x>=row); column=1; do< System.out.print(column+" "); column++; >while(column<=row); System.out.println(" "); row++; >while (row <=5); >>

When the above code is executed, it produces the following results:

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

This program displays a Floyd triangle number pattern using the nested do-while loop in Java

class NestedDoWhile< public static void main(String args[])< int row=1,column=1; int x; do< x=4; do< System.out.print(""); x--; >while(x>=row); column=1; do< System.out.print("*"+" "); column++; >while(column<=row); System.out.println(" "); row++; >while (row <=5); >>

When the above code is executed, it produces the following results:

This program displays a Floyd triangle star pattern using the nested do-while loop in Java

There are other Java language keywords that are similar to this post

Suggested for you

Источник

Nested while loop in Java programming language

Nested while loop in Java programming language

When a while loop exists inside the body of another while loop , it is known as nested while loop in Java. Initially, the outer loop executes once and the afterwards inner loop begins to execute. Execution of the inner loop continues until the condition of the inner loop is satisfied(until the test expression is false ).

Once the Condition of the inner loop is satisfied, the flow of control comes out to the outer loop for next iteration.

Declaration

Flow diagram for the nested while loop

Nested while loop in Java programming language

Example for nested while loop in Java

When we execute the above program, it produces the following results

1 outer loop executed only once 1 inner loop executed until to completion 2 inner loop executed until to completion 3 inner loop executed until to completion 4 inner loop executed until to completion 2 outer loop executed only once 1 inner loop executed until to completion 2 inner loop executed until to completion 3 inner loop executed until to completion 4 inner loop executed until to completion 3 outer loop executed only once 1 inner loop executed until to completion 2 inner loop executed until to completion 3 inner loop executed until to completion 4 inner loop executed until to completion

Print rectangular number pattern using nested while loop in Java

class nestedwhile < public static void main(String args[])< int i=1,j=1; while(i<=10) < while(j<=10) < System.out.print(j); j++; >i++; System.out.println(""); j=1; > > >

When we execute the above program, it produces the following results

12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910

Print rectangular star pattern using nested while loop in Java

class nestedwhile < public static void main(String args[])< int i=1,j=1; while(i<=10) < while(j<=10) < System.out.print("*"); j++; >i++; System.out.println(""); j=1; > > >

When we executed above program, produces the following result

Print triangle star pattern using nested while loop in Java

class nestedwhile < public static void main(String args[])< int i=1,j=1; while(i<=10) < while(j<=i) < System.out.print("*"); j++; >i++; System.out.println(""); j=1; > > >

When we execute above program, produces the following result

Источник

Nested Loop in Java

If a loop exists inside the body of another loop, it’s called a nested loop. Here’s an example of the nested for loop.

// outer loop for (int i = 1; i .. >

Here, we are using a for loop inside another for loop.

We can use the nested loop to iterate through each day of a week for 3 weeks.

In this case, we can create a loop to iterate three times (3 weeks). And, inside the loop, we can create another loop to iterate 7 times (7 days).

Example 1: Java Nested for Loop

Week: 1 Day: 1 Day: 2 Day: 3 . .. . Week: 2 Day: 1 Day: 2 Day: 3 . .. . . .. . 

In the above example, the outer loop iterates 3 times and prints 3 weeks. And, the inner loop iterates 7 times and prints the 7 days.

We can also create nested loops with while and do. while in a similar way.

Note: It is possible to use one type of loop inside the body of another loop. For example, we can put a for loop inside the while loop.

Example 2: Java for loop inside the while loop

Week: 1 Day: 1 Day: 2 Day: 3 . .. . Week: 2 Day: 1 Day: 2 Day: 3 . .. . . .. . 

Here you can see that the output of both Example 1 and Example 2 is the same.

Example 3: Java nested loops to create a pattern

We can use the nested loop in Java to create patterns like full pyramid, half pyramid, inverted pyramid, and so on.

Here is a program to create a half pyramid pattern using nested loops.

class Main < public static void main(String[] args) < int rows = 5; // outer loop for (int i = 1; i System.out.println(""); > > >
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

break and continue Inside Nested Loops

When we use a break statement inside the inner loop, it terminates the inner loop but not the outer loop. For example,

class Main < public static void main(String[] args) < int weeks = 3; int days = 7; // outer loop for(int i = 1; i System.out.println(" Days: " + j); > > > >
Week: 1 Day: 1 Day: 2 . .. . Week: 2 Week: 3 Day: 1 Day: 2 . .. . . .. . 

In the above example, we have used the break statement inside the inner for loop. Here, the program skips the loop when i is 2.

Hence, days for week 2 are not printed. However, the outer loop that prints week is unaffected.

Similarly, when we use a continue statement inside the inner loop, it skips the current iteration of the inner loop only. The outer loop is unaffected. For example,

class Main < public static void main(String[] args) < int weeks = 3; int days = 7; // outer loop for(int i = 1; i System.out.println(" Days: " + j); > > > >
Week: 1 Days: 2 Days: 4 Days: 6 Week: 2 Days: 2 Days: 4 Days: 6 Week: 3 Days: 2 Days: 4 Days: 6

In the above example, we have used the continue statement inside the inner for loop. Notice the code,

Here, the continue statement is executed when the value of j is odd. Hence, the program only prints those days that are even.

We can see the continue statement has affected only the inner loop. The outer loop is working without any problem.

Table of Contents

Источник

Different Nested Loops in Java Explained [Practical Examples]

If a loop is written inside the body of the another loop, it is referred as a nested loops. There are three different types of loops supported in Java. They are for loop, while loop and do while loop. This loops can be nested with similar type or with the combination of other loops. There is no limitations on number of times loops are nested. Moreover, we can nest them in any combination.

Nested for loop

We are using a for loop inside the other for loop. So, in all it will execute for n * m times where n is number of iteration of outer loop and m is number of iteration of inner loop.

The syntax of nested for loop is as shown below.

for (initialization; condition; increment / decrement) < for (initialization; condition; increment / decrement) < // statement of inside loop >// statement of outer loop >

Example : In this example, we are using two for loops to draw a pattern of *.

// Program to print pattern of * using nested loops class Main < public static void main(String[] args) < // Declaring variable int i, j; // Using nested for loop and printing for (i = 1; i System.out.println(); > > >

Nested While loop

We are using a while loop inside the other while loop. So, in all it will execute for n * m times where n is number of iteration of outer loop and m is number of iteration of inner loop.

The syntax of nested while loop is as listed below.

while (condition) < while (condition) < // statement of inside loop >// statement of outer loop >

Example : In this example, we are using two while loops to print a multiplication table of 5 and 6.

// Program to print multiplication table of 5 and 6 using nested loops class Main < public static void main(String[] args) < // Declaring variable int i = 5, j; // Using nested for loop and printing while (i i++; > > >
Multiplication table of 5 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 Multiplication table of 6 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60

Nested do-while loop

We are using a do-while loop inside the other do-while loop. So, in all it will execute for n * m times where n is number of iteration of outer loop and m is number of iteration of inner loop.

The syntax of nested do-while loop is as shown below.

do < do < // statement of inside loop >while (condition); // statement of outer loop > while (condition);

Example : In this example we are having a menu driven program that computes addition and multiplication of n numbers.

import java.util.Scanner; // Menu driven program using nested loops class Main < public static void main(String[] args) < int ch, count; Scanner sc = new Scanner(System.in); int a, result, n; do < System.out.println("Enter your choice \n1. Addition \n2. Multiplication"); ch = sc.nextInt(); switch (ch) < case 1: count = 0; result = 0; System.out.println("Enter how many numbers you want to add"); n = sc.nextInt(); do < System.out.println("Enter a number " + (count + 1)); a = sc.nextInt(); result = result + a; count++; >while (count < n); System.out.println("Summation result is " + result); break; case 2: count = 0; result = 1; System.out.println("Enter how many numbers you want to multiply"); n = sc.nextInt(); do < System.out.println("Enter a number " + (count + 1)); a = sc.nextInt(); result = result * a; count++; >while (count < n); System.out.println("Multiplication result is " + result); break; >> while (ch >
Enter your choice 1. Addition 2. Multiplication 1 Enter how many numbers you want to add 3 Enter a number 1 10 Enter a number 2 20 Enter a number 3 30 Summation result is 60 Enter your choice 1. Addition 2. Multiplication 2 Enter how many numbers you want to multiply 3 Enter a number 1 10 Enter a number 2 20 Enter a number 3 30 Multiplication result is 6000 Enter your choice 1. Addition 2. Multiplication 3

Examples using Hybrid Nested Loops

Example 1 : Find repeated words in a string using for loop and while loop

In this example, we are finding the list of words repeated in the given String.

// Program to find repeated words in a string using nested loops public class Main < public static void main(String[] args) < String s = "Life is like a book. Life is like a cup of tea. Life is like a rose garden. Life is like a dream"; int count; // Converts the string into lowercase s = s.toLowerCase(); // Splitting the string to words for comparison String w[] = s.split(" "); // Finding the repeated words from a string System.out.println("Finding repeated words in a string : "); int i = 0; while (i < w.length) < count = 1; for (int j = i + 1; j < w.length; j++) < if (w[i].equals(w[j])) < count++; // Setting w[j] to 0 again to avoid printing visited word w[j] = "0"; >> // Displays the repeated word if count is greater than 1 if (count > 1 && w[i] != "0") System.out.println(w[i]); i++; > > >
Finding repeated words in a string : life is like a

Example 2 : Print transpose of a matrix

In this example, we are printing the transpose of a two dimensional matrix.

// Program to print transpose of a matrix using nested loops public class Main < public static void main(String[] args) < int r, c; // Initialize matrix a int a[][] = < < 1, 2, 3 >, < 4, 5, 6 >, < 7, 8, 9 >>; // Finding the number of rows and columns present in given matrix r = a.length; c = a[0].length; // Declare array to store transpose int t[][] = new int[c][r]; int j; // Calculates transpose of given matrix for (int i = 0; i < c; i++) < j = 0; while (j < r) < t[i][j] = a[j][i]; j++; >> // Printing transpose for (int i = 0; i < c; i++) < for (j = 0; j < r; j++) < System.out.print(t[i][j] + " "); >System.out.println(); > > >

Example 3 : Print pattern using do-while and for loop

In this example, we are printing the transpose of a two dimensional matrix.

public class Main < public static void main(String[] args) < int i = 1, j, n = 5; do < // this loop is used to print the n for (j = n; j >= 1; j--) < // this loop is used to print numbers in a line if (j != i) System.out.print(j); else System.out.print("*"); >System.out.println(""); i++; > while (i < n); >>

Summary

The knowledge of Nested loops in Java is very useful while working on a real time applications. There are various combinations that can be used depending on requirement. In this tutorial, we covered nested loops in Java along with the examples of hybrid nested loops. As per the requirement of an application, we can choose an appropriate loops. We learned in detail about this looping with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on Nesting loops in Java.

Источник

Читайте также:  Как сжать html страницы
Оцените статью