0

I was wondering if you could clarify me the difference between the conditions while(k) and while(k > 0) when do they differ?

#include <iostream>
int main() {
    int k;
    std::cin >> k; 
    while(k) {
        std::cout << "Hello" << "\n";
        k--;
    }
}
#include <iostream>
int main() {
    int k;
    std::cin >> k; 
    while(k > 0) {
        std::cout << "Hello" << "\n";
        k--;
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Fabiodlt
  • 9
  • 3
  • 1
    Does this answer your question? [Casting int to bool in C/C++](https://stackoverflow.com/questions/31551888/casting-int-to-bool-in-c-c) – ph3rin Feb 08 '21 at 20:09
  • 3
    `while(k)` is equivalent to `while(k != 0)`. – Damien Feb 08 '21 at 20:10
  • Try searching on the internet, before asking a question. – Nitai Chandra Banik Feb 08 '21 at 20:10
  • @sweenish solved – Fabiodlt Feb 08 '21 at 20:10
  • @NitaiChandraBanik i found nothing – Fabiodlt Feb 08 '21 at 20:11
  • @Damien I know I wanted to know how do they differ – Fabiodlt Feb 08 '21 at 20:11
  • 2
    The first version doesn't take negative `k` into account. Try entering `-1` and see what happens. (Note that signed overflow is undefined in C++). – rustyx Feb 08 '21 at 20:11
  • @Fabiodlt In this context, you will get the same result, if `k > 0`. Infinite recursion in the first case if `k < 0` – Damien Feb 08 '21 at 20:12
  • I mean, it does. It just matters on what you mean by "take into account." – sweenish Feb 08 '21 at 20:12
  • @Fabiodlt check this link for understanding while loop: https://www.programiz.com/c-programming/c-do-while-loops – Nitai Chandra Banik Feb 08 '21 at 20:12
  • As @Damien said, `while(k)` is equivalent to `while(k != 0)` which is different from `while(k > 0)` only if k can jump over zero and become negative - which can't happen in your example, however if you wrote a program to print k/2 lines of "Hello" and you did that with `k-=2;` instead of `k--` then there would be a difference when you entered an off number for k.. – Jerry Jeremiah Feb 08 '21 at 20:14
  • Everyone here assuming the input will always be positive. – sweenish Feb 08 '21 at 20:15
  • @Fabiodlt Both versions run into [undefined behavior](https://en.cppreference.com/w/cpp/language/ub) if you enter something other than a number, `abc` for example. Additionally, the first version runs into UB if you enter a negative number such as `-1`. – dxiv Feb 08 '21 at 20:17
  • @πάνταῥεῖ Not sure that's really a duplicate. Closely related, for sure ... but where is the cast in this question? – Adrian Mole Feb 08 '21 at 20:27
  • @AdrianMole _"but where is the cast in this question?"_ It's done implicitely, and conditions require a `bool` value, isn't that the whole point? Also OP, confirmed, it explains their problem: https://stackoverflow.com/questions/66108612/how-do-these-two-conditions-differ?noredirect=1#comment116879720_66108612 (the auto generated duplicate comment was there before) – πάντα ῥεῖ Feb 08 '21 at 20:30
  • @πάνταῥεῖ OK - I'll concede *this time*. ;) – Adrian Mole Feb 08 '21 at 20:36
  • @Adrian Well, as always, there's nothing wrong with duplicate duplicate questions, as long quesiton and answer(s) themselves provide valuable content. ;-) – πάντα ῥεῖ Feb 08 '21 at 20:55

1 Answers1

1

The two code snippets you present will give equivalent results if – and only if – the input value for k is not negative. Try it.

Specifically, for the first snippet, if a value of -1 is input for k, the --k; line inside the while loop will (maybe) never reduce k to zero, or (maybe) just take a long time (until k reaches INT_MIN – something like -2147483648), depending on how the platform you are using handles signed integer underflow.

The while (k) loop will run until k is zero.

However, in the second snippet, a negative input for k will mean that the loop never runs (k will not be greater than zero on the first test).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I mean why in the first snippet the condition while(k) is regarded as while(k!=0)? – Fabiodlt Feb 08 '21 at 20:30
  • @Fabiodlt If you're happy that the linked duplicate answers your question, then fine. If I were to elaborate in my answer, I would simply quote that same question/answer. – Adrian Mole Feb 08 '21 at 20:38