Java Tutorial for Beginners #11 - While Loops

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches the basics of while loops in Java, including their syntax and use cases. It also covers do-while loops and provides examples of how to use them.

Key Takeaways
  1. Declare a variable to store the loop condition
  2. Use the while keyword to start the loop
  3. Specify the condition for the loop to continue
  4. Write the code to be executed inside the loop
  5. Use a do-while loop as an alternative to a traditional while loop
💡 While loops and do-while loops can be used interchangeably, but while loops are more suitable when the number of iterations is unknown.

Related Reads

📰
The End of YouTube Search? Why AI Creator Discovery Is Becoming the Smarter Way to Learn in 2026
AI creator discovery is becoming a smarter way to learn, shifting focus from video content to creator expertise
Medium · AI
📰
Why AI Tools Are Becoming Essential for Modern Professionals
Learn how AI tools are revolutionizing everyday work for modern professionals, increasing productivity and efficiency
Medium · AI
📰
The Food Stayed Real. The World Around It Changed.
Learn how AI transformed real breakfast photographs into various art forms without altering the food itself
Medium · AI
📰
AI APIs in 2026: The Honest Developer's Guide to Choosing One
Learn how to choose the right AI API for your project by considering tradeoffs, not just picking the 'best' model
Dev.to · Shaw Sha
Up next
I Built a Live Dashboard With Claude - Zero Coding, Zero IT Skills
Nicolas Boucher
Watch →