Erle Robotics C++ GitBook

Conditionals

The if conditional has the form:

if(condition){

    statement1

    statement2
    …
}

The condition is some expression whose value is being tested. If the condition resolves to a value of true, then the statements are executed before the program continues on. Otherwise, the statements are ignored. If there is only one statement, the curly braces may be omitted, giving the form:

if(condition)

    statement

The if-else form is used to decide between two sequences of statements referred to as blocks:

if(condition){

    statementA1

    statementA2

    …
}else{

    statementB1

    statementB2

    …
}

If the condition is met, the block corresponding to the if is executed. Otherwise, the block corresponding to the else is executed. Because the condition is either satisfied or not, one of the blocks in an if-else must execute. If there is only one statement for any of the blocks, the curly braces for that block may be omitted:

if(condition)

    statementA1

else

    statementB1

The else if is used to decide between two or more blocks based on multiple conditions:

if(condition1){

    statementA1

    statementA2

    …

} else if(condition2){

    statementB1

    statementB2

    …
}

If condition1 is met, the block corresponding to the if is executed. If not, then only if condition2 is met is the block corresponding to the else if executed. There may be more than one else if, each with its own condition. Once a block whose condition was met is executed, any else ifs after it are ignored. Therefore, in an if-else-if structure, either one or no block is executed.

An else should be included at the end. If none of the previous conditions is met the else is executed.

Here you have a simple example:


#include <iostream>

using namespace std;

int main() {

  int x = 6;
  int y = 2;

  if(x > y)

    cout << "x is greater than y\n";

  else if(y > x)

    cout << "y is greater than x\n";

  else

    cout << "x and y are equal\n";

  return 0;

 }

The output of this program is x is greater than y. If we replace lines 5 and 6 with

int x = 2;

int y = 6;

then the output is y is greater than x. If we replace the lines with:

int x = 2;

int y = 2;

then the output is x and y are equal.