rashid_aziz's blog

By rashid_aziz, history, 2 years ago, In English

include

using namespace std; int main() { string s = "abcd"; int i = -1; while(i < s.size()){ cout << "inside "; i++; } cout << "outside"; return 0; }

why this loop does not run, where as when we initialize i = 0, then it runs properly

  • Vote: I like it
  • +8
  • Vote: I do not like it

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by rashid_aziz (previous revision, new revision, compare).

»
2 years ago, # |
  Vote: I like it +17 Vote: I do not like it

You make a comparison between i, which is a signed int, and s.size(), which of an unsigned type. One of them must be casted to the type of the other one in order for the comparison to be calculated. Try casting s.size() to int manually and the code will run