Stop Low Resolution Warning Sign

Transcript

Transcript of the tutorial. TOP


          
        
          So then just a quick video about do while loops, which is really just an extension of the while loop

          up here.
          
          So we've seen that we can do a while loop where we evaluate some kind of condition and if that is true,
          
          we run the code and then we add one to whatever the counter is.
          
          Now imagine that we start off with I equal to five, Then this thing right here, this is never going
          
          to be true and we're never going to run this code.
          
          Now, that might be fine, but sometimes you want the code to run at least once, even if this condition
          
          is not true.
          
          Okay, so even if I starts at five, it would be nice to maybe run the code once and then we don't execute
          
          the loop anymore.
          
          Okay?
          
          Because the condition is not true.
          
          So how do we do that?
          
          Well, we use a do while loop, which, like I said, is just an extension of the while loop.
          
          We just write it a bit different.
          
          So I'm going to take all this code here, the code block and cut it.
          
          And then above while I'm going to say, do and then I'm going to paste the code.
          
          Block And now what we're saying right here is do this code and it's going to do it once to begin with,
          
          regardless of the condition being true or false in here.
          
          And then at the end of the do block, we have our while keyword and then we do add a semicolon at the
          
          end because it's not a code block at the end anymore.
          
          We don't add them when it's a code block at the end.
          
          But now it's the while statement.
          
          We do add our semicolon.
          
          So again, we're saying let I equal five.
          
          Then we come down here, we do this code block regardless and we still add one to I.
          
          Then we run the while condition.
          
          And if I is less than five again, then we go back up to the loop and run it again and we carry on as
          
          normal.
          
          But all this does is run it once to begin with, even if the condition is not true in here.
          
          So let me save this and preview in a browser and we can see the value of I is five.
          
          So that is because we run this once an I is five.
          
          If we change this to four, then save it.
          
          It's still only going to run once because we run it and I is four.
          
          Then we add 1 to 4, which is five.
          
          Then we evaluate the condition.
          
          Well, I is five now, so it's not less than five.
          
          So we don't run it again.
          
          If I is three, then we should get it twice.
          
          We get three and four.
          
          Okay.
          
          So that's an easy way to execute a block of code inside a loop first, regardless of whether the condition
          
          is true or false.