Decision making or conditional statement and Branching statements (conditional Statements) in C++

types of conditional statement in c++

  1. Simple if Statement
  2. if – else Statement
  3. Nested if-else statement
  4. else – if Ladder
  5. switch statement
if else statement in cpp

Simple if Statement in c++

The if statement is a powerful decision making a statement and is used to control the flow of execution of statements

The syntax of a Simple if is :

if(condition)
{
   statement
}

it is only one option

The statement is executed only when the condition is true.

In case the condition is false the compiler skips the lines within the “if Block”.

The statement block may be a single statement or a group of statements.

If the Condition is TRUE, the Statement Block will be executed and executes the rest of the program.

If the Condition is FALSE, the Statement Block will be skipped and the rest of the program executes next.

if-else in c++

If the condition is true, then the true block statements are immediately executed. Otherwise, the false block statements are executed.

In if-else either True – Block or False – Block will be executed and not both.

The “else” Statement cannot be used without “if”.

The syntax of an if-else is :

if (condition)
{
    statements; /*true block (or) if block */
}
else
{
    statements; /* false block (or) else block */
}

Nested if–else in c++

Using one if-else statement in another if-else statement is called a nested if-else control statement.

When a series of decisions are involved, we may have to use more than one if-else statement in nested form.

The syntax of a Nested if-else is :

if ( test condition1)
{
    if ( test condition2)
    {
        statement - 1;
    }
    else
    {
        statement - 2;
    }
}
else
{
    if ( test condition3)
    {
        statement - 3;
    }
    else
    {
        statement - 4;
    }
} /* end of outer if-else */

If Test Condition-1 is true then enter into outer if block, and it checks Test Condition-2 if it is true then Statement-1 is executed if it is false then else block is executed i.e Statement2.

If Test Condition -1 is false then it skips the outer if block and it goes to the else block and Test Condition-3 checks if it is true then Statement-3 is executed, else Statement-4 is executed.

else–if Ladder in c++

Using one if-else statement in another if-else statement is called a nested if-else control statement. This is another way of putting ifs together when multiple decisions are involved.

A multipath decision is a chain of ifs in which the statement associated with each else is an if.

The syntax of an else–if Ladder is :

if (test condition - 1)
{
    statement - 1;
}
else if ( test condition - 2)
{
    statement - 2;
}
else if ( test condition - 3)
{
    statement - 3;
}
 :
 :
 :
 :
else if ( test condition – n)
{
    statement – n;
}
else
{
    default statement;
}

The above construction is known as else if ladders.

The conditions are evaluated from top to bottom.

As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the Rest of the Program Statement–X (skipping the rest of the ladder).

switch Statement in c++

The switch statement is a multi-way branch statement.

In a program, if there is a possibility to make a choice from a number of options, this structured selection is useful.

The switch statement requires only one argument of int or char data type, which is checked with a number of case options.

The switch statement evaluates an expression and then looks for its value among the case constants.

The syntax of a switch case is :

switch(variable or expression)
{
  case value-1:
  {
      Block -1; (or) Statement-1;
      break;
  }
  case value-2:
  {
      Block -2; (or) Statement-2;
      break;
  }
      _ _ _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _ _ _
      _ _ _ _ _ _ _ _ _ _
 
  case value-n:
  {
      Block -n; (or) Statement-n;
      break;
  }
  default:
  {
      default – block; (or) Statement;
  }
}

If the value matches with the case constant, then that particular case statement is executed.

If no one case constant is not matched then the default is executed.

Here switch, case, and default are reserved words or keywords.

Every case statement terminates with the colon “:”.

In switch, each case block should end with a break statement.