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

Java Lesson 4


4m read
·Nov 3, 2024

Hey guys, this is Mackielaw with Java lesson four.

So in this Java lesson, what we're going to be doing is we're going to be making an app that makes a Starbucks a five by five square of stars. The way we're going to do this is not by just doing System.out.printline("star star star star") or System.out.printline("star star star star star"), etc. We're going to use something called loops. If you already have programming experience, you might already know about loops.

So, if I open Xcode, I can make a new project. While it's loading, I'll explain to you: a loop is a piece of code that runs multiple amounts of times. You can make it run while a condition is true or while all the numbers are less than another number. You can have loops inside of loops too.

So either way, I'm going to create a new project. Then I'm going to make a Java tool and I'm going to call it "star box". Okay, now under source, I'm going to edit starbucks.java, so I'll get rid of all this code they already had for you.

Now here's the type of loop we're going to be using. We're going to be using a type of loop called a for loop. There are two types of loops: a while loop and a for loop. A for loop has three different parameters you give it, while a while loop only has one.

So here's how a for loop works: for (, then the code right here is a code that runs the first time it hits this loop, or the first time around in this loop. So I'll say int i = 0;, and then you do a semicolon. The next thing here is the condition, so while this condition is true, continue going on. So I'm gonna make sure i is less than five. By the way, you can use the less than and greater than sign in conditions, and then a semicolon. The third thing is the code that happens every time, so i = i + 1. An easier way to add one to a number is just to do the number then two pluses directly after it.

So we're adding one to i. Then we do {, and now we use curly braces to contain the code inside of the for loop. So here's a for loop: this for loop, the code inside of this for loop, will happen five times. Now here's what we're going to do right now: we're going to do System.out.print("*"); so right now this app is going to print out five stars.

Okay, so if you run it, go into the console, here's our five stars. So let's say we want it to print out five stars five times. I'm going to add another for loop in here: for (int j = 0; j < 5; j++).

Okay, now outside of this for loop, I'm going to do System.out.print("\n"); and that just makes a new line. So what this code is doing: it's hitting this, then it's going to this. This code will run five times, so it'll print five stars. Then I'll get out of that for loop and print a new line.

Now this loop has repeated, so now i is equal to 1. Then it starts this for loop over again, prints out five stars once again, and then prints a new line. Now i is equal to 2, it does it again, prints five stars, and then a new line again, and again. Eventually, you have a box of stars.

So I'll demonstrate, and here's a box of five stars. Now say we wanted to print out 10 stars every line. So all we do is change j while it's less than 10. So if it starts out at 0 and we're doing it while it's less than 10, then it's going to print out 10 stars instead of 5 stars.

It's still going to print out only 5 new lines because it's only running this code 5 times, so if we run this now, it's 10 by 5. Now it looks square, but since going up makes it longer than going across, it looks more square.

So this is for loops. Now I'll show you while loops. A while loop is like a for loop, except without the first thing here that happens the first time and without the third thing here.

So say we wanted to do this with while loops, we'd have to declare int i = 0 up here. Then we do while (i < 5) and then right here we'd say i++ at the end of this code. And I'll do it: int j = 0; while (j < 10).

Okay, pretty cool! So right here is the equivalent for y in while loops. It's a lot more code because the way it works is we declare i up here, then we do a while i < 5, we're adding i down here instead of right here, and it goes on.

I forgot j++; So now I'm just going to comment out this for loop code and demonstrate the while loop version. I forgot to print a new line here, so now I'll demonstrate the while loop frame, and here it is.

Pretty similar; it's exactly the same actually, but I like the for loop code better. So we're going to continue using for loops in this project. But anyway, that is how to do loops.

So I hope you learned something about these loops today. I will try to put this project in the description of the video, or at least the link to download the project.

Anyway, thanks for watching! Mackielaw, subscribe, and goodbye!

More Articles

View All
Representing points in 3d | Multivariable calculus | Khan Academy
So, a lot of the ways that we represent multivariable functions assume that you’re fluent with understanding how to represent points in three dimensions and also how to represent vectors in three dimensions. So, I thought I’d make a little video here to …
'Hey Bill Nye, If There Is a God, Should We Obey It?' #TuesdaysWithBill | Big Think
Hi Bill. My name’s Cameron and I am a junior at Libertyville High School in Washington. And I was just wondering, on this hypothetical question, if there was a completely intelligent all-knowing entity, whether it was human or otherwise, that if it existe…
Cattoos: How This Tattoo Artist Helps to Immortalize Beloved Pets | Short Film Showcase
[Music] My first cat tattoo was on my 18th birthday. Then came Patches, and then came a lot. [Music] More like a lot of teenagers, I suffered from depression. The best part about being a teenager, uh, was Patches because she was always there. I would wal…
Will future robots & AI take over? | How Sci-Fi Inspired Science
Let’s face it, one of the worst things about adulting is having to clean. If we can get out of it in any way, we’ll do it. And since machines are made to make our lives easier, it makes sense we want a machine made to clean. But in sci-fi, we want to go o…
Inside the Amazon: A Photographer’s Story | Nat Geo Live
(Instrumental music) One of the things I like to do when I’m in the Amazon is just bust all the myths. I don’t want all this nonsense, this romantic image we have of indigenous people. They’re just people. You know, the reason I like these pictures is be…
SSH
Hey guys, this is Mids. And1, um, today this video is going to be on SSH. Now, someone asked me what SSH was, and I decided I’d explain it. Most people use SSH as a tool they call hacking, where basically they can connect to your computer, and with the p…