if, if...else and switch statements in C with samples

By Siva Viewed: 31747 times Emailed: 194 times Printed: 180 times Bookmark and Share



If ……….else statement: 

The if..else statement is a two way branch: it means do one thing or the other. When it is executed, the condition is evaluated and if it has the value `true' (i.e. not zero) then statement1 is executed. If the condition is `false' (or zero) then statement2 is executed. The if..else construction often saves an unnecessary test from having to be made. 

   if (condition)

   {

   statements

   }

else

   {

   statements

   } 
 

Example: 

/*-------------------------------------------------------*/

/* To find whether a given year is leap or not   */ 

/*-------------------------------------------------------*/

main()

{

int y,r1,r2;

printf(“Enter a year”);

scanf(“%d”,&y);

r1=y%4;

if (r1 = = 0)

{

         r2=y%100;

         if(r2 =  = 0)

                printf(“The given year is not a leap year”);

         else

                printf(“The given year is a leap year”);

}

else

printf(“The given year is not a leap year”);

} 
 
 

Logical Operators:

Comparisons are often made in pairs or even in groups and linked together with words like OR and AND. The following are the logical operators in C: 

  1. ||  [logical OR]
  2. & & [logical AND]
  3. ! [logical NOT]

[ || ]OR Logical Operator 

Condition-I                      Condition-II                              Result

--------------                     -----------------                            ---------

True                                  False                                         True

False                                 True                                          True

False                                 False                                         False

True                                  True                                         True 
 
 

[& &]AND Logical Operator 

Condition-I                      Condition-II                              Result

--------------                     -----------------                            ---------

True                                  False                                         False

False                                 True                                          False

False                                 False                                         False

True                                  True                                         True 
 

[!] NOT Logical Operator 

!(True) => False

!(false) => True 
 

Example: 

/*--------------------------------------------------------------------------------------------*/

/* To check whether a given alphabet is capital or not (using logical operator) */ 

/*--------------------------------------------------------------------------------------------*/

main()

{

char x;

printf(“Enter an alphabet”);

scanf(“%c”,&x);

if (x >= 65 && x <=90)

      printf(“The given alphabet is capital”):

else

      printf(“The given alphabet is not capital”):

} 
 
 
Switch Statement:

The switch statement is another way of making a program path branch into lots of different limbs. The switch statement has the following form:  
 

switch (integer value or constant value) 

   {

   case 1:  statement1;

            break;                /* optional line */ 

   case 2:  statement2;

            break;                /* optional line */ 

   .... 

   default: default statement

            break;                 /* optional line */

   } 
 

Example:

/* Input a direction code and print the direction name(using switch)*/

main()

{

char y;

clrscr();

printf(“Enter the code”);

scanf(“%c”,&y);

switch (y)

{

  case ‘N’ : printf(“North Direction”);

                   break;

  case ‘S’ : printf(“South Direction”);

                   break;

  case ‘E’ : printf(“East Direction”);

                   break;

  default : printf(“West Direction”);

                   break;

}




Comments(1)


1. Good examples for beginners.


By: Sagar at 2009-10-16 01:04:46

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-04-16]Calculator in C
[2010-04-16]Passing double value to a function in C
[2010-04-16]Passing pointer to a function in C
[2009-03-21]Infix to Prefix And Postfix in C
[2008-09-16]while, do while and for loops in C
[2008-08-13]Unicode and UTF-8 in C
[2008-08-06]Formatting with printf in C
[2008-08-06]if, if...else and switch statements in C with samples
[2008-07-31]Statements in C
[2008-07-08]Writing The First C program
[2008-07-05]The C Character Set
[2007-10-03]Using malloc() Function in C
[2007-10-03]Using calloc() Function in C
[2007-10-03]Using realloc() Function in C
[2007-10-03]Using free() Function in C

More Latest News

Most Viewed Articles (in last 30 days)
Using memset(), memcpy(), and memmove() in C
scanf and sscanf sample program in C
Using free() Function in C
Using realloc() Function in C
assert() Function Example program in C
perror() Function - example program in C
fgets(), fputs() - Line Input and Output - sample program in C
Using calloc() Function in C
The C Character Set
lseek() sample program in C
Arrays sample program in C
UNIX read and write system calls sample program in C
Binary Tree - (Self-referential Structures) example program in C
union example program in C
Infix to Prefix And Postfix in C
Most Emailed Articles (in last 30 days)
Using memset(), memcpy(), and memmove() in C
Using calloc() Function in C
Using realloc() Function in C
Using free() Function in C
Using malloc() Function in C
fgets(), fputs() - Line Input and Output - sample program in C
Using the qsort() and bsearch() functions with values - example program in C
getch and ungetch in C
The Birth and history of C Programming Language
Arrays sample program in C
assert() Function Example program in C
Binary Tree - (Self-referential Structures) example program in C
Using Shift Operators in C
perror() Function - example program in C
lseek() sample program in C