User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







cpp:control_structures:if

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.

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
cpp/control_structures/if.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz