Conditional Statements

Most of the conditional constructs available in C language.

The relational operators required for evaluating the conditions are similar to those in the other languages.

Operator

Meaning

= =

Equals

!=

Not Equals

<

Less than

>

Greater than

<=

Less than or equals

>=

Greater than or equals

 

 

 

The if...else Construct

The following program shows the use of the if....else conditional construct:

#include <stdio.h>

main()

{

  char niv;

  puts("Enter a character");

  niv=getchar();

  if (niv = = 'L')

           puts("Character is L");

  else

           puts("Character is not L");

}

 

In the above example, if  the input character is L, the message displayed is character is L, otherwise the message character is not L is displayed. Also, note that the condition has to be specified within parenthesis.

 

 

The switch...case Construct

The switch ...case Construct is used in situations where based on the value of a variable certain action is to taken.

#include <stdio.h>

main()

{

  char niv;

  puts("Enter a character in lower case");

  niv=getchar();

  switch(niv)

  {

    case 'a':

                puts("vowel a");

                break;

    case 'e':

                puts("vowel e");

                break;

    case 'i':

                puts("vowel i");

                break;

    case 'o':

                puts("vowel o");

                break;

    case 'u':

               puts("vowel u");

               break;

    default:

               puts ("The character is not a vowel");

  }

}

In this function, switch specifies the variable name whose value has to be compared with specified cases.

The switch variable may be

The case statements specify values which may be integer or character constants. They also specify the corresponding action to be taken if the value compares with the value of the switch variable.

If the break statements are not specified in the above function and the switch variable, niv, contains 'o', the display will be

vowel o

vowel u

The character is not a vowel

The default statement is optional and is used for handling situations where none of the case statements are evaluated as true .

 

The Loop Construct

There are several loop constructs available in C language.

The while Loop

The function below shows the use of the while construct in accepting an input as 'y' or 'n' only. Any other input is rejected by the function until a valid input is provided.

#include <stdio.h>

main()

{

  char inp,valid='n';

  while(valid=='n')

  {

    valid = '';

    puts("Enter y or n");

    inp=getchar();

   if(inp=='y')

         puts("Valid input");

   else if(inp=='n')

         puts("Valid input");

   else  

         puts("Invalid Data");

   }

}

In a while block of statements,

After execution of the loop body, the condition in the while statement is evaluated again. This repeats until the condition false .

 

The do...while Loop Construct

The do...while loop construct, like the while loop construct, is used in situations where the number of iterations to be performed is not known. Also, iterations are to be performed until the specified loop condition becomes false.

#include <stdio.h>

/* function to generate a menu, to accept and display */

/* choice */

/* using do...while loop construct */

main()

{

  char chc,ok;

  do      /* beginning of do...while block */

  {

   ok = '';

   puts("Menu");

   puts("1.Create Directory");

   puts("2.Delete Directory");

   puts("3.Show Directory");

   puts("4.Exit");

   puts("your choice");

   chc=getchar();

  }while(ok=='n');     /* end of do...while block */

}

 

The intialisation ,ok= 'n', is required, to enter a while loop, whereas this step is not required in the function that uses the do...while construct.

In this body has to be executed at least once before the loop condition has to be evaluated .

 

 

Copyright © 2008 Manikkumar. All rights reserved.