Looping and Branching

The While Loop

The while loop is an entry controlled loop statement, means the condition is evaluated first and it is true, then the body of the loop is executed. After executing the body of the loop, the condition is once again evaluated and if it is true, the body is executed once again, the process of repeated execution of the body of the loop continues until the condition finally becomes false and the control is transferred out of the loop.

Example : Program to give maximum and minimum value of character  datatype.

/* Program gives maximum and minimum value of character datatype */
#include<stdio.h>
#include<conio.h>
void main()
{
   char i,j ;   /* Local definitions */
   /* Statements */
    clrscr();
    i = 1;
    while (i > 0)                                                                               
                    {
                         j = i;                                                                           
                         i++;                                                                            
                     }  /* while */
printf ("The maximum value of char is n",j);
printf ("The value of  char after overflow is n",i);
getch();
}  /* main */
  
Output:
The maximum value of char is 127
The value of  char after overflow is -128

Do-While Loop

It is also repetitive control structure and executes the body of the loop once irrespective of the condition, then it checks the condition and continues the execution until the condition becomes false.

 The statements with in the body of the loop is executed once, then it evaluates for the condition, if it is true, then it executes body until the condition becomes false.

Example : Program to print n numbers using do..while loop.

/* Program to print n numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;     /* Local definitions */
/* Statements */
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
i = 0;
do                                                                                                       
   {
      printf("the numbers are %d \n",i);
      i = i +1;
    }        /* do..while */
while( i<n)  ;
getch();
}   /* main */

Output:
                        Enter the number : 6
                        the numbers are 0
                        the numbers are 1
                        the numbers are 2
                        the numbers are 3
                        the numbers are 4
                        the numbers are 5

The for loop:

The for loop is another repetitive control structure, and is used to execute set of instructions repeatedly until the condition becomes false.

The assignment, incrementation or decrementation and condition checking is done in for statement only, where as other control structures are not offered all these features in one statement.

Example : Program to print n numbers using for..loop structure.

/* Program to print n numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;  /* Local definitions */
/* Statements */
clrscr();
printf("Enter a number :");
scanf("%d",&n);
for(i = 0; i<n; i= i+1)
            {
            printf("The numbers are %d \n",i);
            }          /* for */
            getch();
}/* main */

Output:
                        Enter a number :5
                        The numbers are 0
                        The numbers are 1
                        The numbers are 2
                        The numbers are 3
                        The numbers are 4

Nesting of For loop

Like if statement for loop also nested. The loop within the loop is called nested loop. In nested for loops two or more for statements are included in the body of the loop. The number of iterations in this type of structure will be equal to the number of iterations in the outer loop multiplied by the number of iterations in the inner loop.

Example : Program to demonstrate nesting of for loops.

/* Program using nesting of for loops */
#include<stdio.h>
main()
            {         

                        int i,j;   /* Local definitions */

                        /* Statements */

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

                                    {          /* for loop starting */

                                                printf("\n");
                                                for(j=1;j<=3;j++)

                                                printf("%d\t",j);

                                    }          /* for */

            }                      /* main */
Output
            1          2          3
            1          2          3
            1          2          3

The Switch Statement

The switch statement is used to pickup or execute a particular group of statements from several available group of statements. It allows us to make a decision from the number of choices.

It is a multiway decision statement, it tests the value of given variable or expression against a list of case values and when a match is found, a block of statements associated with that case is executed.
Example :  Program to print the given number is odd/even using switch case statement.

/* Prints the given number is odd/even */

#include<stdio.h>
#include<conio.h>
void main()
            {

                        int i,n;  /* Local definitions */

            /* Statements */
            clrscr();
            printf("Enter a Number : ");                                       

            scanf("%d",&n);

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

                                    {

                                                switch(i%2)                                        

                                                {

                                                case 0 :

                                                printf("The number %d is even \n",i);            
                                                break;             
                                   
                                                case 1 :
printf("The number %d is odd \n",i);
break;
                                                }          /* switch case */
                                    }          /* for */
getch();
}          /* main */

Output:
                        Enter a Number : 7
                        The number 1 is odd
                        The number 2 is even
                        The number 3 is odd
                        The number 4 is even
                        The number 5 is odd
                        The number 6 is even
                        The number 7 is odd

Nested Switch() Case

'C' supports the nested switch( ) statements. The inner switch( ) statement can be a part of an outer switch( ) statement. The inner and outer switch( ) case constants may be same. No conflict arises even if they are same.
Example : Program to find even or odd numbers using nested switch() case.

/* Program to find even or odd numbers using nested switch() case */

#include<stdio.h>

#include<conio.h>

void main()

{

/* Local definitions */

int a,b;

/* Statements */

clrscr();

printf("Enter a number:");

scanf("%d",&a);

switch(a)

                  {          /* switch case1 open */

      case 0:

      printf("The number is even.\n");

      break;

      case 1:

      printf("The number is odd.\n");

      break;

      default:

      b=a%2;

      switch(b)
     
                              {                      /* Switch case2 open */

                              case 0:

                              printf("The number is even.\n");

                              break;

                              default:

                              printf("The number is odd.\n");

                  }                      /* Switch2 */

  }              /* Switch1 */

getch();

}          /* main */

Output: 1
                        Enter a number:89
                        The number is odd.

            Output: 2
                        Enter a number:48
            The number is even.

The Break Statement

The break statement is used to terminate the loop. When the keyword  break is used  inside any 'C' loop, control automatically transferred to the first statement after the loop. A break is usually associated with an if statement.
Example          : Program to print the number upto 5 using break statement.

/* Program using break statement */

#include<stdio.h>

#include<conio.h>

main ()

{   

/* Local definitions */                                                                  

int i;

/* Statements */

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

      {         

      if(i==6)

      break;   /* break statement */

      printf("%d", i);

                        }          /* for */

}    /* main */

Output
                        1 2 3 4 5

The Continue Statement

In some situations, we want to take the control to the beginning of the loop, bypassing the statements inside the loop which have not yet been executed, for this purpose the continue is used. When the statement continue is encountered inside any 'C' loop control automatically passes to the beginning of the loop.

Example  : Calculate the sum of the given positive numbers.

/* Program to calculate the sum of the given positive numbers */

#include<stdio.h>

main

{   
            int i,n,sum=0;  /* Local definitions */
     
      /* Statements */
            for(i=1;i<=5;i++)

                  {         

      printf("Enter any number.....\n");
            scanf("%d",&n);

      if(n<0)
      continue;

      else
      sum=sum+n;

      }          /* for */

printf("Sum is .....%d",sum);

}          /* main */

Output
                        Enter any number.....10
                        Enter any number.....5
                        Enter any number.....15
                        Enter any number.....25
                        Enter any number.....-10
                        Enter any number.....50
                        Sum is .....105

Goto Statement

'C' provides the goto statement to transfer control unconditionally from one place to another place in the program.

A goto statement can cause program control almost anywhere in the program unconditionally.

The goto statement requires a label to identify the place to move the execution. A label is a valid variable name and must be ended with colon (:).

Example :
Program to prints the given both number is equal or not.

/* Program using goto statement */
#include<stdio.h>
#include<conio.h>
main ()

{         
/* Local definitions */
            int a, b;

/* Statements */
            printf("\nEnter the numbers");
            scanf("%d %d",&a,&b);
            if(a==b)
            goto equal;
            else

      {
                                    printf("\n A and B are not equal");
                                    exit(0);

      }          /* if else */

equal:        
printf("A and B are equal");
}          /* main */

Output :

            1.         Enter the numbers  3,  3
                                    A and B are equal
            2.         Enter the numbers  3,  4
                                    A and B are not equal


No comments:

Post a Comment

Copyright © EduRAR @ www.edurar.blogspot.in