• Techiax

Tutorial - C programming - Decision Statements


Decision Statements:

Decision statements allow you to change the flow of control in your program based on some conditions of your choice. It can also be made to perform different functions with different user interactions.








C programming has the following decision statements:

  • if statement
  • if else statement
  • if - else if ladder
  • Switch
  •  Nested if statements
  • Nested Switch statements

if statement:

        It executes the code in if-sub-block  if the condition given in the if(condition) becomes true. If it becomes false, then the program will skip the sub-block of code to perform and continue rest of program.


 if(condition)
 {
 //code to perform
 }

Example:


 #include <stdlib.h>
 #include <stdio.h>

 int main() {
    int a=1;
    if(a==1)
    {
        printf("TRUE");
    }
    return 0;
 }

Output:


 TRUE


if else statement:

If the condition on the if(condition) becomes true then the code inside if block would be executed and if it becomes false then the code inside the else block would be executed. It has an extra feature of the else block to do some action if the condition becomes false.


 if(condition)
 {
 //code to run if condition becomes true
 }
 else
 {
 //code to run if condtion becomes false
 }


Example:


 #include <stdlib.h>
 #include <stdio.h>

 int main() {
    int a=1;
    if(a==2)
    {
        printf("TRUE");
    }
    {
        printf("FALSE");
    }
    return 0;
 }

Output:


 FALSE

if- else if ladder:

if- else if ladder is a series of if conditions one after another with its own code to perform. If all the conditions become false then the common else block code at the bottom of all the conditions.


 if(condition)
 {
 //code to perform
 }
 else if(condition)
 {
 //code to perform
 }
 else if(condition)
 {
 //code to perform
 }
 else
 {
 //code to perform
 }

Example:


 #include <stdlib.h>
 #include <stdio.h>
 int main() {
    int a=3;
    if(a==1)
    {
        printf("one");
    }
    else if(a==2)
    {
        printf("two");
    }
     else if(a==3)
    {
        printf("three");
    }
     else
    {
        printf("nothing");
    }
    return 0;
 }


Output:


 three


Switch statement:

Switch statement is a multiple branching statement. It takes only one variable and choose the codes to run based on the value in the variable. And it has a default condition at the end to perform if all the choices do not match the variable value


 switch(value)
 {
 case value1:
 {
 //code to perform
 }
 case value2:
 {
 //code to perform
 }
 case value3:
 {
 //code to perform
 }
 default:
 {
 }
 }

Example:


 #include <stdlib.h> 
 #include <stdio.h>
 int main() {
    int a=3;
   switch(a)
   {
    case 1:   
    {
        printf("one");
        break;
    }
    case 2:
    {
        printf("two");
        break;
    }
     case 3:
    {
        printf("three");
        break;
    }
     default:
    {
        printf("nothing");
        break;
    }
   }
    return 0;
 } 

Output:

 three

Note: Don't forget to add the break at the end of each case. If you didn't add it, then all the cases below your correct choice would be executed.

Nested if statement:


Nested if statements are the use of if, if else, else-if statements within their own code to perform i.e placing one statement inside another statement.

Example:


 #include <stdlib.h> 
 #include <stdio.h>
 int main() {
    int a=2;
    if(a<=10)
    {
        printf("a less than 10");
        
        if(a<5)
        {
         printf("\na less than 5");   
        }
    }
    else
    {
       printf("a greater than 10");   
    }
    return 0;
 }

Output:


 a less than 10
 a less than 5


You could also perform nesting with other statements. Try it with different statements and different programs.


Nested switch statement:

Nested switch statement is the use of one switch statement within the case block of another switch condition.

Example:


 #include <stdlib.h>
 #include <stdio.h>

 int main() {
    int a=2;
    int b=1;
   switch(a)
   {
    case 1:   
    {
        printf("a is one");
        break;
    }
    case 2:
    {
        printf("a is two");
        
        switch(b)
        {
            case 1:
            {
                printf("\nb is one");
                break;
            }
            case 2:
            {
                printf("\nb is two");
                break;
            }
            default:
            {
                printf("\nb is nothing");
                break;
            }
            
        }
        break;
    }
    
     default:
    {
        printf("a is nothing");
        break;
    }
   }
    return 0;
 }

Output:


 a is two
 b is one




Please share your views and thoughts that would help us do more.






Author:

I am an enthusiastic person striving to gather as much information as possible to achieve higher heights and to share the information to all others to make them also reach the stars.
"People who are crazy enough to think they can change the world are the ones who do"
-Steve Jobs.

Previous Post
Next Post