Control Statement

By Bipin Tiwari

 Important Questions

The statements which alter flow of execution of a program are known as control statements.

There are two types of control statements: Decision Making & Repeating construct

             Decision Making Statements             Loop Statements or Repeating construct

if statements                                                                        for loop

if.. else statements                                                           while loop

if.. else  if  statements                                                   do.. while loop

Nested if.. else statement

switch statement

 Decision Making Statements

i.  if  Statements

An if  statements is used to control the flow of execution of statements. The if statement first evaluates an expression  and then, it executes the statements within its block if the value of the evaluated expression is true.

 

if(test_ expression)
{
  statements-block;
}

Example 1

Write a program to read a number from user and test whether the number is negative [Show message “The number is negative ” if it is negative number otherwise show nothing].

#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter a number to be tested:");
scanf("%d", &num);
if(num<0)
  printf("The number is negative");
getch();
return 0;
}

Output
Enter a number to be tested:-6
The number is negative 

 Nested if  Statement

When one if statement is written within body of another if statement, it is called nested if statement if statement. The  several if statements shall be declared as nested if statements.

         Syntax

if(expression)
{
 if(expression)
{
//body
  }
 }

 Example

Write a program to read percentage of marks obtained by a student in SLC and +2 level, Display message “Congratulation!! You have first division in both SLC and +2” if both levels have percentage greater than or equal to 60.

#include<Stdio.h>

#include<conio.h>

int main()

{

float SLC_ per, plus2_ per; ;

print f("Enter percentage of SLC:");

scan f("%f", &SLC_ per)

print f("Enter percentage in +2 in science:");

scan f("%f", &plus2_per);

if(SLC_ per>=60)

  {

     if(plus2_per>=60)

   {

       print f("Congratulation!!");

       print f("\n You have first division in both SLC and +2.");

  }

    }

getch () ;

return 0;

}

 Output

 Enter percentage of SLC: 70

 Enter percentage in +2 in science: 60

 Congratulation!!!!!

You have  first division in both SLC and +2.

 

if…. else Statement

The if.. else statement is an extension of the simple if statement . It is used when there are only two possible actions one happens when a test condition is true, and the other when it is false.

if(test_ expression)
  {
    true-block statement (s);
   }
  else
 {   
   false-block statement (s);
 }

Example

Write a program to read a number from user and determine whether the number is even or odd.

#include<stdio.h>

#include<conio.h>

int main()

{

int num, remainder;

print f("Enter a number:");

scan f("%d", &num);

remainder = num % 2;

                     // Modulo division

if (remainder == 0)

   {

     print f("The number is even");

     }

  else 

   {

   print f("The number is odd");

  }

 getch();

 return 0;

  }

Output

Enter a number : 90

The number is : even

Enter a number: 17

The number is odd

 

Nested if.. else Statement

Similar to nested if  statement if.. else statements shall also be written inside the body of another if.. else body called nested if.. else statement.

if(condition)
  {
     if(condition2)
        {
           statement-1;
            }
            else
          {
            statement-2;
             }
       }
      else
     {
      if(condition3)
           {
               statement-3;
                  }
           else
          {
            statement-4;
          }
    }

Example

Write a program to read three number from user and determine the largest number among them.

int main()

  int n1, n2, n3;

  print f("Enter 3 numbers:");

  scan f ("%d%d%d", &n1 , &n2  , &n3);

if (n1>n2)

