Control Statements

Control Statements

7 | Control Statements


7.0 | Contents :

     1.   if statement
     2.   if...else statement
     3.   Nested-if statement
     4.   swicth statement
     5.   while loop
     6.   do-while loop
     7.   for loop
     8.   break statement
     9.   continue statement


7.1 | if statement :

Syntax :
          if(test ondition)
            {
              statement block(s);
            }
              next statement;


     Here, statement block(s) will be executed if and only if when the test condition is true, otherwise execution is directly starts from the next statement.



Example : Write a C program to find the student is passed or not

Input :

#include<stdio.h>

int main()
  {
    float percent;

    clrscr();

    printf("Enter percentage you obtained :\n");
    scanf("%f",&percent);

    if(percent >= 35)
      {
        printf("\nCongratulations...you are passed with %f percentage.",percent);
      }

    getch();
    return 0;
  }


Output :

Enter percentage you obtained :
90

Congratulations...you are passed with 90.000000 percentage.


     Here, in above program, the sentence Congratulations...you are passed with %f(student's percentage) percentage will display on screen if and only if percentage of student is greater than equal to 35.


7.2 | if...else statement :

Syntax :
          if(test condition)
             {
               true statement block(s);
             }
           else
             {
               false statement block(s);
             }


     Here, if test condition is true, then true statement block(s) is executed, but if test condition is false, then false statement block(s) is executed.



Example : Write a C program to find two entered numbers are same or not

Input :

#include<stdio.h>

int main()
  {
    int x,y;

    clrscr();

    printf("Enter a number :\n");
    scanf("%d",&x);

    printf("Enter a number :\n");
    scanf("%d",&y);

    if(x==y)
      {
        printf("\nBoth the entered numbers are same.\n");
      }
    else
      {
        printf("\nBoth the entered numbers are different from each other.\n");
      }

    getch();
    return 0;
  }


Output :

Enter a number :
65
Enter a number :
45

Both the entered numbers are different from each other.


     Here, if both the numbers entered by the user are same, then Both the entered numbers are same this statement will dispaly on screen, but if entered numbers are different, then Both the entered numbers are different from each other this statement will display on the screen.


7.3 | Nested-if statement :

Syntax :
          if(test condition 1)
             {
               if(test condition 2)
                 {
                   statement block 1;
                 }
               else
                 {
                   statement block 2;
                 }
             }
           else
             {
               statement block 3;
             }


     Here, first of all, computer checks the test condition 1, if it is true then computer checks the test condition 2, if it is also true, then computer execute statement block 1 as an output; but if test conditon 2 is false, then computer execute statement block 2 as an output. But if test condition 1 is false, then computer directly execute the statement block 3.



Example : Write a C program to recommend laptops according to budget

Input :

#include<stdio.h>

int main()
  {
    int a;

    clrscr();

    printf("Enter your budget in INR :\n");
    scanf("%d",&a);

    if(a>50000)
      {
        if(a>80000)
          {
            printf("\nYou can buy Intel core i9 or gaming laptops.");
          }
        else
          {
            printf("\nYou can buy Intel core i7 laptops or cheaper MacBooks.");
          }
      }
    else
      {
        printf("\nYou can buy Intel core i3 OR i5 laptops for daily usage.");
      }

    getch();
    return 0;
  }


Output :

Enter your budget in INR :
75000

You can buy Intel core i7 laptops or cheaper MacBooks.


     Here, if budget entered by user is greater than 80000 INR, then You can buy Intel core i9 or gaming laptops this statement will execute and if budget entered by user is greater than 50000 INR but less than 80000 INR, then You can buy Intel core i7 laptops or cheaper MacBooks this statement will execute and if budget entered by user is less than 50000 INR, then computer directrly execute You can buy Intel core i3 OR i5 laptops for daily usage this statement.


7.4 | swicth statement :

Syntax :
          switch(expression)
             {
               case lable 1:
               statement block 1;
               break;

               case label 2:
               statement block 1;
               break;

               .................................
               .................................

               case lable n:
               statement block n;
               break;

               default:
               default statement;
               break;
             }


     where, switch, case, default and break are keywords
                 expression is a valid C expression or codeiable
                 label 1, label 2 and label n are nothing but different values of expression

     Here, first of all, computer checks that the value of expression is equal to which label and then the satement block of that label is executed. But if no value of expression is equal to any of the label, then the default section is executed. Let, the value of expression is equal label 1, then statement block 1 is executed.



Example : Make a calculator using switch statement

Input :

#include<stdio.h>

int main()
  {
    printf("Calci :\n\n");

    float n1,n2,ans;
    char op;

    clrscr();

    printf("Enter first number : ");
    scanf("%f",&n1);

    printf("\n");

    printf("Enter the operator : ");
    scanf(" %c",&op);

    printf("\n");

    printf("Enter second number : ");
    scanf("%f",&n2);

    printf("\n");

    switch(op)
      {
        case '+':
        ans=n1+n2;
        printf("\nAddition of %f and %f is %f\n",n1,n2,ans);
        break;

        case '-':
        ans=n1-n2;
        printf("\nSubtraction of %f and %f is %f\n",n1,n2,ans);
        break;

        case '*':
        ans=n1*n2;
        printf("\nMultiplication of %f and %f is %f\n",n1,n2,ans);
        break;

        case '/':
        ans=n1/n2;
        printf("\nDivision of %f and %f is %f\n",n1,n2,ans);
        break;

        default :
        printf("\nYou entered an incorrect value/operator.\n");
        break;
      }

    getch();
    return 0;
  }


Output :

Calci :

Enter first number : 76.234

Enter the operator : *

Enter second number : 45.657


Multiplication of 76.234 and 45.657 is 3480.615967


7.5 | while loop :

Syntax :
          while(test condition)
             {
               //body of the loop
             }
           next statement;


     Here, the computer first evaluates the test condition, if it is true, then the body of the loop is executed again and again until test condition becomes false. And if test condition is false, then next statement is executed.



Example : Write a C program to display numbers from 1 to entered number

Input :

#include<stdio.h>

int main()
  {
    int a,i=1;

    clrscr();

    printf("Enter a number :\n");
    scanf("%d",&a);

    printf("\n");

    while(i<=a)
      {
        printf("%d\n",i);
        i++;
      }

    getch();
    return 0;
  }


Output :

Enter a number :
5

1
2
3
4
5


     Here, let the value entered by user is 5 i.e. the value of a is equal to 5. Now, initial value of i is 1. The body of the loop is executed again and again until the value of i will equal to the value of a (i.e. 5). So the loop is executed 5 times and in each time of execution, the value of i is increased by 1. And after 5 times of execution of the loop, the value of i reached at 5 i.e. equal to the value of a and now according to test condition (i.e. i<=a) execution of the loop is stopped.


7.6 | do-while loop :

Syntax :
          do
             {
               //body of the loop
             }
           while(test condition);
           next statement;


     Here, the computer first execute body of the loop and then verify test condition. If verified test condition is true, then body of the loop is executed again. Body of the loop is executed again and again until the test condition becomes false. And if test condition is false, then the computer execute next statement.



Example : Write a C program to display numbers from 1 to entered number

Input :

#include<stdio.h>

int main()
  {
    int a,i=1;

    clrscr();

    printf("Enter a natural number :\n");
    scanf("%d",&a);

    printf("\n");

    do
      {
        printf("%d\n",i);
        i++;
      }
    while(i<=a);

    getch();
    return 0;
  }


Output :

Enter a natural number :
5

1
2
3
4
5


     Here, let the value entered by user is 5 i.e. the value of a is equal to 5. Now, initial value of i is 1. The body of the loop is executed again and again until the value of i will equal to the value of a (i.e. 5). So, the loop is executed 5 times and in each time of execution, the value of i is increased by 1. And after 5 times of execution of the loop, the value of i reached at 5 i.e. equal to the value of a and now according to test condition (i.e. i<=a) execution of the loop is stopped.


7.7 | for loop :

Syntax :
          for(initialization;test_condition;incredecrement)
             {
               //body of the loop
             }
           next statement;


     Here, computer checks the test condition and if it is true, then computer execute the body of the loop and increase the value of incredecrement expression by 1. Computer do this again and again until the test condition becomes false. And then computer execute the next statement.



Example : Write a C program to display numbers from 1 to entered number

Input :

#include<stdio.h>

int main()
  {
    int a,i=1;

    clrscr();

    printf("Enter a natural number :\n");
    scanf("%d",&a);

    printf("\n");

    for(i=1;i<=a;i++)
      {
        printf("%d\n",i);
      }

    getch();
    return 0;
  }


Output :

Enter a natural number :
5

1
2
3
4
5


     Here, let the value entered by user is 5 i.e. the value of a is equal to 5. Now, initial value of i is 1. The body of the loop is executed again and again until the value of i will equal to the value of a (i.e. 5). So the loop is executed 5 times and in each time of execution, the value of i is increased by 1. And after 5 times of execution of the loop, the value of i reached at 5 i.e. equal to the value of a and now according to test condition (i.e. i<=a) execution of the loop is stopped.


7.8 | break statement :

Syntax :
          break;

     The break statement is used to terminate a loop while the test condition is true.


Example : Write a C program to display natural numbers from 1 to 10 using for loop, but terminate the program at natural number 5 using break keyword

Input :

#include<stdio.h>

int main()
  {
    int i=1;

    clrscr();

    for(i=1;i<=10;i++)
      {
        if(i==6)
          break;

        else
          {
            printf("%d\n",i);
          }
      }

    getch();
    return 0;
  }


Output :

1
2
3
4
5


     Here, the loop execute 5 times (i.e. until the value of i becomes 5) and after that the execution of the loop will stop. Because we used break keyword at i is equal to 6 (i.e. i==6), which means if the value of i becomes 6, then execution of the loop will stop.


7.9 | continue statement :

Syntax :
          continue;

     The continue statement is used to skip a step.


Example : Write a C program to display natural numbers from 1 to 10 using for loop, but skip/don't display the natural number 5 using continue keywordr

Input :

#include<stdio.h>

int main()
  {
    int i=1;

    clrscr();

    for(i=1;i<=10;i++)
      {
        if(i==5)
          continue;

        else
          {
            printf("%d\n",i);
          }
        }

    getch();
    return 0;
  }


Output :

1
2
3
4
6
7
8
9
10


     Here, the program will display natural numbers from 1 to 10 except 5 on the output screen.




Comments