if, if...else and switch statements in C with samples
By Siva Viewed: 31747 times Emailed: 194 times Printed: 180 times
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:
- || [logical OR]
- & & [logical AND]
- ! [logical NOT]
[ || ]OR Logical Operator
Condition-I
--------------
True
False
False
True
[& &]AND Logical Operator
Condition-I
--------------
True
False
True
False
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. |
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 |
Most Viewed Articles (in last 30 days)