{

  if(n1>n3)

    print f("Largest = %d , n1);

  else 

    print f("Largest = %d", n3);

    }

  else

  {

   if (n2>n3)

    print f("Largest = %d , n2);

   else

     print f("Largest = %d, n3);

    }

  getch();

  return 0;

}

Output

Enter 3 number : 100  50  10

Largest = 100

Enter 3 number : 50 100 10100

Largest = 100

 

if…. else if Statement

An if… else  if  statement is used when there are more than two possible actions depending upon the outcome of test expression. When an action is taken, no others can be executed or taken. In such situation, if…. else if… else  if.. else structure is used.

if(condition)
 {
  statement-1;
  }
 else if(condition2)
 {
  statement-2;
 }
  else if(condition3)
  {
   statement-3;
  }
  else if(condition n)
  {
  statement-n;
  }
  else 
   {
     default-statement;
  }
 statement-x;

Example

Write a program that reads total marks of a student in seven subjects. Then calculate the percentage and determine the division. Use following conditions:

  1. Percentage greater than or equal to 80- distinction,
  2. Percentage between 60 and 79- First Division
  3. Percentage between 45 and 59-second division
  4. Percentage between 32 and 44  – third division
  5. Percentage less than or equal to 31 -fail.
#include<stdio.h>

#include<conio.h>

int main()

  {

     float nep, eng, math, phy, chem, bio, comp, percent;

    print f("Enter the marks in 7 subjects:");

   scan f("%f%f%f%f%f%f%f", &nep, &eng, &math, &phy, &chem, &bio, &comp);
  percent =(nep+ eng + math +phy + chem + bio + comp) ;

   if(percent>= 80)

     print f("Distinction");

else if(percent >=60 && percent < 80)

     print f ("First Division");

else if (percent>=45 && percent <60)

     print f("Second Division")

else if (percent >=32 && percent <45)

    print f("Third Division");

  else

     print f("Fail");

      print f("\n Your percentage is: %f", percent);

      getch();

   return 0;

  }

Output:

i. Enter the marks in 7 subjects: 45  78  89  35  76  80  35

   First Division

   Your percentage is :62.5714286

ii. Enter the marks in 7 subjects: 20  10  32  25  24  35  40

   Fail

   Your percentage is: 26.5714286

 

Loop or Iteration or Repeating Construct

Types of Loop

  1. for loop
  2. while loop
  3. do-while loop

i. for Loop

for(counter_ initialization; test_ condition; increment or decrement)
    {
      statements; or body of loop
     }

Example

Write a program to calculate factorial of a number.

#include<stdio.h>

#include<conio.h>

int main()

{

 int num, i;

long fact=1;

print f("\n Enter a number whose factorial is to be calculated:");

scan f("%d", &num);

for(i=1;i<=num;i++)

   fact*= i;

print f("\n The factorial is :%d", fact);

getch();

return 0;

}

Output

Enter a number whose factorial is to be calculated : 5

The factorial is : 120

 

While Loop

While(test_ condition)
  {
  body of loop
   }

Example

Write a program to calculate factorial of a number using while loop.

#include<stdio.h>

#include<conio.h>

int main()

{

  int num, i=1;

long fact = 1;

print f("Enter a number whose factorial is to be calculated:");

scan f("%d", &num);

while(i<=num)

{

   fact*=i;

      i++;

  }

  print f("The factorial is:%d", fact);

 getch();

return 0;

}

 

do-while Loop

Do
{
  //statements or body of loop;
  }while(test_condition);

Example

Write a program to read a number from keyword until a zero or a negative number is keyed in. Finally, calculate sum and average of entered numbers.

#include<stdio.h>

#include<conio.h>

int main()

{

      int  num, count=0;

     float sum=0, avg;

     do

       {

             print f("\n Enter number:\t");

            scan f("%d", &num);

            sum+=num;

            count++;

                 }while(num>0);

          sum=sum-num;

             avg=(sum)/(count-1)

      print f("\n The sum is :\t%d", sum);

     print f("\n The average is: \t%f", avg);

     getch();

    return 0;

   }

 Output

Enter number: 10

Enter number: 20

Enter number:  15

Enter number: 5

Enter number: 0

The sum is:         50.000000

The average is: 12.500000

 

break Statement

The break statement terminates the execution of the loop and the control is transferred to the statement immediately following the loop.

break;

Example

What is the output of following program?

#include<stdio.h>

#include<conio.h>

int main()

{

  int i;

 for(i=1; i<10; i++)

  {

   print f("\t%d, i);

  if (i==5)

       break;

   }

  getch();

}

Output

1  2  3  4  5

 

continue Statement

The continue statement is used when we want to continue running the loop.

continue;

Example

Write a program that asks for a number n from user and then display only even numbers from 2 to n .

#include<stdio.h>

#include<conio.h>

int main()

   {

   int i, num;

     print f("\n Enter a number:");

     scan f("%d", &num);

    print f("\n The even numbers from 2 to %d are: \n", num);

    for (i=1; i<= num; i++)

       {

          if(i%2!=0)

             continue;

       print f("\t%d", i);

        }

  getch();

  return 0;

 }

Output

Enter a number: 20

The even numbers from 2 to 20 are:

  2   4  6  8  10  12  14    16   18   20

 

goto STATEMENT

The goto statement is used to after the normal sequence of program execution by unconditionally transferring control to some part of the program.

goto label;


 label: statements;

Example

Write a program to ask two numbers. Display message” Either number is negative ” if either number is negative ; otherwise display message ” Both numbers are positive”.

int main()

{

   int i, num1, num2;

  print f("Enter first number:");

  scan f("%d", &num1);

  if(num<0)

    goto negative;

print f("Enter second number:");

scan f("%d", &num2);

 if(num2<0)

  goto negative;

 print f("The both numbers are positive");

getch();

return;

negative :

   print f("Enter number is negative ");

getch();

return 0;

}

Output

a) Enter first number: 20

    Enter second number: 10

    The both numbers are positive

b) Enter first number: -5

     Either number is negative

c) Enter first number : 6

    Enter second number: -4

    Either number is negative

 

switch STATEMENT

When there are a number of options available and one of them is to be selected on the basis of some criteria, switch statement is used.

switch(variable or expression)
{
   case caseConstant1:
         statements;
          break;
 case caseConstant2:
         statements;
          break;



     default:
        statements;
  }

 

Important Questions
Comments
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments