Java Tutorial for Beginners #11 - While Loops
Key Takeaways
This Java tutorial covers while loops, including their syntax, use cases, and differences from for loops. It also introduces do-while loops as an alternative to traditional while loops.
Full Transcript
Hello and welcome back. So in this video we are going to be talking about while loops. Now while loops are very similar to for loops. Anything you do with the for loop you can indeed do with the while loop. That being said they do have different use cases in terms of where they're most useful. But just remember anything you do with the for loop you can do with the while loop. Anything you do with the while loop you can do with the for loop. They're completely interchangeable. And if you only had one of them in programming you'd still be able to do everything. it just might be uh not as I don't know nice to code if that's the right word for it or elegant is probably the better word. So a while loop is going to work when you typically don't know how many times you want to loop through something. So it's kind of like a question. Um like you know it's going to be based on this condition but you're not really sure like if a for loop is more like you know you're going to do it a set amount of times. A while loop is it could change based on the program based on user input. Okay, something like that. So, what we're going to do here with this while loop is I'm just going to set one up and then we'll just talk about exactly what it does. So, in this case, uh what I want my while loop to do is I want it to just continually keep asking the user for input until they give me like a certain word or like one or of two words. Okay. So, in this case, what I'm going to do, I'm just going to say uh let's see here. Maybe just get an int faster. Say int x= sc.next int like this. Okay. And what I'm going to do is I'm just going to keep looping through until they type the number 10. Okay? So, while I can do this is you just type the keyword while in here and then in these brackets is going to be your condition. Now, this is why I focus so much about conditions in the uh first two videos cuz they go a lot with if loops, while loops, for loops. So, whenever while this condition is true, then we're going to run the loop. So in this case I'm going to say wow x does not equal 10. Okay. Um yeah exact. Okay that's true. So in if they type in 10 then it will not continue to do this. Otherwise we will continue to do this. So that means I'm also going to have to get the int here because every time that we run this loop we want to continue getting x. So in this case we already have x to find. We just do x. And what I'm going to do is if they don't type in 10, I'm simply going to tell them like type in 10. So we'll just say dot print ln. In this case, I will just say type 10 dot dot dot. And you know what? We're also just going to print out uh cuz I like to do this. We'll just do system.out uh print ln. And in this case, we'll say type a number. So the user knows what we're looking for. Okay. And that means we're also going to have to print this here. And it should just be print, not print ln. My apologies. Okay. So again, the way this is going to work is we're just going to continually keep looping through this until eventually the user types in 10. In that case, we will break out. Okay. And we will not loop through this anymore. So let's run this. Type a number. Let's type four. Says type 10 dot dot dot. Type a number. Type five. Doesn't work. Type 10. There we go. and we broke out of the loop and it no longer asked us for that number. Now these are actually really simple. Um it just while this condition is true do everything that's in here and that's all you really have to know about while loops. Now again like we can what I was saying we can do everything that uh with for loops with while loops because we can also count in while loops as well. So, just like I did before, I could do something like int count equals zero. And then every time we run this loop, we could just say count++. And then maybe at the end of our loop, we wanted to do like tell them how many times they messed up. Okay, what am I saying? Print system.out print ln. And in this case, we'll just say you tried tired. You tried and then plus count plus times. Okay. Make sure we added a little space here. All right. So now if we run this and we'll say like 1 2 3 and 10. You tried three times until like eventually you got it. Okay. So that's like a really simple way to do stuff with while loops. Now you guys might notice that this code here like this um these lines are repeated up here. So I have this int x and this type of number. So how can I do this without repeating those lines? because I don't want to have to type like what if I wanted to ask a ton of different questions and then do the while loop. Well, I don't want to have the same section of code appearing multiple times. So, there's actually something called a dowh loop. So, what I'm going to do is I'm just going to erase all this and I'm going to set up this dow while loop. And it's pretty much what it says. We're going to do everything while this is true. And you you'll see how it works in just a second. So, I'm going to type the keyword do brackets. And now everything in this brackets is what is going to happen. Uh while the condition that I put down here is true. So the condition in here is going to be the same. While x does not equal 10, we're going to do everything in here. So now you can see this is working fine. There's no issues. And what I'm going to simply do is I'm going to take this uh these two lines here. I don't need that scanner cuz we're not going to redefine that every time. And I'm going to paste them in here. And what this is going to allow us to do now is x does not equal zero. Create local gosh. Okay. Uh let's just do this int x and then x. Okay. Perfect. There we go. So sorry that was just a quick issue. But what this is going to allow us to do is we're automatically going to do this once. No matter what this is going to happen once because we're going to do this and at this point x is not equal to 10. So we're automatically going to do this once which means we don't have to have it up top. and then down below. So we do that and then we check the condition. If it's true, we'll do it again or uh yeah and if it's not then we will break. So we can do this. We'll say type a number. Let's do four. Let's do five. Let's do0. Is that a number? Yeah. Okay. Uh and 10. And there we go. We break out of the loop. And everything is working fine. And that is pretty much it for while loops. Again, if you wanted to set one up to look like a for loop, then you'd literally just have to create a variable. In this case, say like int x equals z. You can set up a while loop. So you say while and you say x is less than or equal to 10. And then in here you just say x plus= 1 or plus= 2 or whatever value you want to add to it. And then you can just do what you would do in the for loop underneath your increment. Or you could put the increment. Typically you'd put it at the end. So at the end of the loop you're adding one and then you're checking. Okay. But that's pretty much been it for while loops. They're very simple. Uh, in the next video, what are we going to go over? Let me check my sheet here. We're going to go over sets and collections. Like, we're going to talk about like hashts and stuff. Um, getting a bit more advanced and moving away from some of like the basic stuff in Java. That being said, if you guys enjoyed the video, please make sure you leave a like and subscribe and I will see you again in the next one. [Music]
Original Description
In this java tutorial I will be talking about while loops. While loops in java allow for us to do something while a certain condition is true. They are extremely useful for when you are not sure how many times you will be looping.
Java beginners programming tutorial. A complete java tutorial meant for absolute beginners. Absolutely no programming experience required. If you are new to programming and want to get started you are in the right place!
**************************************************************
One-Time Donations: https://goo.gl/pbCE9J
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Tech With Tim Java
- Java tutorials
- Java for beginners
- Java Tutorial for Beginners
- Beginner Java Tutorial
- Java while loop tutorial
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related Reads
📰
📰
📰
📰
The End of YouTube Search? Why AI Creator Discovery Is Becoming the Smarter Way to Learn in 2026
Medium · AI
Why AI Tools Are Becoming Essential for Modern Professionals
Medium · AI
The Food Stayed Real. The World Around It Changed.
Medium · AI
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Dev.to · Shaw Sha
🎓
Tutor Explanation
DeepCamp AI