Control Statements | IF Statements | Switch Statement | Examples

Control Statements 

These are those statement which controls the flow of program control. They transfer the control of program to another part of program conditionally or unconditionally.

There are several control statements in the C Programming which are being discussed below -

If Statement 

If statement evaluates the condition inside the parentheses. if the condition is true then statement inside the if body are executed and if the condition is evaluated to false then statements inside the body of if are skipped.

Syntax :

 if(condition)
                {
                    Statements;
                 }

Flow Chart : 

If-If Statement 

This is used to check multiple conditions for a particular problem.

Syntax :

      if(condition)
      {
          statement 1;
       }
      if(condition)
      {
          statement 2;
      }

Flow Chart :

If-Else Statement 

This statement is used with the keyword else. if condition evaluates to be true then the code inside the body of if will execute otherwise the code inside the body of else will be executed.

Syntax :

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

Flow Chart :

Nested If Statement

This uses multiple if  statements one inside another and a optional else statement.

Syntax :

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

Flow Chart :

Else-If Ladder 

This statement consist of combination of if and else statements in a ladder fashion which is used to evaluate condition and execute a given specific code.

Syntax :

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

Flow Chart :

Switch Statement

This statement is an alternative to if-else ladder statement and mostly used in menu driven program where a given condition or a variable is matched with the different cases and depending upon the case a given case code gets executed.

Syntax :

switch(condition/variable)
{
case 1:

case 2:

default :

}

Note : We use break statement in order to solve the problem of fall through. Fall Through is a condition in which more than one cases code is executed.

Flow Chart :


Example To illustrate Different If Statements

WAP(Write a program)  to find greatest of three numbers ?

Using Nested IF..

  1. #include <stdio.h>
  2. int main() {
  3. double n1, n2, n3;
  4. printf("Enter three numbers: ");
  5. scanf("%lf %lf %lf", &n1, &n2, &n3);
  6. if (n1 >= n2) {
  7. if (n1 >= n3)
  8. printf("%.2lf is the largest number.", n1);
  9. else
  10. printf("%.2lf is the largest number.", n3);
  11. } else {
  12. if (n2 >= n3)
  13. printf("%.2lf is the largest number.", n2);
  14. else
  15. printf("%.2lf is the largest number.", n3);
  16. }
  17. return 0
  18. }

Using If-Else Ladder

  1. #include <stdio.h>
  2. int main() {
  3. double n1, n2, n3;
  4. printf("Enter three numbers: ");
  5. scanf("%lf %lf %lf", &n1, &n2, &n3);
  6. if (n1 >= n2 && n1 >= n3)
  7. printf("%.2lf is the largest number.", n1);
  8. else if (n2 >= n1 && n2 >= n3)
  9. printf("%.2lf is the largest number.", n2);
  10. else
  11. printf("%.2lf is the largest number.", n3);
  12. return 0;
  13. }

Using If Statement

  1. #include <stdio.h>
  2. int main() {
  3. double n1, n2, n3;
  4. printf("Enter three different numbers: ");
  5. scanf("%lf %lf %lf", &n1, &n2, &n3);
  6. if (n1 >= n2 && n1 >= n3)
  7. printf("%.2f is the largest number.", n1);
  8. if (n2 >= n1 && n2 >= n3)
  9. printf("%.2f is the largest number.", n2);
  10. if (n3 >= n1 && n3 >= n2)
  11. printf("%.2f is the largest number.", n3);
  12. return 0;
  13. }

Thank You So Much.. I will cover different iterative statements in my next post.

For Technology related stuff's visit my youtube channel and subscribe that channel
for getting technology related updates and to learn different technology.

Comments

Popular posts from this blog

Coding tips for Developers

Master Data Structure & Algorithms in Python

How to Start with Coding: A Beginner's Guide