Q&A: If statements

When is the contents of an if statement executed?

if ( CONDITION )
{
  // This is executed when CONDITION evaluates to true
  DO_THING_A();
}

When is the contents of an else statement executed?

if ( CONDITION )
{
  // This is executed when CONDITION evaluates to true
  DO_THING_A();
}
else
{
  // This is executed when CONDITION evaluates to false
  DO_THING_B();
}

When is the contents of an else if statement executed?

if ( CONDITION1 )
{
  // This is executed when CONDITION1 evaluates to true
  DO_THING_A();
}
else if ( CONDITION2 )
{
  // This is executed when CONDITION1 evaluates to false
  // and CONDITION2 evalutes to true
  DO_THING_B();
}
else if ( CONDITION3 )
{
  // This is executed when CONDITION1 evaluates to false
  // and CONDITION2 evaluates to false
  // and CONDITION3 evalutes to true
  DO_THING_C();
}

When is the contents of an else, in an if/else if/else statement, executed?

if ( CONDITION1 )
{
  // This is executed when CONDITION1 evaluates to true
  DO_THING_A();
}
else if ( CONDITION2 )
{
  // This is executed when CONDITION1 evaluates to false
  // and CONDITION2 evalutes to true
  DO_THING_B();
}
else if ( CONDITION3 )
{
  // This is executed when CONDITION1 evaluates to false
  // and CONDITION2 evaluates to false
  // and CONDITION3 evalutes to true
  DO_THING_C();
}
else
{
  // This is executed when CONDITION1 evaluates to false
  // and CONDITION2 evaluates to false
  // and CONDITION3 evalutes to false
  DO_THING_D();
}

Relational operators:

Logic operators:


Author: Rachel Wil Sha Singh

Created: 2023-10-27 Fri 15:32

Validate