Hello and welcome. I have decided to create seperate tutorials for each looping statement.
I want to keep it simple. This will be useful as a reference also if you need to refer to how a particular loop works.
In this tutorial, we will learn about the WHILE loop
A WHILE loop checks for a condition at the start at the loop and executes the code depending on whether this condition is True or not
for example I'll start my 'WHILE loop' here and this is the condition and this is my block.
I'll represent my block between curly brackets.
My condition is here. Now, in the 'IF statement', for example I used 1==1.
Now if I say 'test' or 'loop' here.
This is a loop here and then a break. Now what happens is as long as 1=1, it would create a loop
If I do something here, lets try this.
It would probably crash your browser because the loop would be repeated as long as 1=1 and for infinite no. of times, 1 will always equal 1.
So since the loop will always be repeated, your browser will crash.
Lets say while a variable, 'num' is smaller or equal to 10 and under echo i can say - 'num ++'
'++' is an arithmetical operator. And what it basically does is, it increases num by 1. It is the same as writing 'num =num +1'
So it takes num and says that it is equal to the value of num plus 1.
So this again is an arithmetical operator. What's going to happen is -
We are going to say 'num' lesser than or equal to '10', if yes then echo loop and then say add 1 on to the variable num
But what we should really do is create 'num = 1' at the moment. So loop once at 1. This will then equal 2 then 3 then 4 all the way up to 10 and then it will stop
After that the rest of the code below this will continue.
So we said this 1 and see what will we get. Okay we got a loop 1,2,3,4,5,6,7,8,9,10 times
Now to make it more fun I'll say loop 1 and I'll concatenate 'num' to the end of that
In fact let's make it simpler and say 'num' inside - it will make it much easier to read
Okay I'll say loop 1 and add 1 and then I'll say loop 2 and I'll add another 1; that's loop 3 add another 1 up to 10.
Lets open this up. Refresh it. There you go. You got loop 1,2,3 all the way up to 10 as you can see
Lets change this value here to 100. Refresh that. You can see it has gone to hundred. The bigger the number, the longer it will take to loop
Let's take 6000. Let's refresh that. It's going to take a while. There you go - up to 6000. So it's very efficient in this way
You can try combining this with an 'array' to create a program that echoes out of the alphabet inside the 'array'
You can use loops to echo out every single value of an array
Just give it a go. I will probably do this in one my tutorial - not in the basics section though
However, this is the basics structure. What I also recommend you to do is create a variable here called 'max' and put your maximum value here
This will do exactly the same thing. It's just a lot more easy to read and you can declare all this here and this would be a reference to it
If you have more than 1 loop. I prefer it for readability and flexibility for my program. Okay so that's a WHILE loop. Let me summarise. It checks the condition of the start
If the condition is True. it would execute this block of code and you can do things like 'echo alpha'
Your variable is being incremented. Make sure that you do increment your variable otherwise it will loop for infinity.
Thanks for watching. This is Sidharth for the Spoken Tutorial Project, signing off. Goodbye.