User Tools

Site Tools


cpp:control_structures:if

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpp:control_structures:if [2012/12/16 07:48]
cpp:control_structures:if [2024/02/16 01:04] (current)
Line 1: Line 1:
 +=====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.
 +
 +<code cpp>
 +#include <iostream>
 +using namespace std;
 +
 +int main(){
 +
 +if (TRUE){
 +   cout << "This statement will be executed";
 +}
 +
 +if (FALSE){
 +   cout << "This one will not";
 +}
 +return 0;
 +}</code>
 +
 +The output of this simple program is as follows:
 +<file>
 +This statement will be executed
 +</file>
 +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:
 +<code cpp>
 +#include <iostream>
 +using namespace std;
 +
 +int main(){
 +
 +if (TRUE){
 +   cout << "This statement will be executed";
 +}else{
 +   cout << "This one will not";
 +}
 +return 0;
 +}
 +</code>
 +These two programs produce the exact same output.

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War

Impressum Datenschutz