yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 5 | Loops


3m read
·Nov 3, 2024

I'm a special guest on Mac Heads 101, and this is going to be my fifth Java tutorial. Today, I'm going to be talking about loops. So, I'm not really going to be making like a program with you right now; I'm just going to be demonstrating loops. Later, we'll be making a program that involves loops, so um, in another tutorial.

So, basically, what a loop is, is it allows us to execute a certain block of code a certain number of times in a row. The first one I'm going to show you is called a while loop. To do this, you type while (condition) and then you give it a condition. By the way, I'm going to make a variable so I can actually test a condition. So, say int i = 0;

So, while i is less than 20, do this. Now, of course, since 0 is less than 20, it's always going to be going. It's going to go indefinitely. But if I put i++, which, by the way, is another way of writing i = i + 1, that will add 1 to i every time the loop goes around, which means eventually it'll no longer be less than 20, and the loop will stop.

Okay, so, while i is less than 20, it's going to do this, and it's going to add one every time it loops. So, it's going to do this while i is 0 through 19, and I'm just going to print out i. So now, if I run this, it's going to print out the value of i each time it loops through, which was zero, and then it looped again and added until 19.

I'll show you that i = i + 1 will do the same thing. And there we go, it does the same thing. So, what that does is, like, i, let's say 0 = 0 + 1, so now it equals 1. Then it's going to loop through with it equaling one. So that's the while loop.

Now, I'm going to make a for loop, and the for loop has three things in it. So, I just had a condition: for (int i = 0; i <= 20; i++). So now, I'm creating the variable in here. Then my condition, i is less than or equal to 20; semicolon i++, and then System.out.printLn(i);.

So, that would print; that would do the exact same thing except, yeah, it goes up to 20 because I did equal to or less than. That, um, says i = 0, there's the condition i is equal to or less than 20, and i++ is just like the last one.

I'm only going to be covering those two types of loops in this tutorial, but the last thing I want to mention is called um, a nested loop. So, I'm just going to make um, two loops to demonstrate this. A nested loop is a loop in a loop, so what happens is, um actually, if I make it less than 20, it's going to do this: while (i < 5).

And then, a loop in a loop, I'll just call this int x = 0;, and as long as x is less than 20, x++, right? And then just put nothing in there or actually, yeah, put something: System.out.print(x);.

So, what this is going to do is every time it runs this loop, it's going to run this loop. So, it's going to do this until x is um, 4, because that's 1 less than 5. Until x equals 4, it's going to execute this loop. So, it's going to execute it once: as x = 0, as x = 1, as x = 2, as x = 3, as x = 4.

And so you'll see it will execute it um, this loop that number of times. See, there we go, and it did it again. All right, so those are the loops.

I'm sorry if I went a little fast um, I'll try to go slower in the next tutorial. But, um, see you next time!

More Articles

View All
m͏̺͓̲̥̪í͇͔̠ś̷͎̹̲̻̻̘̝t̞̖͍͚̤k̥̞à̸͕̮͍͉̹̰͚̰ẹ̶̢̪s͏̨͈̙̹̜͚̲ ̛̬͓͟
Hey, Vsauce. Michael here. The title of this video is misspelled in honour of mistakes. Mistakes are everywhere; they surround us like air. To err is human. Faults, flaws, faux pas, fumbles and fallacies are as much a part of who we are today as the stuff…
Making Yogurt | Live Free or Die: How to Homestead
I really like making my own yogurt. I have a quart of milk, and I have some already made yogurt in this bowl, and I have a pot. So the first step is to take your quart of milk and put it into a pot. I’m going to take this milk, and you’re going to warm i…
Getting Buried In Concrete To Explain How It Works
I am about to get buried in concrete. And while that’s happening, I’m going to explain everything you need to know about this substance. So the first thing that I want to clear up is the difference between cement and concrete, because people often mix the…
why is it so hard to live in the moment?
How much of life do you remember? [Music] Sam, you felt like you’re present in the current moment. You’re physically here, but our minds are always busy, always somewhere else. I heard this call and can’t stop thinking about it: you’re depressed because…
The Most Misunderstood Concept in Physics
This is a video about one of the most important, yet least understood concepts in all of physics. It governs everything from molecular collisions to humongous storms. From the beginning of the universe through its entire evolution to its inevitable end. I…
Subtracting with integer chips | Integers: Addition and subtraction | 7th grade | Khan Academy
Let’s say that we want to figure out what negative 8 minus negative 2 is. Now, there’s a lot of ways to approach this, but what we’re going to focus on in this video is to really build the intuition, and we’re going to do that with something called number…