Hello guys. Welcome to LearnVern. The last topic we saw was Conditional statement.
Today we will learn about Looping statement.
What is a Looping statement? Looping statement is a normal sequence which we keep on playing over & over.
Meaning until the condition keeps coming true, we will keep playing the statement.
Why? So that we don’t have to keep on calling the statement over & over again.
And the thing we want repeating can be kept under Looping.
There are 3 types of Looping - While Loop, For Loop and Nested For Loop.
While Loop, For Loop and Nested For Loop.
If we talk about While Loop first, While Loop is one type of Entry Control Loop.
What is it? One type of Entry Control Loop.
Why is it called an Entry Control Loop?
Because if your condition is true, then only the Loop will start working otherwise it won’t.
If your condition is false, it will directly exit the loop.
So this is why it is called Entry Control Loop.
Next is For Loop.
How does simple For Loop work?
Suppose you have a list & you want to call the value again & again.
That time you have to use a simple For Loop which will work according to the sequence.
In the same way, there is Nested For Loop.
Nested For Loop is just like the Nested If condition.
Meaning one For Loop works under another For Loop.
How does it work here, what are the examples, how do we have to work with examples…
Let’s see all about it through practicals.
Moving onto the practical part.
I’ve opened up my Jupyter.
First of all we will check our While Loop.
I'll increase its size a bit to make it clear.
We will check how the While Loop works first.
First of all, what do we have to do for While Loop?
Let’s take an example.
I want to print a series. 5, 9, 12, 14, 15..
I want to print a series under While Loop, okay? By using While Loop.
How will we do it? Let’s see.
First of all, I’ll take a variable of Sum whose initialised value I’ll take 0.
Why did I take 0? Because we have to store our particular series inside it.
We have to store under what? Under sum.
What will we do? I initialised sum a value of 0.
We will take an input of N.
We will take a decision variable for the rotation of the Loop.
How to take it? I took a variable N. I took user input.
Why user input? Because I have to make this a Dynamic Example.
I simply wrote here that I want N’s value.
Now here, my While Loop should work according to N.
So I wrote While N > 0. Means if the N is greater than 0 until the Loop roams, otherwise it shouldn’t roam.
How will you do it? I took N > 0.
Now I have to store the value under sum.
So I took sum = sum + N.
So the values which will go under sum will be the values derived after the addition to N.
Then I wrote N - = 1.
Meaning I’ll have to decrement the value of N otherwise the While Loop will keep rotating continuously.
It will turn into an Infinite While Loop. Infinite While Loop.
Infinite While Loop never stops but we don’t want any such Loop.
So I wrote N - = 1.
We had seen shorthand operators under Assignment Operators.
So I wrote N - = 1. And I simply printed Sum’s value.
At last I wrote Bye to make us know that our program is completed.
Let’s try it once.
Suppose I enter 5 in N. And run it. Which shows my While Loop rotating.
5, 9, 12, 14, 15. It is working this way.
As soon as the N’s value was going to be less than or equal to 0, it wrote Bye to make us exit the Loop.
Clear? So this is how While Loop works.
Now if you want to use Else with While Loop, how will you do it?
I’ll run it once again.
I’ll write a comment here to mark down While Loop With Else Part.
Now how does While Loop with Else Part work?
I’ll copy paste this code here.
It is possible only in Python that you can use Else with While Loop.
How will we do it?
You just have to write Else here & print Loop finished or Loop error.
You can get it printed here.
If there is a condition which is false, it will directly show us Loop error so the Loop is not working.
As soon as I run it, I'll write 5 here and it will show a Loop error.
Why? As it is N - = 1, it showed us false.
As it is false, it printed a Loop Error.
So this is how you can use the Else part in While Loop.
Understood? Hope it is clear to all of you.
Next up we will work with For Loop & see how it will work.
Firstly we will see Simple For Loop.
How does Simple For Loop work in Python?
Simple For Loop goes with its name that it is indeed very simple.
I’ll give you an example and quickly explain it to you.
It’s not difficult at all.
Suppose I have a word named variable, okay?
And I have a collection of strings such as Python, Java, Angular, yes I’m using some coding words...and Graphic.
So I’m taking a collection of 4 strings.
Now what will we do?
Now I want to bring out the strings one by one from the Word variable.
Meaning I want to iterate it.
How will we iterate it? For I in Word.
See there is a very simple Syntax in here.
For I, I is an iteration variable which will roam in Word, which will follow the Word sequence.
What do we have to print? We have to print I. Why I?
Because all the values of Words came under I.
And as soon as I run it, you can see all the values are seen here one by one.
I hope all of this is clear to you.
Now suppose I want to calculate the length of all the words which we brought out.
I want to calculate the length of all the words.
What will I do here? I’ll put a comma here, and decorate using an = here.
Write down the Len function, which is a predefined function in Python provided to calculate Length.
We want to find the length of words, meaning the collection of strings.
But our string is under I.
I’ll write I. And it calculated the full length.
Python is of 6 characters so it wrote 6.
Java has 4 characters so it wrote 4.
So this is how Simple For Loop works.
Did you understand whatever we worked on uptil now?
Moving onto Nested For Loop.
How does Nested For Loop work?
There’s a very simple way of Nested For Loop.
I’ll show you by creating a design here to give you a clear idea about how Nested For Loop works.
I have to create a design pattern this way by using star (asterisks) , 2 stars in 2nd row,
3 stars in 3rd row, 4 in 4th and 5 in 5th row.
I have to create such a pattern.
Now I have to use For Loop to make this.
But first of all, you need to use a new method called Range method to use For Loop. Which method? Range method.
There are 3 parameters in the Range method.
1st is the start point. 2nd is the end point & 3rd is step point which is also called jump.
This is the syntax of range method.
Now how to use the range method?
I’ll explain that to you as well.
We’ll have to roam a For Loop here “For I In Range”.
For roaming I in Range, first we have to see how I works.
I’ll show it perfectly in Paint how it exactly works.
I’ll draw a star pattern here. In a better way.
I’ll draw 3 stars here. 4 stars here and 5 stars here. Okay?
So this is the star pattern we have to draw now.
Now we have to print it using Nested For Loop.
How will we do it?
Here 2 For Loops will roam. First one will be the I which will work only for rows.
2nd will be J which will work only for columns.
Now how will you figure out rows & columns?
There’s a simple trick.
Divide this whole creative into a tabular form.
Like this. It will get divided perfectly & we will get rows & columns easily.
We still don’t know how to decide rows & columns.
We will decide this as I and this as J.
I’s flow would be like this and J will flow like this.
1, 2, 3, 4 & 5.
1st row, 2nd row, 3rd row, 4th row, 5th row.
In the same way, 1st column, 2nd column, 3rd column, 4th column & 5th column.
So I divide both in this way.
Now, creating Logic is the easiest of all for us.
Let’s see how.
Here is For I in Range. I have to see my starting point.
What is the starting of I here? 1. I’ll simply write 1 here.
What is our endpoint? It is 5. We have to write an increment of 6.
2nd For Loop works for J. J in Range.
What is J? J is column. We will see J too.
Starting point is 1. Ending point 5. It is the same as I.
I’ll write I only. Now I’ll print the stars.
And to make it look perfect, we’ll use space and for that, I’ll use the End function.
And wrote a print here to proceed to the next row.
To make it enter the next row. Alright.
Now I'll run it and you can see that the print came out perfectly.
Did you understand how it works?
Suppose now I want to reverse it.
How will we do it?
It is very simple as well.
I’ll copy it. Paste it here and I have to change only its starting & ending point.
I want 5 stars at the beginning & 1 at the end so I’ll put 0.
But remember we have to go in decrement like 5, 4, 3, 2, 1.
So we will have to inform it that we have to decrement in single digits.
That’s it. And it will be reverse.
So this is how a Looping statement works in Nested For Loop.
It is very simple & easy to use.
I hope you all understood today’s concept easily.
In the next video, we will see Loop Control Statement which is used to control Loop Statement.
Share a personalized message with your friends.