Chapter Table of Contents
Section Table of Contents
Throughout this course, there may have been times where you typed the same or very similar code multiple times. For instance, if I wanted to print “Hello World” 5 times, I would have to type
System.out.println(“Hello World”) 5 times, and that sounds easy enough.But what if I wanted to do that 100 times? What about 1,000 times? At that point, you might’ve wished there was some structure that allowed you to repeat code. Fortunately, those structures do exist, and they’re called loops!Loops are great for repeating the same thing over and over again, also known as iteration. If you ever find yourself doing the same thing many times or copying and pasting the same code again and again, think to yourself, “Can I use a loop here?”The two loops that we will be talking about today (and also the most commonly used ones) are called for loops and while loops.