
Nested For Loops and Patterns in Java Programming
Explore the concept of nested for loops in Java programming, along with examples of fencepost patterns and practical code snippets. Dive into the basics of controlling program flow and enhancing your coding skills effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CSE 121 Lesson 5: Nested For Loops, Math, Random Matt Wang Spring 2024 Andy Anju Archit Arkita Autumn Christian TAs: Hannah H Hannah S Heather Hibbah Janvi Jessie Jonus Julia Luke Maria Mia Ritesh Shayna Simon Trey Vidhi Vivian Gumball? sli.do #cse121-5 Today s playlist: CSE 121 lecture beats 24sp
Announcements, Reminders Creative Project 1 is out, due Tue April 16th Resubmission Cycle 0 released, due Thu Apr 18th Eligible for submission: C0 & P0 Even if you're not resubmitting read your feedback!! Revisiting the minimum grade guarantees Lesson 5 - Spring 2024 2
Last time: for loops! For loops are our first control structure A syntactic structure that controls the execution of other statements. Lesson 5 - Spring 2024 3
Fencepost Pattern 1 Some task where one piece is repeated n times, and another piece is repeated n-1 times and they alternate h-u-s-k-i-e-s Lesson 5 - Spring 2024 4
Fencepost Pattern 2 Some task where one piece is repeated n times, and another piece is repeated n-1 times and they alternate h-u-s-k-i-e-s Lesson 5 - Spring 2024 5
(PCM) Nested for loops for (int outerLoop = 1; outerLoop <= 5; outerLoop++) { System.out.println("outer loop iteration #" + outerLoop); for (int innerLoop = 1; innerLoop <= 3; innerLoop++) { System.out.println(" inner loop iteration #" + innerLoop); } System.out.println(outerLoop); } Lesson 5 - Spring 2024 6
Poll in with your answer! What output is produced by the following code? sli.do #cse121-5 for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } 1 22 333 4444 55555 i ii iii iiii iiiii 1 12 123 1234 12345 A. B. C. Lesson 5 - Spring 2024 7
Poll in with your answer! What code produces the following output? sli.do #cse121-5 for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } for (int i = 1; i <= 5; i++) { for (int j = 1; i <= j; j++) { System.out.print(j); } System.out.println(); } 1 12 123 1234 12345 A. C. for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; i++) { System.out.print(j); } System.out.println(); } B. D. Lesson 5 - Spring 2024 8
Pseudo-Randomness Computers generate numbers in a predictable way using mathematical formulas. Input may include current time, mouse position, etc. True randomness is hard to achieve we rely on natural processes e.g., atmospheric noise, lava lamps Lesson 5 - Spring 2024 9
Why randomness? Randomness is a core part of computer science! It powers: cryptography security machine learning! But true randomness is really hard. If we just use math, someone could reverse the formula. LavaRand: CloudFlare s Wall of Lava Lamps So lava lamps. Lesson 5 - Spring 2024 10
(PCM) Random A Random object generates pseudo-random numbers. The Random class is found in the java.util package import java.util.*; We can seed the generator to make it behave deterministically (helpful for testing!) Method Description nextInt() Returns a random integer nextInt(max) Returns a random integer in the range [0, max), or in other words, 0 to max-1 inclusive nextDouble() Returns a random real number in the range [0.0, 1.0) Lesson 5 - Spring 2024 11
Poll in with your answer! Assuming you ve declared: Random randy = new Random(); sli.do #cse121-5 Which of these best models picking a random card? (1-13 inclusive) A.randy.nextInt() B.randy.nextInt(13) C.randy.nextInt(13) + 1 D.randy.nextInt(14) Lesson 5 - Spring 2024 12
Calling: Math.<method>( ) (PCM) Math Method Description Math.abs(value) Returns the absolute value of value Math.ceil(value) Returns value rounded up Math.floor(value) Returns value rounded down Math.max(value1, value2) Returns the larger of the two values Math.min(value1, value2) Returns the smaller of the two values Math.round(value) Returns value rounded to the nearest whole number Math.sqrt(value) Returns the square root of value Math.pow(base, exp) Returns base raised to the exp power Lesson 5 - Spring 2024 13
Food for Thought ? This week s food for thought is: one of matt s favourite areas of computer science less related to tech & society than the others also the most ambitious, so don t stress about it sit back, enjoy the ride :) Lesson 5 - Spring 2024 14
Wouldnt it be nice We ve seen that some for loops go on forever: for (int i = 0; i < 10; i--) { System.out.println(i); } for (;true;) { } Wouldn t it be nice if Java (or the compiler ) could catch this for us? I mean, the loop obviously never ends Lesson 5 - Spring 2024 15
The Halting Problem (1/2) Benedict Cumberbatch showed that it s impossible to generally solve this problem. Regardless of: how good (big, fast) your computer is how good your algorithm is what people come up with the future! Given a Java program, it is impossible to always know if it eventually stops (or loops infinitely). Lesson 5 - Spring 2024 16
The Halting Problem (2/2) Benedict Cumberbatch Alan Turing showed that it s impossible to generally solve this problem. Regardless of: how good (big, fast) your computer is how good your algorithm is what people come up with the future! Given a Java program, it is impossible to always know if it eventually stops (or loops infinitely). Alan Turing at 24 (1936). He had a storied (if also very tragic and short) life. Lesson 5 - Spring 2024 17
Many, many problems are unsolvable. I don t mean we currently don t know how to solve them . I mean, there is no algorithm that will ever solve them . Here are some related undecidable problems: given a Java program, are all the types correct? given a polynomial equation, does it have integer solution(s)? given any Magic: The Gathering board, does either player have a guaranteed winning strategy? Lesson 5 - Spring 2024 18
This statement is false In fact, there s an even more concerning result: there is at least one math statement that we can t prove true or false. What is that statement? It looks something like This statement is false . Lesson 5 - Spring 2024 19
In search of perfection Even though we know it s impossible , we still: try avoiding infinite loops type-check our Java programs play Magic: The Gathering (?) try to prove things in (and do) math! Lesson 5 - Spring 2024 20
Dessert for Thought! I argue there are two takeaways: 1. Don t let perfection be the enemy of the good! applies to you in CSE 121 and as a programmer :) fundamental basis of much of computer science 2. Like thinking about these sorts of problems? This is also computer science! (not all CS is just coding ) See: CSE 311, CSE 417/431 Lesson 5 - Spring 2024 21