C Decision Making Statement In easy way part 2

C Decision Making Statement (nested if-else and switch statement)

As the name says decision making statements helps us to take decision based on certain condition. In C programming we have following decision making statements.

  1. if statement
  2. if-else statement
  3. else-if statement
  4. nested if-else statement
  5. switch statement

nested if-else statement

When we write if statement inside on another if statement is known as nested if-else.

We can nest if statements as many as we want. We can also write if statement is else block of if-else statement.

Syntax:

    if(condition)
{
  statements;
if(condition)
{
statements;
      }
else
{
statements;
}

}
else
{
statements;
}

Code given below is one of the classic example of nested if-else statement. In this example we try to find out greater number among given three number(x,y and z).

First we compare value of x with value of y, the condition is “x>y” which is false, hence if block will not get executed instead else block will get executed.

In else block we have one more if-else statement. This time condition in if statement is “y>z” which is also false, hence else block will get executed.

Example:

    int x=12, y=13, z=14;
if(x>y)

{
if(x>z)

{
printf("X is greater");
}
else
{
  printf("Z is greater");
}

}
else
{
if(y>z)
{
printf("Y is greater");
}
else
{
printf("Z is greater");
}
}

Output:

    Z is greater

Try this fun game

ITVoyagers

switch statement

decision-making-statement-switch-itvoyagers

switch statement can easily replace long if statements or else if statements which compares the variable value with several character or integer values.

switch is multipath decision making statement.

Unlike other decision making statements switch takes decision based on value of expression or variable.

Expression which is given in switch should provide constant value else it will be considered as invalid. 

default case is not compulsory in switch case statement.

We can have any number of cases.

No two cases can have same values for checking.

We use break statement to terminate switch case statement.

Although break statement is optional in switch case statement but break statement will allow us to stop executing switch case statement and get out of it, otherwise we will end up executing all the further cases until we find break or until we reach the end of switch case statement.

Nested switch case statement is allowed.

Syntax:

    switch(expression)
{
  case value_1:
statements;
break;
case value_2:
statements;
break;
case value_3:
statements;
break;
.
.
.
default:
statements;
break;
}

In below example value of i is 2.

Now we mention i in switch and we will use case to check value of i and take decision based on it. Now first case will compare its value with the value i it doesn’t match so next case will compare its value, now in second case value is matched and hence its block of code will get execute which means 

Value of i is equal to 2

will get printed in output and next it will encounter break statement which will terminate the switch case statement.

Suppose if we didn’t find any match then default case’s block of code will get executed.

Suppose we didn’t mention break statement in second case then after executing second case block of code third case block of code will also get executed and this will goes on until it reaches break statement or end. (Check example 2)

Example 1:

    int i = 2;
switch(i)

{
case 1:

  printf("Value of i is equal to 1");
break;
case 2:

  printf("Value of i is equal to 2");
break;
case 3:

  printf("Value of i is equal to 3");
break;

default:
printf("Value of i in not 1, 2 or 3");
break;
}

Output:

    Value of i is equal to 2

Example 2:

    int i = 2;
switch(i)

{
case 1:

  printf("Value of i is equal to 1");
break;
case 2:

  printf("Value of i is equal to 2");
case 3:

  printf("Value of i is equal to 3");
break;

default:
printf("Value of i in not 1, 2 or 3");
break;
}

Output:

    Value of i is equal to 2
    Value of i is equal to 3

Try this quiz

ITVoyagers

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment