Search This Blog


Sunday, September 22, 2019

Control statement /structure


This statement is used to control the flow of execution of statements in a program.

There are three different types of control statements :-

1.       Decision making statement/ conditional statement / selection statements
2.       Looping statements / Iterative statements
3.       Jumping statements / Ad-hoc statements
Decision making stmt.
Looping/Iterative stmt.
Jumping stmt.
-          if…else
-          switch
-          while
-          do..while
-          for
-          break
-          continue
-          goto

Control structure in c
Control statement in C


Decision making statement :-

In this statement we can take a decision for executing block of statements. In this case condition is checked. If the condition is true then true part will execute otherwise false part will execute.

There are five types of decision making statements .

Ø  Simple if stmts/ single alternative if stmt
Ø  if-else stmt / double alternative if stmt
Ø  if-else if stmt / else if ladder stmt
Ø  nested if stmt / compound if stmt
Ø  switch case stmt

Types of decision making statements
Types of decision making statements in C

        I.            Simple if statement :-

In this statement if condition is true then true part will execute otherwise no action will generate .
syntax:   
if(condition)

eg:-

          if(n%2==0)
       printf(“even no.”);
      if( n%2 != 0) or (m%2 == 1)
      printf(“odd no.”);

Decisions making statements in c
Simple if statement 
  


      II.            If-else statement :-

In this statement if condition is true then true part will execute otherwise false part will execute.

syntax:   

if(condition)
 {
   block of stmts;
   }
  else
  {
    block of stmts;
   }
Decision making statement in c
If else statement 

eg:-

if(n>0)
{
   printf( “positive number”);
 }
else
{
 printf(“ negative number”);
 }

 eg2:-

if(n%2 == 0)
{
  printf( “ even “);
}
 else
 {
  printf( “ odd “ );
}

    III.            if-else if statement:-

It is generally used to specify more than one condition . In this case if condition is true then true part will execute after that terminate from if statement , otherwise next condition is checked.

syntax:

if(con dition)
  {
   block of stmts;
    }
  else if ( condition 2 )
{
  block of stmts ;
      }
……..
……..
……..
else if ( condition n)
{
   block of stmts;
}
else
{
  block of stmts;
}  
Decision making statement in c
If else if statement 

eg:-

 p= 85;
if(p>90)
  printf(“ excellent “);
else if( p<90 && p>= 75)
  printf(“ very good “);
else if ( p<75 && p>=60)
  printf(“ good “);
else
   printf( “poor “);

        OR:
if(p>=90)
printf(“ excellent “);
else if (p >= 75)
 printf( “ very good “ );
else if( p> =60)
 printf( “good “);
else
printf(“poor”);

    IV.            Nested if statement :-

When one if statement is defined within another if statement called nested if.
In this case one is called outer if and another is called inner if.

syntax:

if (condition)
{
 if (condition)
 {
   block of stmts;
 } 
  else
   {
     block of stmts;
      }
  else
   {
     if(condition)
       {
          block of stmts;
        }
       else
     {
       block of stmts;
        }
      }
Decision making statement in c
Nested if statement 

eg:-

n= -10
if ( n%2 == 0)
 {
  if (n > 0)
    printf( “ +ve even no.” );
    else
     printf( “ –ve even no. “);
   }
else
   {
     if (n> 0)
    printf( “ +ve odd “ );
    else
    {
      printf( “ –ve odd “ );
   }

We will practice on related programs on the next blog.

No comments:

Post a Comment