Hello, everyone.
Today, we will continue our best scripting videos with while use a while loop in any programming language
is to repeat the same line or a set of lines over and over until you meet a certain condition.
Let's take an example.
In this script, we're creating a variable eye and assigning it a value of zero.
Then we build a while loop by checking if I is less than or equal 10.
If it's not, then echo this line.
But it's very important to keep in mind that if we close our script like this, it will run forever
because I will be zero forever, right?
So basically I will be less than 10 and it will never become 11 for the loop to stop.
So let's change the value of AI inside our.
Noice.
We can also have an infinite while loop without any condition to stop it, but then we have to specify
our own condition inside the loop in order to stop it.
Let's take an example.
In this example, we're creating an infinite while loop just by adding colon after the word while here.
Then we will keep reading a number from the user and tell the number we're reading is bigger than hundred.
If that's the case, then we break our law by using the word break.
Let's try it out in action.
Nice, as you noticed, our look only stopped when we entered hundred and one, which is greater than
a hundred.
A very useful example of using one is to read data from a file.
Let's take a look.
First, we have a file here called Passwords.
This file has two columns separated by space.
We kind of did this before using OK, remember, let's do that now and said our best fit
in this group we're using while loop to keep reading data and stored in a variable called line.
Then we are assigning the output of this command here to user and the output of this command to pass.
We explained this command before, but in general we are splitting each line into two fields, separated
by space.
Then we take the first column, which is No.
One here for the username, and we do the same, but for column number two for the password.
Then we split them again, using common here.
Then we call the two variables in this line.
Finally at the bottom, we're taking input from my file, which is defined at the top of the script
is where?
Let's run it now and see the apple.
Perfect.
Let's take another example and how to take input from the user and match that input against the data
we are getting from the final.
In this script, we ask the user for his username and his password, same way we did before, and we
check if the username and password matched the ones we get from the file.
If they do, then we echo, they match, and then we break the loop.
Let's run it.
Notice, the only user and password match was the user nudniks and the password, Iola Besch, let's
verify that.
Here.
We have reached the end of this lesson, please practice and see you in the next one.