Conditions – true or false

When you want to do a test against true or false with a integer value. Basically 0 is false and anything else is true, as demonstrated below.

#include <stdlib.h>
#include <iostream>
 
using namespace std;
 
int main()
{
  for (int i = -10; i < 10; i++)
  {
      if (i) 
	  cout << "true " << i << endl;
      else
	  cout << "false " << i << endl;
  }
}

and the output of this is, the true/false values is after the if statement.

true -10
true -9
true -8
true -7
true -6
true -5
true -4
true -3
true -2
true -1
false 0
true 1
true 2
true 3
true 4
true 5
true 6
true 7
true 8
true 9

Leave a Reply

Your email address will not be published.