Types of Nested Loops in Java?

You can nest as many for while loops as you'd practically want in Java: there's no practical limit.

Up vote 2 down vote favorite share g+ share fb share tw.

I have a simple question. I have to find out many nested loops as possible in java. I have something like for loop and if statement inside.

I know that we can do like if{if{if{if{ something like that too. Just need some more idea of more types of nested loops. If you can write down some examples.

I'll be very glad. Thank you. Public class Test { public static void main (String args) { int I = 0; for(int j = 1; j Print("**"); } System.out.println(); } } } java for-loop link|improve this question edited May 8 '10 at 9:50skaffman114k8135227 asked May 8 '10 at 7:40dominoos112.

I guess the title answers my question, anyway. – jweyrich May 8 '10 at 7:51.

You can nest as many for/while loops as you'd practically want in Java: there's no practical limit. Java has 4 looping constructs: JLS 14.14 The for Statement JLS 14.14.1 The basic for Statement JLS 14.14.2 The enhanced for Statement (aka "for-each") JLS 14.14 The while Statement JLS 14.14 The do Statement (aka "do-while") Java does not have goto (which isn't really needed anyway). See also Java Nuts and Bolts/the for Statement Java language guide/for-each loop Examples This is a typical example of a simple "triangle"-type nested loop, where the number of iteration of the inner loop depends on the value being iterated in the outer loop: for (int I = 1; I Print("*"); // ** } // *** System.out.println(); // **** } // ***** Here's an example of a "pairing"-type nested loop, where the two loops are independent of each other, using the for-each construct: int numbers = { 1, 2, 3 }; // prints: // if swapped: char letters = { 'A', 'B', 'C' }; // 1 A // 1 A // 1 B // 2 A for (int number : numbers) { // 1 C // 3 A for (char letter : letters) { // 2 A // 1 B System.out.

Println(number + " " + letter); // 2 B // 2 B } // 2 C // 3 B } // 3 A // 1 C // 3 B // 2 C // 3 C // 3 C The for-each construct, which lacks explicit indices, makes the indepedence of both loops obvious: you can swap the two for statements in the above code, and you'd still get all pairs, though listed in a different order. This use of a boolean method for a while loop (this one from java.util. Scanner) is typical: Scanner sc = new Scanner("here we go again"); // prints: while (sc.hasNext()) { // hereeeee String s = sc.next(); // weeeee char lastChar = s.

CharAt(s.length() - 1); // gooooo for (int I = 0; I Println("Hello! "); } while (x! = 0); The above loop prints Hello!

: the body of a do-while is executed before the termination condition is checked. A more elaborate example Here's an example of a nested-loop logic, but refactored into methods to make things more readable. This is something that is important for beginners to learn: just because you can physically nest loops as many levels as you want, doesn't mean you should.

By breaking apart the logic like this, the program becomes more modular and readable, and each logic stands on its own and can be tested and reused, etc. This snippet reverses the letters of a word in a char in-place. Static void swap(char arr, int i, int j) { char t = arri; arri = arrj; arrj = t; } static void reverse(char arr, int from, int to) { int N = (to - from); for (int I = 0; I Println(new String(sentence)); // prints "gnisrever srettel fo sdrow ni ecnetnes" } This example is also instructive in that even though it's essentially a nested loop algorithm, it's actually O(N)! It's a mistake to think that any doubly nested-loop algorithm must be O(N^2) -- it really depends on the algorithm itself more than just the physical structure.

Nested-loop Algorithms These are classical algorithms traditionally implemented using nested loops (at least in their naive forms): O(N^2) Sorting algorithms: Bubble sort Selection sort Insertion sort O(N^3) triply nested loop algorithms: Matrix multiplication Table-filling dynamic programming algorithms Levenshtein distance (O(N^2), string edit distance) Floyd-Warshall Algorithm (O(N^3), all-pairs shortest path in graph) These are far from an exhaustive sampling, but it should provide a good introduction to a variety of nested for loops algorithms for beginners.

Thank you very much. The reason I am asking you this was I have to group book project about nested loop. I wanted to show many many types of nested loops as possible.

So, while, for, if are the only things we can use? – dominoos May 8 '10 at 7:50 @dominoos: are you asking how many control structures there are in Java? Because that's an entirely different question.

– polygenelubricants May 8 '10 at 7:51 I was asking for examples of nested loops. And I wanted to show verities of nested loops. – dominoos May 8 '10 at 7:56 @dominoos: I can't be doing your group book project for you.

I've done all I can here, sorry. – polygenelubricants May 8 '10 at 8:19.

The code if { if { if { ... } } } is not an example of nested loops. If does not use loop. Looping expressions are for example for and while.

3 Actually, there is a theoretical limit in that a single class can only have 64K bytes of bytecodes. But in practice, you are unlikely to reach that limit by simply nesting loops. – Stephen C May 8 '10 at 8:30.

Loops can be done using the 3 basic loop statements: for, while and do..while. However, if your question is about how many types of nested loops are possible in Java, I'd say LOTS, since you can arrange and mix them as you wish.

I guess it depends on the compiler. I wrote a simple test program that generates Java files with different levels of loop nesting. Import java.io" rel="nofollow">java.io.

BufferedWriter; import java.io" rel="nofollow">java.io. FileWriter; public class NestingTest { public static void main(String args) throws Exception { for (int n = 1; n Write("public class "); o. Write(className); o.

Write("\n{\npublic static void main(String args) {\n"); for (int I = 0; I Write("while(true) {\n"); } for (int I = 0; I Compile test5000. If it succeeds, compile test7500, and if it doesn't, compile test2500. After 13 or 14 steps, you should come to a conclusion.

My compiler's sweetspot seems to be 5345 levels of nesting for this simple program, so I guess in practice it doesn't matter at all.

I like the previous answer, but in an attempt to answer directly this curious question: if-else is not a loop type. Java has for, while, and do-while loops. You could argue the the "foreach" syntax for for loops, introduced in Java 5, is a different kind of loop for your purpose.

But really it is just shorthand for writing a while loop. But all of these "different" loop types are just language constructs. In the byte code that is compiled, they're not intrinsically different.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions