Pages

Tuesday, 15 October 2013

Conditional Statements in C Language

Conditional Statements or Decision Making Statements or Conditional Branching Statements

1)     The statements are executed based on the condition .

2)     If the condition is true then the statements are executed .

3)     If the condition is false then the statements are not executed .

C & C++ supports following types of conditional statements.

1)     If
2)     If else
3)     If else if / else if /else if lader
4)     Nested if


1. ‘if’ Statement :

Syntax for ‘if ‘ statement:

 If the condition is true, the control enters into the if block and executes statements in the block .

If the condition is false, the control cannot enter into the if block and does not executes statements in the block .

/*C program to check eligibility for vote*/

Program
#include<stdio.h>
#include<conio.h>
Void main()
{
int vage;
clrscr();
printf(“\nEnter the age:”);
scanf(“%d”,&vage);
If(vage>=18)
{
printf(“\nHe/She is eligible for vote.”);
}
getch();
}

Out put:

Input:

Enter the age: 23

Output:

He / She is eligible for vote

2. ‘if else’ statement:

Syntax for ‘if else’ statement:

If the condition is true,the control enters into the if block and executes statements in the block .

If the condition is false,the control enters into else block and executes statements in the block .

Note:

For one if block there must be one else block .

No statements are allowed between if and else .

/*C program to check eligibility for vote*/

Program
#include<stdio.h>
#include<conio.h>
Void main()
{
int vage;
clrscr();
printf(“\nEnter the age:”);
scanf(“%d”,&vage);
If(vage>=18)
{
printf(“\nHe/She is eligible for vote.”);
}
else
{
printf(“\nHe/She is not eligible for vote.”);
}
getch();
}

Out put:

Input:

Enter the age: 13

Output:

He / She is not eligible for vote

3. If else if / else if / else if ladder Statement:

Syntax for ‘if else if’ statement:


In this type of conditional statements the execution is very fast because

If the 1st condition is true then it cannot check the 2nd condition

If the 2nd condition is true then it cannot check the 3rd condition

this process continues till the end of the block.

/*C  program to find the greatest number of given three numbers*/

Program
#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b,c;
clrscr();
printf(“\n enter the values of a,b,c”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
{
printf(“\n %d is greater”,a);
}
else if(b>c)
{
printf(“\n %d is greater”,b);
}
else
{
printf(“\n %d is greater”,c);
}
getch();
}

Out put:

Input:

Enter the values of a, b, c: 10
67
78

Output:

78 is greater

4. “nested if” Statement :

Syntax for “nested if”:


If inside another if is known as nested if.

/*C  program to display student result*/

Program
#include<stdio.h>
#include<conio.h>
Void main()
{
int sno,m1,m2,m3,tot;
float avg;
clrscr();
printf("enter sno,m1,m2,m3:");
scanf("%d%d%d%d",&sno,&m1,&m2,&m3);
tot=m1+m2+m3;
avg=tot/3;
printf("\nsno=%d",sno);
printf("\nm1=%d",m1);
printf("\nm2=%d",m2);
printf("\nm3=%d",m3);
printf("\ntotal=%d",tot);
printf("\navg=%0.2f",avg);
if(m1>=35&&m2>=35&&m3>=35)
{
            if(avg>=70)
            {
            printf("\distinction");
            }
            else if(avg>=60)
            {
            printf("\nfirst class");
            }
            else if(avg>=50)
            {
            printf("\second class");
            }
            else if(avg>=40)
            {
            printf("\n third class");
            }
            else if(avg>=35)
            {
            printf("\n pass");
            }

}
else
{
printf("\nfail");
}
getch();
}

Out put:

Input:

enter sno,m1,m2,m3:1
96
85
42

Output:

sno=1
m1=96
m2=85
m3=42
total=223
avg=74.00
distinction

Switch statement :

Syntax:

If the expression value matches with any case const value then the control executes that case statements.

If the expression value not matches with any case const value then the control executes default statements.

Default can be placed any where in the switch block but in general we place it at the end of block.

Switch statements expression values can take either int or char but not take float and double.

Switch statements does not support duplication of case.

Break:-It is used to exit from the switch statement or innermost of the loop.

/*C program to demonstrate switch statement*/

Program
#include<stdio.h>
#include<conio.h>
Void main()
{
int rno;
clrscr();
printf(“\nEnter the student Roll.No:”);
scanf(“%d”,&rno);
switch(rno)
{
case 1:
             printf(“\nabc”);
            break;
case 2:
             printf(“\nxyz”);
            break;
case 3:
             printf(“\nmno”);
            break;
case 4:
             printf(“\npqr”);
            break;
default:
             printf(“\nInvalid Roll num….try again!!”);
            break;
}
getch();
}

Out put:

Input:

Enter the student Roll.No: 3

Output:

mno

No comments:

Post a Comment