While Loops – Intro to Computer Science

I said we didn’t need any new constructs, but we are gonna introduce one and that’s gonna make writing the code we need for a search engine much more convenient It’s also gonna be very useful for lots and lots of procedures that we wanna write and the construct we are gonna introduce is called a loop It’s a way to do things over and over again The kind of loop that we are gonna introduce first is called the while loop The syntax for the while loop is this We have the key word “while”, followed by a test expression followed by a colon, and then inside we have a “block” and the “block” is a sequence of instructions This is very similar to the “if” statement So as a reminder, here’s what the “if” statement look like we had an “if” followed by a test expression, followed by a colon and then an indented “block”, which is a list of statements that executes whenever the test expression evaluates to a “True” value With “if”, the block executes either zero or one times, depending on whether the test expression is “True” With “while”, the block can execute any number of times It keeps going as long as the test expression is “True” So with the “if ” statement, if the test expression is “True” we go to the block, run the block and then continue if the test expression was “False”, we go right to the next expression With a “while”, we also start by checking the test expression if it’s “True”, we go to the block But now, instead of going to the next statement, after the block, we go back we try the test expression again, if it’s “True”, we go back to the block we always go back to the test expression if it’s “True”, we do the block again, we go back to the test expression If it’s “True” again, we do the block again, we go back to the test expression and we can keep going around as many times as we need as long as the test expression is “True” we’ll keep executing the block and keep trying the test expression again At some point, maybe the test expression is “False” Once the test expression is “False”, we go to the next instruction So this means a while loop can execute the block either zero times if the test expression was “False” at the beginning one time if it was “True” the first time, but “False” after that two times, three times…any number of times, it could keep going forever There’s no requirement that guarantees the test expression eventually becomes “False” So here’s an example of while loop. We start by initializing variable i, and we’ll give it a value, zero then we have the “while”, the test expression says i is less than ten So that means as long as this evaluates to “True”, we’ll evaluate the block and what the block does is print i and then add one to i So here’s what happens when this executes So initially, the value of i is zero, i is less than ten So that means we’ll enter the loop So that means the test expression is “True”, so we’ll enter the block, we’ll print i So we’ll see the value zero printed and then we’ll do the assignment, so that will change the value of i We add one to i, so that’s gonna be making the value of i now refers to one So if it was an “if”, we would be done now, but because it’s a “while”, we keep going We go back, test again, if i is less than ten Now the value of i is one, which also is less than ten so we continue, go to the block again we are gonna print i, this time we’ll see the value one Then we go to the next statement, increase the value of i by one that’s gonna make the value of i two Now i refers to the number two Because it’s a “while”, we keep going, we go back to the test expression i is less than ten, still less than ten, now it’s two, we are gonna print the two we are gonna add one that it will make the value of i three and we are gonna keep going Test again, and i is still less than ten, so we are gonna print i again we will print the value nine, then we add one, so that’s gonna have one to nine, we’ll get ten So that’s the new value of i and we go back again and now we do i is less than ten, and now i has the value ten Ten is not less than ten, so that will be “False” and we are done with the while loop We’ll continue with whatever statements here, in this case, there is none So then the loop——what we’ve done, we’ve gone through it ten times we’ve printed the numbers from zero to nine The new value of i will be ten, if we do anything here that uses the value of i we’ll see that the value of i is ten So to see that you understand while loops, we’ll have a quiz So the question is what does this program do Here’s the program We start by assigning zero to i We have a while loop, where the test is not equal to ten So the test is i is not equal to ten Then we have i equals i plus one, so we are assigning to i the value of i plus one and then we are printing i So this is similar to the example, but different in a couple of ways So it’s up to you to see if you can figure out what the program does Try to figure out yourself You can certainly also try running this in the Python interpreter The choices are produce an error print out the numbers from zero to nine print out the numbers from one to nine print out the numbers from one to ten or the final choice is it runs forever or at least until our machine runs out of power So see if you can figure out what it does You can definitely try running it but try to figure out on your own before running it in the Python interpreter

34 thoughts on “While Loops – Intro to Computer Science

  1. Thanks for comparing While and if…….
    can we use colon instead of parenthesis in WHILE ???
    Semicolon after printf() is missing

  2. Actual answer is 1-10: Option 4. The reason it stops at 10 instead of 9 now is because it's checking… is i = 10 yet? once it says YES then we exit the loop. The loop terminates RIGHT there because it reached its goal which is 10. Only part i don't get is why it does not start at 0 to 10… why 1.

  3. i = 0
    while i is not equal to 10
    i = i +1
    print i

    is i not equal to 10? ( i = 0)
    Yes, the program ends until it keeps adding the value 1 to the variable i until it defies the while condition, repeating the while loop 9 times until "not equal to 10" becomes "equal to 10". Thank me later

  4. lol u just made it unnecessarily complecated…
    For printing "Ben" 10 times in a loop:

    10 FOR i = 1 to 10
    20 PRINT "Ben"
    30 NEXT i
    40 END

  5. heloo any one can help me to solve this program
    Write a C++ programme that reads a sequence of integers terminated by zero value then finds the percentage of the negative numbers and the percentage of the positive number in the sequence. The programme output is as shown: :

    Enter a sequence of in integers terminated by zero

    6 -7 -8 10 -12 14 15 2 -6

    the percentage of negative numbers is 44.4

    the percentage of positive numbers is 55.6

  6. Good explanation, but damn that's weird they way you're writing om the screen, I think I spent more time thinking how that could be possible in the real world than I should've.

  7. It took me longer to finally find the answer, at first I would gave the answer as 0 to 9. And then, I scroll down the comments to see if anyone have the same answer as mine. Unfortunately, most of people here answered 1 to 10. I kept on insisted that my answer just now was correct. Wrote the syntax in vb.net and also python emulator. I still got 0 to 9. then I REALIZE something. Take a look at i=i+1 positions. It'll give you the answer straight away. 😉 anyway, big thanks to the tutorial guy, I finally understand how loops works.

  8. Great video but your hands distract from everything. We don’t need to see your hand writing it, we need to see the code. Also, use white font on a black background like anyone who stares at a screen for 8+ hours a day does, can’t watch this after a day of work + school.

Leave a Reply

Your email address will not be published. Required fields are marked *