Hello, everybody.
Welcome back to the courts.
In the last lesson, we covered basics of flowchart and solved a few problems.
Today, we will solve a few different types of problem.
And with the help of those problem, we will learn a new concept.
So let us start today's class.
So the problem statement is so the problem with treatment is you have to read to no E and the.
And you have to print it as D.
And you have to print five.
DOMS.
Of deform.
A plus B, A plus duty, A plus to be A plus 40 and A plus 50.
So let us first solve this problem in the most easiest and the simplest way.
I will start my program and then I will read.
Do no read.
E.
andI.
And then I will print.
A place de.
Now, I will print a plus 2D.
Now, I will print a plus 30.
Now I will print a place for me.
And finally, I will bring to my last home, which is a plus five.
No way work is over.
I will.
Anyway.
Program, so let us revise again what we have done, so the Brownlow's treatment was you have to read
numbers A and B and you have to print five times of deform A plus D plus two, DTD four and five.
So what I did, we start our program.
Then we do numbers A&E and we have printed the five times and then we have another program.
So I hope by this time you might have noticed something strange with this approach.
And if you didn't, then I have a question for you.
So my question is very simple.
What do you think of what might be the problem with this approach?
I'm telling you this a problem with this approach.
And you have to tell me what is the problem?
Take your time and try to think.
OK, let me give you a hint.
In this case, I am printing just only five of them.
So this approach is working fine.
No problem at all.
But what if we have to print one million item?
Will this approach work?
Would you use this approach to print one million item?
Obviously, no.
So I hope you got the idea.
Let's come back to the question that I asked you.
What is the problem with this approach?
Yes, you got it right.
This approach is very time consuming and inefficient.
But why this approach is time consuming and inefficient?
The answer is very simple.
We are doing the same work again and again.
Basically, repetition is dear.
You can see the repetition here.
We are printing again and again and again.
This is just repetition of the work.
If we look carefully, I can express any of their time as E-Plus idee.
If the value of AI is one, if I equals when my time is a plus D, if I equals to my thumb is a plus
Dody.
And similarly, if the value of AI equals five.
My time is a plus five.
So basically we have to print a plus idee and the value of I will vary from one to five.
So there are two ways to change the values of AI.
So I told you there are two ways to change the values of AI.
So the first way is that we ourself manually change the value of AI.
But the problem with this approach is what would you do if you want to print one million item?
So this approach will fail.
The second key is that we will write some logic in such a way that the value of I will change automatically.
Our logic will look something like this.
Initially, my eye will be one.
Then I will check is a list tonight.
Close to five.
Yes.
One is less than I.
Close to five.
The condition is true.
If the condition is true, what will I do?
I will print E-Plus idee.
So in this case, E-Plus Duvel reprinted and after printing Ableist Idee.
What will I do?
I will increase the value.
Off I buy when?
So the new really, if I becomes to, then I will check the condition again is to let the necklace two.
Five.
Yes, 20 plus 80.
OK.
So E-Plus truly gets printed.
Then everything is the value if I buy one.
So the new value if I is three then I will check my condition again.
Estela's then goes to five years.
Ben.
Now the new value if I is for is for less than it goes to five.
Yes.
Ben diatom.
Increase the value of I buy went to the new value of I is five.
Check the condition Fifers Latona goes to five.
Yes Pendelton E-Plus finally incremental value if I buy one to the new value if I is six is six less
done it goes to five.
No.
So the condition is false.
So what will I do?
I will come out of the loop.
Or who can say exit from the loop?
Or you can see break the loop.
So to implement the logic, the thing we need is called a loop, or you can see a cycle.
So basically, there are three parts of a loop.
So the first part is initialise the second Pardo's condition and the third parties update.
So in the above case.
I equals one.
This is known as nasalization.
This is the first part of a loop.
Second part is to check the condition.
So this is condition.
And the third parties update.
So this is update.
We are updating the value of Ibai.
When?
So let us see, how does a loop look?
How does.
I loop.
Look.
So the first part is initialise.
So I really wish lay something.
The second part is to check the condition.
Here we have it condition.
If the condition is true, if the condition is true, then I will do some book.
I will do some work, and after doing the book, I will update.
After updating, I will check my condition again, if the condition is Viento, I will again do some
work and then update that will check the condition again.
If the condition is again through, then I will do some work again.
Then I will update and then I will check the condition if the condition is false.
Then I will exit or you can see.
I will break my loop.
Okay, great.
Let us draw the flowchart for this problem using loops.
So I will start my program.
Then I will read two numbers, A and B.
Now, the first part of Loop is to initialize.
So I'm taking a variable.
Let the name of the variable is a.
So the variable AI is initialized to one.
Now the next part of the loop is to check the condition.
Sam checking is a list.
Noriko's to five.
So, yes, one is less than I did close to five.
What will I do?
The condition is true.
Let's say I'm taking another variable.
Dum dum equals a plus.
I in two days.
So the value of AI is one and time equals A plus D.
Now what will I do?
I will print them.
So plus D will be printed after branding the them after doing the work.
We will update.
So I equals a plus one.
And then I will check my condition again.
So I equals I plus one means incremented the value if I buy one.
Or you can see I equals I plus one means calculate this part and copy this part into this part.
So first we really will read outages and will add the value of outages will go into Elitch's.
So simply the value of I will increase by one.
So this is the only way if I.
And this will be new value.
So a new value if I is two is to less than close to five.
Yes.
Through time equals plus truly printed them a plus truly incremental value if I went to the new value
of a three.
So it's three less than it goes to five.
Yes.
True democracy two duplicity gets printed.
Similarly a plus for they will get printed as similarly a plus five will get printed.
So the new elif I will become six is six less than it goes to five.
So the condition is no.
False, you can see false.
So if the condition is false and I have rendered all their thumbs.
My work is over now and I will end my program.
And here you can see the loop on against this cycle.
So this approach is way better than our previous approach.
Why?
Because if you want to print one million items, I only need to change just one thing.
That is the condition.
So what will I do?
I will write her one million and one million items will be printed.
So this approach is very efficient.
Okay, great.
So today we learned a new concept called Loop with the help of one problem.
Do not worry if we are facing difficulty in understanding loops.
This is completely normal.
Initially, everyone faces difficulty in understanding loops and in fact, you should consider it in
a positive way.
It is a positive sign that you are learning new things which are challenging and difficult to understand.
As far as I remember when I was learning about lubes.
It took me nearly two days in understanding how I look and why we need loop.
So if you understood just 30 percent of this lesson, you are way ahead of me.
In the next class, we will be solving many questions based on loops because loops are very, very important
and we will be using loops throughout this course.
And I want to make sure that working off loops is crystal clear in your mind.
So if you have any doubt, then first voice the next redo.
And I am pretty sure that all of your doubts will be resolved.
And if you still have doubts.
Feel free to reach me.
I will be more than happy to answer.
So I hope you enjoyed Redistributor.
I will see you in the next room today.
Till then, enjoy C++ and thanks for watching.