User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







cpp:control_structures:if
click
to support this page (eg 0,42 € or so) Thanks.

if

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.

advertising
cpp/control_structures/if.txt · Last modified: 2012/12/16 07:48 (external edit)

Impressum