Decision-making statement

The if statement is a powerful decision-making statement. It is used to control the flow of execution of the statement. The syntax If ( text expression ) is allow the computer to evaluate the expression and depend on the value of expression is ‘true’or ‘false’.It transfers the control to statement.

Decision making statements in C language are:

  • Simple if statement
  • If…else statement
  • Nested if else statement
  • Else…if ladder
  • Switch statement

Simple if statement

In simple if statement, the block of statement can be a single. If the expression is true, then the statement is executed and if the expression is false, then the control transfers to the next statement. 

Syntax

if (expression)
{
statement
.......
}

This expression is called condition.


If…else statement

In if…else statement, if the expression is true, Then the statement 1 is executed and if the expression is false, then the statement 2 is executed. In case, both statement will be executed the control is transfers to the next statement.

Syntax

if (expression)
{
statement 1
}
else
{
statement 2
}

The both statements can be single statements.


Nested of if…else statement

Nested of if…else statement have to use more the one if else statement. Inside the nested have another if..else statement.

Syntax

if (expression 1)
{
if (expression 2)
statement A1
else
statement A2
}
else
{
if (expression 3)
statement B1
else
statement B2
}

It is a valid to write so because if..else statement is a single statement.


Else…if ladder

Else…if ladder is a type of nested statement in which there is if..else statement in every else part except the last else part. It is also called else..if chain.

Syntax

if (expression 1)
statement A
else
if (expression 2)
statement B
else
statement C

Each expression is evaluated on by one one in order. If none of condition is true, then the last else part is executed.


Switch statement

C has a built-in multi-way decision statement known as switch. Switch control expression is evaluated, if the value of expression matches with any case constant expression, then the control is transferred to that case label. If none of the case constant matches with the value of the expression then the control is transferred to default label.

Syntax

switch (expression)
{
case 1:
statement
........
case 2:
statement
........
......
......
default:
Statement
.........
}

Switch, case and default are a keywords. The constant expression in a case should be of integer or character. Each case have a unique value.The default is optional.