if
The
if statement allows either
executing or not a certain
statement according to the truth-ness of a boolean, arithmetic or
pointer expression. For example, the following statement will print
out
true because the expression between parentheses is
true:
if(true)
cout << "true\n\n";
while the statement:
if(3)
cout << "3 is different from 0, hence interpreted as true\n\n";
prints a message on the screen, because any expression not evaluating to 0
is interpreted as true.
The statement:
if(false)
cout << "false";
will print out nothing.
Example
#include <iostream>
using namespace std;
int main()
{
if(3)
cout << "3 is different from 0, hence interpreted as true\n\n";
int i;
cout << "Enter a number: ";
cin >> i;
if(i > 0)
cout << "Number is positive\n\n";
if(true)
cout << "true\n\n";
if(false)
cout << "false";
}
Output
3 is different from 0, hence interpreted as true
Enter a number: 8
Number is positive
true