Learn C++ With Me #11 - While, Do While Loops & Break/Continue

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

Key Takeaways

This video covers the basics of while loops and do-while loops in C++, including their syntax, usage, and differences. It also discusses the break and continue keywords and how they can be used to control the flow of loops.

Full Transcript

hello everybody and welcome to another youtube video so in today's video i'm going to be covering while loops now while loops are more general type of loop in c plus plus in just any programming language in general anything you can do with the for loop you can actually replicate with a while loop but a wallop is more powerful can do other things as well so with that said let me talk to you about while loops in this video so before we dive in i'm just going to quickly mention why i haven't covered something called for each loops if you don't know what a 4-h loop is don't worry about it uh it's just something that allows you to iterate by item in an array or some data structure that's iterable again don't worry about that if you don't know what that means the reason i haven't covered that yet is because it uses some more advanced syntax and symbols that we haven't yet seen and so in the interest of not confusing anyone and actually being able to explain the syntax fully i'm going to show that in some later videos anyways in this video we're talking about while loops so let's compare a while loop to a for loop and let's do a quick kind of refresher on for loops so i'm going to type out a for loop here i'm going to say while int i is equal to zero i is less than 10 and then i plus plus and then i'm going to c out and we'll just see out i and then end l all right so this is a basic for loop we're going to print the numbers from 0 to 9. we're not going to do 10 because it's i is less than 10 and well that is the fourth we defined our iterator variable we defined our condition this loop runs while this condition is true and then we do something to the variable at the end of the loop so a while loop is actually very similar to a for loop except we're not constrained to the fact of having to use some iterator variable so here we have to decide define some variable right and this variable is what tells us how many times we're looping and if you remember back to the previous video i said you would use a for loop when you know how many times you want to loop so that's great but here we have to know how many times we want to loop like we have to loop 10 times 20 times 30 times some number of times we have to define that and even if we're looping through an array we're looping through the number of elements in the array right or some variation of that so now we talk about a well now a wallop is the exact same as this except we don't have this iterator variable that we need to define so what you say when you define a while loop is while you do your open and clothing parentheses and then you have your curly braces like this and while this condition is true whatever is inside of these parentheses right here this loop is going to run so a little bit different than the for loop and then in that you don't have to define these two things right here but you do need some condition now this condition can literally be whatever you want like in this case i'm using true that means i'm going to have an infinite loop here because this while loop will just continually run forever this condition true is always going to be true but if i were to put say false here then well this is going to never run right because this condition is false and it's never going to change to true so the idea behind this while loop is that we don't need to use a condition that depends on some iterator variable we can use some condition that kind of says we don't know how many times we're going to be running now to give you an idea of what i mean by that when you would use a while loop a really classic example is when you're asking user for valid input so you want the user to type a number maybe you want them to type either the number one or the number two those are the only two valid options for whatever program you're writing well you want them to keep or you want them to type in the number one or two like they have to type in that number for your program to continue so you're going to keep asking them to type in the number one or two until eventually they type it in you don't know how many times they're going to give you invalid input you don't know how many times they're not going to give you any input at all so you're going to use a while loop because well you don't know when that condition they typed one or two is going to be true so i'll show you what i mean but i want to show you how we can simulate a for loop with a while loop because again anything you can do with the for loop you can do with a while loop so let me define some iterator i'm going to say int i and then here uh sorry let's make this equal to zero and then as the condition i'm going to say i is less than 10 and then i'm going to say i plus plus and before i do the plus plus i'm just going to see out this exact same thing so let me just run this code here when i do this you're going to see that what we get is 0 to 9 and then we get 0 to 9. so the exact same thing happened from both of these statements right here this while loop does the exact same thing that this for loop does now hopefully you can see why i may maybe preferred to use the for loop this is just a bit of a cleaner syntax you don't have to write as many lines of code all of the stuff related to your iterator variables in one line this is kind of the preferred style you should really never write a while loop that's exactly like this it would make much more sense to write a for loop but the idea behind a wallop is it's much more general whereas the for loop is kind of a specific type of loop and just makes things a little bit cleaner when you need this type of functionality so now let's erase this for loop and let me show you a real practical use of a well so we want to get some valid input from the user so we're going to ask the user to type in some number we're going to store whatever number they type in and we're going to check if that number is equal to whatever options we have so in this case let's say one or two so let's do this i'm going to clear out the swallow and i'm just going to set the condition for my wallet to be equal to true to start i'm going to make a variable here i'm going to say int input we'll just leave it undefined for right now and then i'm gonna ask the user to type something in so i'm gonna say co i'm gonna say type the number one or two colon okay then i'm gonna say c in and i'm gonna see in to the variable input so what i'm gonna do now is i want to check if this input is equal to one or two if it's equal to one or two i can stop my loop because they've typed in valid input if it's not equal to one or two then we need to keep looping keep asking them to type in something valid now for this example here i'm going to kind of ignore the fact that the user could type in a string or something that's not a number we're just not going to deal with handling that right now we're going to assume they give us some number and we're just going to check if that number is equal to 1 or 2. so i'm going to say wow and in this case the input is not equal to 1 and the input is not equal to 2. so what we're pretty much saying here is while the input is not equal to 1 and not equal to two keep looping so if at any point in time they type in one or they type in two then we're gonna stop and just to show you a little trick here if you wanna see what needs to be true for the loop to stop so like the opposite of this like you wanna know the condition that has to be true for the loop to stop running then what you can do is apply a not to this condition so if i copy all this and i put this down here and apply a knot to it then what this gives me if i have the knot is i take the knot apply it to the left apply to the right and then swap the logical operator so that's going to give me input is equal to 1 or input is equal to 2 so this means if the input's equal to 1 or the input's equal to 2 this while loop stops and that's because when you apply the not to this condition well that's what it tells you right because if this is not true then that means the loop stops running so if this is true this condition right here the loop will stop right so hopefully that makes it clear but just a little trick in case you have difficulty reading some of these conditions but anyways that's what it means now as i said here when we start out input it doesn't really have a value so we actually don't know what the value of input will be so just to kind of safeguard us a little bit i'm going to set input equal to negative 1 to start and we're just going to keep asking the user to type something in so this is not necessarily the best practice to do this but for the purpose of this example i'm just going to do it this way anyways let me go to my terminal now the reason i have to go to my terminal and i can't run this in sublime text is because sublime text is not capable of actually capturing user input at least in c plus plus uh it will kind of work but then it will break halfway through anyways i have to go here and do it so i'm going to compile my program i'm going to say tutorial 11 and then ask me to type the number one or two so if i type three asked me to type another one type four asked me to keep typing i type one boom we're done run this again i type two we're done run this again i type a number another number type two we're done so you can see in every single one of these programs i typed a different number of numbers right like the loop ran a different number of times and that is where a while loop is helpful because we don't know at the beginning of the program how many times i'm going to make a mistake or not give something valid and so that's why we use a while loop so hopefully that kind of clears it up but now i'm going to show you another way that we can end a while loop or a for loop so the next two things i'm going to show you here are called the break and continue keyword very important i probably should have shown them in the previous video and they apply to both a while loop and a for loop so i'm going to change this now to be while true i'm going to show you how even though we have a true here we can actually end this while loop from inside of it this is kind of just a different pattern or paradigm of doing things sometimes there's like reasons why you would want to do this so it is very important that you understand the break and continue keyword anyways let me show it to you so i want to end this while now there's this keyword it's called break you can see how it's highlighting kind of here in red whenever your program hits a break keyword it's going to break out of the closest loop that that it's inside of what i mean by this is that when you see break it's going to look kind of outside of the break keyword and find whatever loop it's inside of the closest loop it finds because you can nest loops inside of each other it's going to break and by break just immediately end so anything after the break keyword say i see out like hi will not run because immediately when you hit this keyword as soon as the program sees this boom loop is done anything after is not going to continue running even if you're in a for loop the entire for loop is finished running doesn't matter what the i variable was at that is what the break keyword does so to use the break keyword well i only want to break when something happens right when a condition is true so i'm going to say if now and we're going to change this we're going to say if input is equal to 1 or input is equal to 2 then break so now notice how this condition here is different than the condition i put up here it's the negation of it right now if i apply a not to this that's going to give me the while of condition i should have so here we're typing in the condition that needs to occur for us to end the loop not the condition that needs to occur for us to continue running the loop so in a lot of cases it's much simpler to write this condition the one that just uses the or doesn't have the and and the not equal to than it is to write the other version right so that is sometimes why use a break keyword to simplify the expression or simplify the condition that you're using to kind of control the flow of the wallet hopefully that makes sense but that's one of the reasons why you'd use a break keyword because oftentimes it's easier just to break out when something happens than it is to write all of the things that need to be true to continue running the loop anyways let's just see if this works so let's go here let's run this or compile and then run i'm gonna type a number four three two boom we're done type a number one we're done so it is working again does the exact same thing as the previous loop instead now we are using brick so that's how the break keyword works now you can use multiple instances of the break keyword right maybe i have another condition if the input is equal to four then maybe i want to see out cool and then i want to break that's totally fine now i have two break conditions so if either of these things are true i want to break and really we can change this to an else if right uh and the reason we could change that to an elsif is because if this is true well we're just going to break so we can change it to an l-sit now it doesn't matter if it's an if or if it's elsif but we could change it just showing you that there so let's run this let's go tutorial 11. let's type a number five let's type four says cool and we end so you go this indeed works so that's the break keyword and just like you can use this in a for loop you can use this or sorry just like you can use this in a while loop you can use this in a for loop so we will continue in one second but i need to quickly thank the sponsor of this video and this series which is algo expert algo expert is the best platform to use to prepare for your software engineering coding interviews they have over 140 coding interview questions and more are constantly being added to the platform check out algo expert from the link in the description and use the code tech with tim for a discount on the platform so let me delete all this let me quickly show it to you in a for loop so i say int i equals 0 i less than 5 i plus plus i'll say if and let's just go i equals equals two then c out done and then break and we will just c out i and and l okay so let's just run this for loop and i'll just show you that this works i can run this in sublime text so notice that we break as soon as we get to 2 the reason we broke at 2 is because while this condition was true we saw the break keyword and we ended now it's important to note that again you're breaking out of the most recent loop so if i wrap a while loop so i say like while true and then i wrap the for loop inside of this well totally valid to do this by the way you can put a for loop in a while loop there's there's nothing wrong with that if i do this now when i hit this break keyword i'm just breaking out of this for loop and so what will continue to happen here is i will keep printing 0 1 2 0 1 2 0 1 2 because this for loop will keep running because we're in an infinite while loop and then what's going to happen is well it's going to run until it gets to 2 it's going to break at 2 and then continue running again continue running again so let's make this non-infinite loop let's say and j equals zero oops i keep typing nine uh we'll say while j is less than three and then at the bottom of the loop we'll say j plus plus and there we go so now let's run this and let's see what our output actually is so let me just move this up let's run we get zero one two done zero on two done zero one two right and there you go because we're printing done whenever we break this loop and well this for loop runs three times so hopefully that's kind of clear but that's how the break keyword works now we can talk about the continue keyword so the continue keyword is kind of a way of continuing looping but skipping the rest of the stuff for the current iteration so i'm going to clear this kind of wrapped stuff because it's a little bit hard to read i'm going to make another for loop so we're going to say 4 and i equals 0. we'll say i is less than 10 and we'll say i plus plus now for this right i want to say skip every even number or every odd number or maybe when it's a certain number i want to do something when it's another number i don't want to do something maybe for the number like 8 and 9 i don't want to print it out but all the other numbers i want to print it out now you could try to change this kind of iterator to do that for you but i'm going to show you how the continue keyword works regardless because there's a lot of use cases where you do want to use it so what i'm going to do now is i'm going to say if and here i'll say if i is equal to 9 or i is equal to 7 then continue now whenever you see the continue keyword what this will do is immediately bring you to the next iteration of the loop so as soon as you see this the loop doesn't necessarily end but it just goes to the next step so if we're at nine or sorry let's seven is a better example if we're at seven we're just immediately going to go to iteration eight and start the for loop from the very beginning that means anything underneath here so say c outing i is not going to happen so if this condition is true and we continue we don't print out the number so let me show you what i mean let's run this here you're gonna get zero one two three four five and notice we skip seven and we skip nine that is because we have the continue so as soon as you hit the continue you skip anything afterwards hopefully that's clear but very useful because a lot of times when you're looping you want to check some type of condition or do something but if that is true maybe there's a bunch of operations you can skip and so rather than say wrapping the opposite condition which i'm going to show you one second you can use this continue keyword so if you didn't have this continue keyword and you wanted to do what i just showed you you would have to do this in kind of a very backwards way you'd have to say if i equals equals 9 or i equals equals 7 then you could like do nothing inside of here and then you could write an else statement here and then in the else you could print this out so only if i is not equal to 9 or not equal to 7 are you doing this right that would work but that just doesn't really make much sense so what a lot of people would do is they would say okay well instead of doing that i'm going to remove this continue because imagine we don't have this continue keyword i'm going to put this inside of here i'm going to change this now to say if i is not equal to 9 and i is not equal to 7 right so now what you're saying is okay only if i is not equal to 9 or not equal to 7 am i doing this but again this is kind of like a backwards way of doing this because really you should do it in the opposite order you should have your condition be simpler so you say if i is equal to 9 or i is equal to 7 we're just going to continue the loop as opposed to here while where you're checking every single time if i is equal to 9 where i is equal to seven and only if it's not equal to those two values are you printing these f so you can make an argument for why one or the other is better let's just run this and make sure it does indeed work but continue is very useful and you'll see why it becomes useful because now if i wanted to do like a ton of different things right say i wanted to do this like i wanted to print this out a bunch of times now again we would just use a loop to do this but imagine we had like you know 200 lines of code or something inside of this for loop i don't really want to have all of these lines of code indented as soon as i add layers of indentation my code just becomes a little bit more difficult to read it's not kind of clean and nice and so all of the stuff that i want to happen i'm putting inside of this if statement when really it would be better if i could flip this around so i could put all of my code not indented although i am going to have to unindent this so if i can move it all unindented and then i can change this here so the only thing that's indented is that continue right that's just like a little bit cleaner and a better style when it comes to coding now i'm getting a bit of my a bit ahead of myself here because you know we don't really care about industry standards and that kind of stuff when we're learning a programming language but just something to keep in mind when this continue word will actually be useful because a lot of people i think see these types of syntax or words and are like well why are you showing this to me i'm like never going to use that okay so sorry for the abrupt cut here but now i want to talk to you about something called the do while loop now the do while loop allows you to make sure that whatever's in your while loop runs at least one time to show you why this is useful let's consider the following example let's say i have some variable i'm going to say int x is equal to 10. okay so into x is equal to 10 and then what i want to do is i'm going to say while x is less than 10 i want the user to type in a number so the idea behind my program here is i'm going to ask the user to add some numbers to x and while the value of x is less than what it started at so it's less than 10 i want them to keep adding numbers essentially i want them to make x greater than 10 right however we're sorry greater than or equal to 10. so they need to type in anything that's zero or above however if i run this while loop right now like let's put something inside of here this isn't going to run the reason it won't run is because well 10 is not less than 10. and so the initial condition is false which means i'm never going to enter this wallet anyways let's just say int add uh like that and then we can say c out we'll say type a number and then to add to x colon okay and then we'll say c in and we will see in to add and then we will say x plus equals add and then we will just c out x is colon x and then and l okay so i know i went kind of fast there but the idea is we're asking user type a number they type in a number we store it in add we then add whatever number they typed into x we print out what x is if x is less than 10 we continue running so we want this to run at least one time because what might happen right is if the user types in like negative one then that means that we're going to keep running this while loop so after the initial entry they may be in this while loop many times but they need to enter the while so to do that we need to make sure we can enter inside of here because if i run this go to my terminal and g plus plus and then run tutorial 11 nothing happens one way that we can make this run is we could just copy all this code right so we can copy all of this we could put this here and then let me just erase the initialization of add just because i can't re-initialize a variable once i've initialized it and now this will actually run what i've done is i've taken all the code copied outside of here so this will happen one time we'll then check if x is less than 10 if it is we'll continue repeating these steps right here so let's try this let's compile let's run it says type a number let's type like negative two and then it's going to keep asking us to type numbers so if i type four boom we're done however this is not very good this is not good practice i don't want to copy all of this code and what if the code inside of here was again like 10 000 lines or something i'm exaggerating but what if it was very large i don't want to copy all of it beforehand so this is where the do while loop comes in so keep in mind this is what we had to do previously now if we use the do while loop what we can do is ensure that our loop will run at least one time so i'm going to copy all of this i'm going to put it inside of the do so let's indent all of this and then i'm going to put my condition i'm going to say while x is less than 10. so this code right here is equivalent to all of this minus the index equals 10 that we wrote above and what this does is it says all right we're going to do this no matter what we do this at least one time then after we do this we check if we should do it again while this thing is true do all of this stuff inside of here but it ensures that we do it at least once and intuitively it kind of reads that way right because we do it then we check then we do it then we check whereas up here we're checking then we're doing we're checking that we're doing so it's kind of in that opposite order which allows us to have this happen immediately at least one time so let's erase all of this and let's now see if this works so let's go to our terminal let's compile and what is the problem oh i need a semicolon sorry just add a semicolon to the end of the wall there let's compile let's run and now it asks us to type a number so if i type number like negative six it's gonna say x is four negative two x is two uh that's six and then eleven and we stop running so that is the do while loop really straightforward you just put the do whatever you want to happen and then you add the condition afterwards after the while statement so with that said i think i'm going to end the video here i hope you guys found this helpful if you did make sure to leave a like subscribe i will see you in another youtube video

