The 'if' statement is one of many methods to control the flow of a program. It tests whether or not a certain condition is true, and then executes code based on the test.
#include <iostream> using namespace std; int main(){ if (TRUE){ cout << "This statement will be executed"; } if (FALSE){ cout << "This one will not"; } return 0; }
The output of this simple program is as follows:
This statement will be executed
The 'if' statement is commonly used with the 'else' statement. Else gives a program direction in case a condition of an if statement evaluates to false.
It can be used to clean up the above code a bit:
#include <iostream> using namespace std; int main(){ if (TRUE){ cout << "This statement will be executed"; }else{ cout << "This one will not"; } return 0; }
These two programs produce the exact same output.