Hello, welcome back guys. Today we're going to discuss about loops. loops as the mean by the term. We want to do some actions over few iterations. loops help us in that. So let's say you want to iterate over an array and want to perform an action on every index or something.
Or maybe you have a list of items you want to do some action. There loops are very handy. Even if you have a condition, you want to do something until the condition is true. loops are very handy. Let me create a class and I'll explain you more new class similar thing we've been doing loops today will do while while and do while loops. And in next tutorial will cover the for loop.
So just to Make you guys understand with some real world example, let's say you guys have a bank account and you have hundred dollars in it. You can only withdraw until you your balance is greater than zero, right? So you can write a loop something like okay withdraw money from my account until it's greater than zero. If it's zero, you cannot really withdrawing right? There is a loop. So, pretty much goes like that.
And we want to do something until some condition is true. If that's false, then we would want to get out of the loop. So let's take an example. Let's say I declare a variable i with value 10. And I create a while loop. So very easy to understand while my Boolean condition is true.
I'll execute this code block this Boolean condition Understand is nothing I haven't declared any variable. So it's given me an error. But until it's true, I'll execute some code. What is a Boolean condition we already discussed in tutorial, the conditional statements tutorial, I can explain you guys again. So like Boolean, let's say condition is equals to 10 less than 20, which is always true. Let me commend this code for raw for now guys.
And I'm impressed print, see, so and my condition. So this has got a brain the condition. Since 10 is always less than 20. It's printing out to true, let's say by 10 is greater than 20 in the condition variable and I print out the condition variable it should print out false That's what it's been doing. So this is a Boolean condition, right? I can use the similar thing in my by loops.
And until my condition is true, so what I'm doing in Mac is Ctrl command and covert slash to command out and backward slash to uncomment. The code block. There's a shortcut. Very handy shortcut. So let's say I put a condition until my eye is less than 20. It's 10.
Less than 20. I want to print out the value of AI. All right, now guess guys what's gonna happen now it's gonna fall into an infinite loop. Because 10 is always less than 20. It's gonna print out I endless number of times, and the program is continuously running. I have to terminate this program using this terminate button.
What I need to do is to make this program a little more sense. If I do i plus plus, if you guys are wondering what this is, let me first explain what it does it is it is actually equal to i equals two i plus one, what I'm doing is I'm adding one to AI and again putting into the I variable. So it's incrementing I every time let me run this code right now. So it prints out from 10 to 19. Not 20, because i is less than 20 not equal to 20. So let me show show you the shortcut.
So if I do i plus plus, it does the same thing. 10 to 19. This is a while loop. So while my is less than 20. I want to perform this action. You can put any code block inside this according to the need of the program and the application.
This is a simple while loop. There are a couple of handy, like very important things in loops. One thing is breaking one thing is continue to break we already discussed in switch statement at oriel. But I'll show you again, that thing. So what break means is it's used to break out of something. So when I'm using break in the while loop, it's gonna kick the program out of that while loop as soon as it's the break.
And it's gonna again continue executing the program after that particular code blocks after that. So let's say I'm gonna print something here. out, outside loop. See right now this is perfectly working. 1011 nine, all right, sorry, 10 to nine and outside. Oh, let's see if I do it.
The value let's say if i is equals to equals to 70. Let's say I don't want to pray and I, if it's 70, and I just want to break out of the loop and do something else. And I say, Okay, if i is equals three, we'll just have it equal to 70. Now to break out of the loop, so what it does it it prints from 10 to 16. As soon as it encounters 17, it just breaks out of the loop and prints the the actions after the while loop. So right here, the while loop ends.
So it just goes and execute any statement of code which is after the while loop. By break statements are sometimes very handy when we encounter something we don't want to exclude anything further and get out of the program. Get out of that at least while loop not that complete program by loop is very handy. Now what is the difference between break and continue there is a continue keyword also What Cardinal causes is it causes the loop to immediately jump out of the loop. But again go to the next iteration. So continue will not go to this line right away, it will stop that iteration, the iteration of it goes to 17 and will not perform any code after continuous statement and will again jump to this statement by loop and will again start the loop the code block inside the loop.
So, I do continue right now, it's gonna fall into an infinite loop. I'll explain you guys let me show you when I'm doing this. It's actually you are not seeing it that infinite loop but what's happening is it just printed out till 16. Right? What happened? When it's 17 it had the continue it didn't print this, it went there.
Then what happened? Why didn't print 18 and why? So you are you guys Not seeing but the crew program is still learning. See this red button is still enabled, and it says terminate. So it got stuck here. Because when I was 17, it hit the continue.
And we never incremented the AI. So right away from this point, it went to the while statement, the first first line here and started executing, I still 17. It's right away. It's just moving into these three lines of code. So we have to stop this. And what we need to do is we need to put these two lines of code ahead of it goes to 17 and then execute that program.
Let's see what happens now. This time, it prints out the value of i increment c++. It's equals to 17. No, ma'am, it just goes to the loop. If it's 17 what happened this time is it printed out every single thing. So what is the use of this, maybe let me show you this thing, maybe print this again.
Value of i. So, maybe I'm printing it twice, just to show you guys that it's not gonna execute this statement at this step 17. So you guys must be seeing 10 value of i is 11 because it's incremented after that, so maybe I should do i minus one and save it. So it's sitting at that point. So while you have eyes this so now you guys must be seen after after 16 So it didn't print the value of is 17 so it incremented here, right? It must be a little bit confusing for you guys.
So see it's showing one plus because 10 and it's showing value is 11 because it has incremented So right now the value of i is 11 it's printing collecting value of i is 11. That is true. So when it had the 11 then it has printed out 12. So let me just and let me come in this thing. So mad must be easy for you guys. So right now while you have eyes 1112 1314 1516 and it skipped the 17 right.
So as soon as I had it, encounters 17 it just continued the code block and again went to the first line and then print the 17 so this is the use of continue keyword guys. Other than while there is a similar loops thing, which is a doozy While the only differences in do while loops it checks for the Boolean expression after executing the first code. So, in my loop we are first taken the Boolean expression if it's true, we are getting inside the loop. What happens in do wireless and below me comment this code out and start with do while what happens and do wireless do and I'm putting a while here with the Boolean i is less than 20. I'm going to put the similar line here. See, so I an increment i.
So it goes to an SME column. Yeah semicolon. Yeah. So all this while code is commander right now, if you guys notice this, it started printing out from 10 to 19. To is 10 it printed out, it takes the value, okay is Tony, when I until i is less than 20. it printed out everything. The owner references let's say I is the condition I gave is is less than 10.
And okay, let me first run it guys, I'll show you what happens. So if I do this condition in the while loop if is less than 10. This will never execute the code because i is equals to 10. And it's not less than 10. What happens in do while loop is the first first iteration is always done. It's going to do this and then take a while if this condition is met Then it's gonna go to that do block again.
So even at the first step, this condition was not true. It did execute these lines. So let me show you exact thing here. So if this is the case and I gave the value here as 10 it didn't execute this thing, right? It didn't print out anything. It just died right away printed out this thing and printed out this thing.
So let me let me print this to make it easy for you guys. So this is dou y. No. So you guys know what 10 is printed inside the while loop. So from the while loop, nothing is printed. If it's less than 11 then it printed out the value.
The value is 11 and outside low bid came and then went into Dubai low. So the only difference is in Dubai Look, the step is always executed and then the condition is set. I think this is a lot, a lot more material to absorb to guys just write the code yourself, try and practice more. And you should be comfortable in this it's not really tough. Next class we'll discuss about for loops, which are a little bit more with more options to tackle and more things we can perform in the follow ups. So see you next time guys.
Thanks