hi welcome to an Arduino step-by-step course by Robojax
now let's have a look at a while loop while loop is used extensively in programming and also
an Arduino so what is a while loop a while loop is a type of loop which executes
block of code until a certain condition is met and when the condition is met the loop exits
so let's see how we can define a while loop
while loop will be defined this way while loop starts with a word while with lowercase w and
then we have open and close parenthesis here and then the other important part of it is this
curly braces that that's opening and then we have also the closing
and then the condition goes between these two parentheses that opens and close here in English
it means while this condition is true do these codes whatever is between this opening and closing
and here is a very basic while loop example we have a variable b of type integer and the value
is 4. now we have a while loop this while loop says that while b is greater than zero do this
we said whatever is between these two will be executed so do this so what it means is that
when b is greater than zero at the moment b is greater than zero then it says decrement which
means subtract one from b and then print the b and when it could reaches here it goes back so this is
what happens first it compares b then it comes to the this section first we will decrement b becomes
3 and then we will print it so this line will print b for us and after that we go back here
now b is 3 is 3 bigger than 0 true if this is true then continue come here and then decrement when
it decrements b now becomes two then after this this line will print it for us it will become two
after this it goes back here compares now if true come back here
subtract one from two so two become becomes one and then print this line will print it
and after this we go again and then compare is one bigger than zero true come here decremented
it becomes zero and then prints b now after this again compares is 0 bigger than 0 false
and this cannot happen nothing will be executed and the while loop exits so the while loop
stays here as long as the condition between the two parentheses are true
this will continue doing whatever is inside here if you found this tutorial helpful please thumb
up and also click on the subscribe on the right side in the lower corner I really appreciate it
now let's have another example this time we have b equals zero and here's now I put it
below so it will first print and then this is incrementing plus plus mean add one more to b
first we compare if b smaller than 4 true because b is 0 and 4 is large
then the code comes here prints b and then increments
b is one now from here code goes back here compares is one smaller than four through
comes here prince one increments b is two goes back here compare is 2 is smaller than 4 through
prints 2 increment b becomes 3 goes back there is 3 smaller than 4 true
now we print 3 we end big b becomes 4
and when the code goes back here compares 4 is not smaller than four exits increment
for example three this time we check the j we have one variable called j initial value is true
and we have integer b this is zero and we have a while loop checks the j
g is a Boolean automatically it is either true or false prince increments and here we
have extra condition here it says if b is equal 3 this means equal equal it compares that if it
is 3 then set j equal false so if b is equal 3 we are changing j to false initially it is true
so first while j is j true because it is true comes here and prints 0
and then increment b is 0 b becomes 1 this is false because b is not equal 3 so
this condition is not true so this will not happen it goes here now is j true yes because
we have not changed it g is still true come here and print one increments it becomes two, then two
is not equal 3 this will not be executed it goes here j is true prince 2 increments it
b become 3. now here we check b is equal to 3 true then we will change j is equal false now this
comes here j is equal false the code will exit nothing will be executed as soon as this is false
this time we are using a new syntax break in order to break that code regardless of this condition
uh let's check it we have b equals zero and b is smaller than four it's true this will print it and
b becomes one this is not true we're not executed b one is smaller than four parent become two
now if b equals two yes break, break means do not go there immediately code will exit from loop and
proceeds to the code after this curly practice another loop called this is
exactly the same as a loop but the condition of while which was at the beginning now it's
at the end and we have the word do at the beginning so that's why this is called
do while this is exactly the same example the same outcome I’m just presenting it so
initially it doesn't compare initially it just execute the code this code execution involves
first decrement so b become three and then print it becomes three and then after that compare
is bb bigger than zero true goes here decrement b becomes 2 prints 2 compare is 2 bigger than 0 true
decrement become 1 prints 1 is one bigger than zero true and then decrement b becomes zero
and then prints zero because after this we print it
then we come here is zero bigger than zero falls immediately exit
please make sure to subscribe by clicking the subscribe button
in here thank you thank you for watching tutorial from Robojax if you learned something and found
this useful please thumb up as this will help my video in the search algorithm of YouTube
if you have comment or question please post it at the comment section below
I try to answer and reply and don't forget subscribe so you get updates on my upcoming videos