Original Description

Welcome back to today's video where I'll be talking about while loops. While loops are a more general type of loop in C++ (and other languages) than for loops. Anything you can do with a for loop you can replicate with a while loop, but while loops are a more powerful choice. 💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim 🔍 Playlist: https://youtube.com/playlist?list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3 ⭐️ Timestamps ⭐️ 00:00 | Introduction 00:34 | For Each Loop Info 01:01 | While Loops vs For Loops 02:14 | While Loop Syntax 05:08 | While Loop Examples 09:22 | Break Keyword 14:42 | Continue Keyword 19:19 | Do While Loop ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 💰 Courses & Merch 💰 💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 🔗 Social Medias 🔗 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear 🎬 🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9 🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV 📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r 🕹 Tripod: https://amzn.to/3hpSprv 🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc 🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl 🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z ☀️ Lights: https://amzn.to/2ApeiXr ⌨ Keyboard (Daskeyboard 4Q): https://amzn.to/2YpN5vm 🖱 Mouse (Logitech MX Master): https://amzn.to/2HsmRDN 📸 Webcam (Logite
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 and do-while loops in C++, including their syntax, usage, and differences. It also covers the break and continue keywords and how they can be used to control the flow of loops. By the end of this video, you will be able to write effective loop conditions and use do-while loops for repetitive tasks.

Key Takeaways
  1. Define a while loop with a condition
  2. Use a while loop to ask the user for valid input
  3. Use the break keyword to end a loop immediately
  4. Use the continue keyword to skip to the next iteration
  5. Use a do-while loop to ensure initial execution regardless of condition
💡 The break and continue keywords can be used to control the flow of loops, allowing for more flexibility and precision in programming.

Related Reads

Chapters (8)

| Introduction
0:34 | For Each Loop Info
1:01 | While Loops vs For Loops
2:14 | While Loop Syntax
5:08 | While Loop Examples
9:22 | Break Keyword
14:42 | Continue Keyword
19:19 | Do While Loop
Up next
The Official Shopify Sidekick Tutorial (Part 2)
Learn With Shopify
Watch →