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

  • Thread starter Thread starter deltapapazulu
  • Start date Start date
  • Tags Tags
    Loop
Click For Summary
SUMMARY

Entering '0' in a C++ do-while loop terminates the loop due to the implicit conversion of the integer to a boolean value, where '0' is interpreted as false. The code snippet provided demonstrates this behavior, as the loop continues until a '0' is entered, at which point the condition evaluates to false. Utilizing the -Wall compiler option can reveal warnings about such implicit casts, which are common in C and C++ programming. The discussion also highlights the use of similar boolean checks for pointers.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with vectors in C++ (e.g., std::vector)
  • Knowledge of boolean logic in programming
  • Experience with compiler options (e.g., -Wall)
NEXT STEPS
  • Research C++ implicit type conversions and their implications
  • Learn about the use of std::vector and its methods in C++
  • Explore boolean expressions and their evaluations in C++
  • Investigate compiler warning options and how to enable them
USEFUL FOR

Beginner C++ programmers, software developers learning about control structures, and anyone interested in understanding type conversions in C++.

deltapapazulu
Messages
84
Reaction score
13
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::count << "Please enter some integers (enter 0 to end):\n";

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

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

  return 0;
}
 
Technology news on Phys.org
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   Reactions: Asymptotic
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);
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
6
Views
2K
Replies
12
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K