0

Ex: https://youtu.be/HHmiHx7GGLE?t=134

Ex: https://youtu.be/HHmiHx7GGLE?t=478

I sort of understand a do while loop. The loop can executes at least once.

The while loop can execute zero times if the condition is not met

A while loop can execute when the Boolean expression is true and the do while loop will can execute when Boolean expression is false.

There must be examples when a "do while loops" Boolean expression executes and is true and a "while loops" Boolean expression is false. Can someone give an example?

In the second link there is an error. My trouble is understanding that error. The error is while(n<0) and the correct answer is (n<=0). I think while (n < 0) makes more sense even though it doesn't work.

My thinking is you flip that sign n > 0 so n is greater than zero.

Can someone explain why n<=0 makes more sense ?

1 Answer 1

1

A while loop is used to do a specific something while the Boolean expression is true. Take the following code:

int a = GetInt();
while( a > 0 )
{
    // code
}

a > 0 is our Boolean expression. The loop will execute as long as this is true. But, if a is not greater than 0, the Boolean expression will evaluate to false, which will stop the while loop. It is possible for a while loop to never execute if the Boolean expression evaluates to false. But sometimes you want the loop to execute once, right? Say you want to ask someone something once, and then keep asking if they don't give the right answer. That's where do/while loops come in.

Do/while loops execute once and keep executing as long as the Boolean expression is true. Your line of thinking seems correct - you want n to be, in the end, greater than 0, correct? But what if the Boolean expression is n > 0? The do/while loop will keep executing as long as n is positive - thus, it'll only stop prompting the user if the user gives a negative number!

Now, n < 0 vs. n <= 0. We want a positive number, right? 0 is technically not a positive number nor a negative number, so it wouldn't work to count it as positive. n < 0 would allow 0 to count as a positive number, even though it shouldn't. Thus, n <= 0 works to prompt the user as long as they enter a negative number or 0.

If it helps you, here are CS50's definition of these loops:

while: Use when you want a loop to repeat an unknown number of times, and possibly not at all.

do-while: Use when you want a loop to repeat an unknown number of times, but at least once.

Let's look at while(n <= 0) and while(n < 1). These are essentially the same, because both check for 0 and negative numbers. n <= 0 simply specifies to check if n is less than or equal to 0, while n < 1 checks if n is less than 1.

However, if it was n >= 0 and n > 1, these would be different. In n >= 0, it is checking if n is greater than or equal to 0. This includes 0, 1, 2, 3, and so on. In n > 1, it checks if n is greater than 1. This includes 2, 3, 4, and so on, but not 0 or 1. The difference is that n >= 0 includes 0 and 1 while n > 1 doesn't.

If you still have issues/questions, comment below.

9
  • I Still have a problem with the n < 0 vs. n <= 0. Could you explain it again? Also would n< 1 vs n<=1 use the same logic as the 0 example?
    – Tom55555
    Commented Apr 2, 2017 at 1:51
  • The code for that loop wants a positive number and wants to keep prompting the user until they enter a positive number. In math, 0 is unsigned - it is neither positive nor negative. So, it technically wouldn't count as a positive number. By making it n <= 0, if the user enters 0, it'll count it as not positive (which it should) and prompt the user again. Does this help at all? :-) Commented Apr 2, 2017 at 1:55
  • Could I go n<=1. What happens if I type in zero?
    – Tom55555
    Commented Apr 2, 2017 at 2:35
  • Nice job, Nova, but I want to put a big fat bullseye on the loops. In the simplest terms, a while loop tests the condition at the beginning of the loop, so it will ONLY execute if the test condition is true. On the other hand, a do/while loop checks the test condition at the end of the loop, so it is guaranteed to run at least once. If the test condition is true at the END of the loop (even if it was not true at the beginning), it will run again. It's all about when the test condition is checked.
    – Cliff B
    Commented Apr 2, 2017 at 4:08
  • As for positive integers (and as long as we're talking integers) n<1 and n<=0 are effectively the same. If we were talking floats, only n<=0 would be valid to make the loop repeat when a non-positive value was stored in n.
    – Cliff B
    Commented Apr 2, 2017 at 4:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .