Inner for Loops How to Make It Loop Again
four.4. Nested For Loops¶
A nested loop has i loop inside of another. These are typically used for working with two dimensions such as printing stars in rows and columns equally shown below. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations earlier the outer loop can continue to its next iteration.

Effigy 1: Nested Loops¶
Coding Exercises
What does the following code print out? Watch the code run in the Coffee visualizer by clicking the CodeLens push and then forward. Notice how the inner loop is started over for each row. Can you predict how many rows and columns of stars at that place volition be?
Can you change the code to print a rectangle with 10 rows and eight columns of stars? You tin besides try replacing line 10 with this print statement to meet the rows and columns: System.out.print(row + "-" + col + " ");
Check your understanding
- A rectangle of 7 rows with 5 stars per row.
- This would exist true if i was initialized to 0.
- A rectangle of seven rows with 4 stars per row.
- This would be truthful if i was initialized to 0 and the inner loop continued while
y < five
. - A rectangle of 6 rows with v stars per row.
- The outer loop runs from ane upward to 7 but non including 7 then at that place are 6 rows and the inner loop runs 1 to five times including five so in that location are 5 columns.
- A rectangle of 6 rows with 4 stars per row.
- This would be true if the inner loop continued while
y < 5
.
4-4-2: What does the following code impress?
for ( int i = ane ; i < 7 ; i ++ ) { for ( int y = 1 ; y <= v ; y ++ ) { System . out . print ( "*" ); } Organisation . out . println (); }
- A rectangle of four rows with 3 star per row.
- This would be true if i was initialized to 1 or ended at 4.
- A rectangle of five rows with three stars per row.
- Yeah, the outer loop runs from 0 up to 5 but not including 5 so at that place are 5 rows and the inner loop runs from 3 downward to 1 so 3 times.
- A rectangle of 4 rows with 1 star per row.
- The inner loop runs 3 times when j is 3, 2, and and then 1, and so there are 3 stars per row.
- The loops have errors.
- Endeavour the code in an Active Lawmaking window and yous volition run across that information technology does run.
4-4-iii: What does the following code impress?
for ( int i = 0 ; i < 5 ; i ++ ) { for ( int j = 3 ; j >= i ; j -- ) { System . out . print ( "*" ); } System . out . println (); }
The master method in the post-obit class should print x rows with 5 <lawmaking>*</lawmaking> in each row. But, the blocks have been mixed up and include 1 extra block that isn't needed in the solution. Drag the needed blocks from the left and put them in the correct club on the correct. Click the Check button to bank check your solution.
public form Test1 { public static void primary(String[] args) { --- for (int x = 0; 10 < ten; x++) { --- for (int y = 0; y < five; y++) { --- for (int y = 0; y <= 5; y++) { #paired --- System.out.print("*"); --- } --- System.out.println(); --- } --- } }
iv.4.1. Nested Loops with Turtles¶
Try nested loops with turtles to create a snowflake design!
Coding Exercise
The turtle beneath is trying to depict a square many times to create a snowflake pattern. Tin can yous alter the outer loop and then that the pattern completes all the manner around? Try unlike ending values for the counter i to detect the smallest number that works between five and fifteen.
If the code below does non piece of work in your browser, y'all can re-create the code into this repl.information technology link (refresh page later on forking and if it gets stuck) or download the files here to apply in your own IDE.
4.4.2.
Programming Challenge : Turtle Snowflakes¶
In the last exercise, you lot used nested for-loops to have the turtle draw a square repeatedly to make a snowflake. Use the Agile Code window beneath or this repl.information technology link to take yertle draw the following shapes using nested loops. Nosotros encourage yous to work in pairs on this.
-
Consummate the code in the active code window beneath to draw a snowflake of triangles. Remember that triangles accept 3 sides and you will demand to plow 120 degrees (external angle) 3 times to draw the triangle. Use the turnAmount variable for the single turn after drawing a triangle. How many times did yous need to run the outer loop to get all the way around? Try irresolute the turnAmount variable to 40 to come across how many times you demand to loop with a wider distance between the triangles.
-
In the exercise above, you figured out how many times to run the outer loop to finish the snowflake. Yous may take noticed that the number of times the loop needs to run is related to the bending you plough before drawing the next triangle. These turns have to add together up to 360 degrees to go all the way around. Change the outer loop then that information technology runs the number of times needed past using a formula with the turnAmount variable and 360. Can you describe a snowflake using more than or less triangles than before past simply irresolute the turnAmount value?
-
Create another variable called n for the number of sides in the polygon the inner loop draws. Change the angle in the inner loop to besides use a formula with 360 and this new variable. Can yous change your snowflake to describe squares or pentagons instead? (Note if this overwhelms the Active Lawmaking server and times out, try a larger turnAmount. (Or you can switch to using this repl.it link or your ain IDE).
-
Allow'south add together some more color! Add an if/else statement that changes the Color of the pen before the inner loop depending on whether the outer loop variable is odd or even. Remember that even numbers have no residual when divided past 2.
-
Exist creative and design a unique snowflake!
Use nested for-loops to have the turtle draw a snowflake of polygons. Use the variable turnAmount to plough after each shape and the variable north for the sides of the polygon.
If the code below does not work in your browser, you can copy the lawmaking into this repl.it link (refresh page after forking and if it gets stuck) or download the files hither to use in your own IDE.
four.iv.3. Summary¶
-
Nested iteration statements are iteration statements that announced in the body of another iteration statement.
-
When a loop is nested inside some other loop, the inner loop must complete all its iterations before the outer loop can go on.
You lot have attempted of activities on this page
mcintyrehingivend40.blogspot.com
Source: https://runestone.academy/ns/books/published/csawesome/Unit4-Iteration/topic-4-4-nested-loops.html
ارسال یک نظر for "Inner for Loops How to Make It Loop Again"