Why does entering '0' terminate do-while loop here?

  • Thread starter deltapapazulu
  • Start date
  • Tags
    Loop
In summary, the conversation is about a do-while loop in C++ and the use of '0' as input to terminate the loop. The reason for this is that in C++, '0' is equivalent to false and the loop only continues as long as the condition is true. This behavior is inherited from C programming. The conversation also mentions the use of implicit casting and how it can be used to check for pointers.
  • #1
deltapapazulu
84
12
I am relatively new to programming and C++ and for the life of me I can't figure out why entering a '0' as input in the do-while loop here terminates the loop.
Code:
// vector::push_back
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
   std::cin >> myint;
   myvector.push_back (myint);
  } while (myint);

  std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";

  return 0;
}
 
Technology news on Phys.org
  • #2
Zero means false in C++ a holdover from C programming.

https://www.le.ac.uk/users/rjm1/cotter/page_37.htm

http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=bool
 
  • Like
Likes Asymptotic
  • #3
What's happening is that your int is being implicitly cast to a boolean. This is explicitly what you are doing.
C:
do {
   std::cin >> myint;
   myvector.push_back (myint);
  } while (static_cast<bool>(myint));
If you use the -Wall compiler option, you should get a warning about the implicit cast.
This also works for things other than ints. It's very common to check for pointers this way.
C:
FILE * fp = fopen("/tmp/file", "wb");
if (fp){
   //write to file because it's open
} else {
   std::cerr << "Was not able to open file" << std::endl;
}
fclose(fp);
 

FAQ: Why does entering '0' terminate do-while loop here?

Why does entering '0' terminate the do-while loop?

The condition for the do-while loop is to continue running as long as the input is not equal to 0. When 0 is entered, the condition is no longer met and the loop terminates.

Can any other value be used to terminate the do-while loop?

Yes, any value that is not equal to the condition can be used to terminate the do-while loop. In this case, 0 was chosen as the terminating value.

What happens if the input is not a number?

If the input is not a number, the loop will continue to run as the condition is still not met. To terminate the loop, a different condition or terminating value would need to be used.

Why is the do-while loop used instead of a while loop?

A do-while loop ensures that the code within the loop is executed at least once, even if the condition is initially false. This can be useful in situations where the first iteration of the loop is necessary for the code to function correctly.

Can the do-while loop be nested within another loop?

Yes, the do-while loop can be nested within other loops or conditional statements. This can be useful for creating complex loops and conditions in a program.

Similar threads

Replies
22
Views
3K
Replies
1
Views
1K
Replies
8
Views
1K
Replies
8
Views
2K
Replies
10
Views
2K
Replies
1
Views
1K
Replies
39
Views
4K
Back
Top