Branching

Two main structures for branching are if and switch.

If-statement

Syntax :

if (condition)
{
statement-1 
}

  • When an if statement is processed, the condition expression is evaluated, and the result is interpreted as a Boolean value.


If-else

Syntax :
if (condition)
{
statement-1
}
else
{
statement-2
}
  • If condition is true, statement-1 is executed.

  • If Condition is not true, and there is an else clause, statement-2 is executed.

  • If condition is false, and there is no else clause, execution simply proceeds with the next statement after the if construct.


if...else if....else Statement

Syntax :
if (condition)
  {
  Statement-1;
  }
else if (condition)
  {
Statement-2;
 }
else
 {
  Statement-3;
 } 

  • If condition is true, statement-1 is executed.

  • If Condition is not true, and there is an elseif clause, statement-2 is executed.?

  • If condition is false, and there is else clause, statement-3 is executed.


Switch

Syntax :
switch(expression)
{
  case value-1:
                   statement-1
                   statement-2
                   ......
                   [break;]
  case value-2:
                   statement-3
                   statement-4
                   ......
                   [break;]
  [default:

                  default-statement]
}