Monday, 9 September 2019

What is Nested if else statement and How Switch Statement in C Works?

Switch statement in C

There are some situations in real life, on which we have to make some decisions. On these decisions, we need to decide what we should do next. The same situation also occurs in our programming. Block of statements will execute or not based on these decisions. Decision-making statements are also known as the conditional statement in C. These statements are:

A) If statement
B) If else statement
C) Nested if else statement
D) Switch statement in C
E) Jump Statements
i) Break
ii) Continue
iii) Goto
iv) return

See Also: Operators in C

Nested If Else Statement

If our program has more than one conditions, then, we use the nested if else statement. Each condition is checked in the nested if else statement until we get the true condition.

In the nested if else, when the condition is true, then the statements associated with this condition is executed. All other conditions are skipped.

Syntax
if (condition-1)
{
statement 1;
}
elseif (condition-2)
{
statement 2;
}
..............
else if ( condition-n)
{
statement n;
}
else
{
statement;
}

Flow of Chart for Nested if else statement in C

Switch statement in C

Example 1

Write a program to compare two integer value using nested if else statement in C.


#include <stdio.h>
int main()
{
int a, b; //Declared variables
printf ("Enter values of a and b");
scanf ("%d%d", &a, &b); 
//Compare integer values are equal or not
if (a==b)
{
printf ("Integer a and b has same values");
}
//Checks a is greater than b or not
else if (a>b)
{
printf ("Value of integer a is greater than b");
}
//Checks a is less than b or not
else
{
printf ("Value of integer a is less than b");
}
return 0;
}

Output
Enter values of a and b 12 13
Value of integer a is less than b

Explanation


First, Declared variables a and b.
Enter the values of a = 12 and b= 13.
Check first if condition i.e. (a==b), means check (12 ==13) 12 is equal to 13 or not. 12 is not equal to 13. Condition fails. Statements attached to this condition is not executed. Compiler checks next condition i.e. (a>b). a is greater than b (12>13) or not. a is not greater than b. Again condition fails. So, statements associated with it not executed. Now, the last statement attached to else will print.

See Also: Structure in C

Example 2

Write a C program to find out given triangle is equilateral, scalene or isosceles.


#include <stdio.h>
int main()
{
int side1, side2, side3; //Declared variables
printf ("Enter values of three sides of a triangle");
scanf ("%d%d%d", &side1, &side2, &side3);
//If triangle is equilateral, then all sides are equal.
if ((side1==side2)&&(side2==side3))
{
printf ("Triangle is equilateral");
}
//If triangle is scalene, then no side is equal.
else if (side1!=side2 && side2 != side3 && side3!=side1)
{
printf ("Triangle is scalene");
}
else
{
printf ("Triangle is isosceles");
}
return 0;
}


Output
Enter values of three sides of a triangle 13 12 13
Triangle is isosceles

What is the Switch Statement?

The switch statement in C reduces complex conditional and branching operations. This statement is the replacement of nested if else statement. It tests or compares the variable value with multiple cases. When the value is matched with the case, then, statements associated with it will execute. Each case has a different name and number known as the identifier.

Syntax
switch (n)
{
case1:
Statement1;
Break;
case2:
Statement2;
Break;
default:
statement;
Break;
}
Statement z;

Flow Chart of Switch Statement in C


Switch statement in C

Example 1

What happened when the break statement is not used in the switch statement?


#include <stdio.h>
int main()
{
int choice;
printf ("Enter your choice\n");
scanf ("%d", &choice);
switch (choice)
{
case 1:
printf ("case 1\n");
case 2:
printf ("case 2\n");
case 3:
printf ("case 3\n");
case 4:
printf ("case 4\n");
default:
printf ("default\n");
}
return 0;
}

Output
In the above program, if the choice is 1. Then all the statements are executed because there is no break statement in the program.

Switch statement in C


When the choice is 2, then the control jumps to case 2. Execute all the statements because there is no break statement.

Switch statement in C

Example 2

What happened when multiple cases have a single statement?


#include <stdio.h>
int add(int a, int b); //Declaration of add function
int main()
{
int choice; //Declared Variables
printf ("Enter choice\n");
scanf ("%d", &choice);
switch (choice)
{
case 1:
case 2:
case 3:
case 4:
add(10, 20);
break;
case 5:
printf ("What happened\n");
break;
default:
printf ("Default\n");
}
return 0;
}
int add(int a, int b)
{
int result;
result = a+b;
printf ("result = %d\n", result);
}
Output
Switch statement in C

Let's see when the choice is 2. Even then, add function will execute.

Switch statement in C

Example 3

What happened when the switch case has the same case label?


#include <stdio.h>
int main ()
{
int choice; //Declared Variables
printf ("Enter Choice");
scanf ("%d\n",&choice);
switch (choice)
{
case 1:
printf ("Case 1");
break;
case 2:
printf ("Case 2");
break;
case 2:
printf ("Case 3");
break;
default:
printf ("default");
break;
}
return 0;
}
Output
error: duplicate case value

Example 4

What happened if the case value is a floating number in the switch statement in C.


#include <stdio.h>
int main()
{
int choice; //Declared variable
switch (choice)
{
case 1:
printf ("Case 1");
break;
case 2.5:
printf ("Case 2");
break;
default:
printf ("default");
break;
}
return 0;
}
Output
error: case label does not reduce to an integer constant

Example 5

What happened when the default case is used anywhere in the body?

#include <stdio.h>
int main()
{
    int choice; //Declaration of Variable
    printf ("Enter your choice");
    scanf ("%d", &choice);
    switch (choice)
    {
    default:
        printf ("default\n");
        break;
    case 1:
        printf ("Case 1\n");
        break;
    case 2:
        printf ("Case 2\n");
        break;
    case 3:
        printf ("Case 3\n");
        break;
    case 4:
        printf ("Case 4\n");
        break;
}
return 0;
}
Output
Enter your choice 5
default

Some Points to Remember

A) The switch statement in C must have integral value. For instance,

switch(1+2+3)
switch(1*2+12/4)
switch(a+b+c)
switch(a*b+c/d)


B) Case Values should not be duplicate.

C) It is your choice to use a default statement. If you don't want to use the default statement, then the program will run without any problem.

D) The break statement is used to terminate from the sequence of the statements.

E) A break statement is an option for the programmer. If you don't want to use the break statement, then, the execution of the program will continue until the break statement occurs.

F) Nesting of the switch statement in C is allowed. But, it makes the program complex and less readable.

Nested Switch Statement in C

When we use many switch statement inside a switch statement. Then those statements are known as a nested switch statement. For instance,
#include <stdio.h>
int main()
{
    int a = 1, b = 2; //Initialization of variables
    switch (a)
    {
    case 1:
        printf ("Outer switch loop\n");
        switch(b)
        {
        case 1:
            printf ("Case 1 switch inner loop\n");
        case 2:
            printf (" Case 2 switch inner loop\n");
            break;
        }
    }
    return 0;
}