Till now we have been dealing with sequential execution i.e. every program in the statement could be executed sequentially ( i.e. A statement can be only executed only after the execution of its previous statements )
However many times we need to control the sequence of execution of instructions for that purpose we have to use control structures.
Control structures in ' c ' are classified into:
DECISION CONTROL STRUCTURES:
1)" If " statement :
Syntax 1: if ( condition )
Statement ; single statement body
Syntax 2: if ( condition )
{
Statement ;
....... compound statement body
Statement ;
}
Example :
/* Program to illustrate If statement */
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,big ;
clrscr ( )
printf(" enter a,b,c values ");
scanf(" %d%d%d",&a,&b,&c );
big = a;
if ( b > big )
big = b;
if ( c > big )
big = c;
printf("The largest of 3 numbers is %d ",big );
getch( )
}
2)" If Else " statement :
syntax 1: if (condition)
Statement; Single statement
else body
Statement;
syntax 2: if (condition)
{
Statement 1; Compound statement
Statement 2;. body
}
else
{
Statement 3;
Statement 4;
}
/*Program to illustrate if else statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
printf(" enter any number ");
scanf("%d",n);
if (n%2==0)
{
printf(" the entered num is even number ");
}
else
{
printf(" the entered num is odd number ");
}
getch( );
}
Example :
---> Write a ' c ' program to illustrate whether the given number is zero or non-zero.
/*Program to illustrate if else statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
printf("enter any number");
scanf("%d",&n);
if ( n == 0 )
{
printf(" the entered number is zero ");
}
else
{
printf("the entered number is non-zero");
}
getch( );
}
Example :
--> Write a ' c ' program to check whether the entered number is +ve or -ve.
/*Program to illustrate if else statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int n;
clrscr( );
printf(" enter any number ");
scanf(" %d",& n);
if (n>0)
{
printf(" entered num is +ve ");
}
else
{
pf(" entered num is -ve ");
}
getch( )
}
3) "Nested If Else" statement :
syntax: if ( condition 1 )
{ - open
if ( condition 2 )
{ - open 1
statement ; 1
statement ;
} - close 1
else
{ - open 2
statement ; 2
statement ;
} - close 2
} - close
else
{ - open
statement ; 3
statement ;
} - close
}
Example :
Write a Program to illustrate nested if else
#include<stdio.h>
#include<conio.h>
void main()
{
int age,exp;
clrscr();
printf("enter age");
scanf("%d",&age);
printf("enter experience");
scanf("%d",&exp);
if((age>=23) && (age < 30))
{
if (exp>=2)
{
printf("candidate is with 2 years experience");
}
else
{
printf("candidate is not having 2 years experience");
}
}
else
{
printf("age limit should be in the specified limit");
}
getch();
}
Example :
Write a Program to illustrate nested if else
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr( );
printf("enter values for a,b,c");
scanf("%d%d%d",&a,&b,&c);
if((a>b) && (a>c))
{
if(a>c)
printf("%d is the largest number", a);
else
printf("%d is the largest number", c);
}
else
{
if (b>c)
printf("%d is the largest number", b);
else
printf("%d is the largest number", c);
}
getch();
}
4)"Else If Ladder" statement:
Syntax: if ( condition 1)
Statement 1;
else if ( condition 2)
Statement 2;
else if ( condition 3)
Statement 3;
......
Default
Statement n;
Example :
Write a program to illustrate else if ladder statement.
/*Program to illustrate else if ladder statement*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int m;
clrscr( );
printf("enter the marks :");
scanf("%d",&m);
if((m>90)&&(m<100))
printf("mark grade is O");
else if((m>80)&&(m<90))
printf("mark grade is A");
else if((m>70)&&(m<80))
printf("mark grade is B");
else if((m>60)&&(m<70))
printf("mark grade is C");
else if((m>50)&&(m<60))
printf("mark grade is D");
else if((m>40)&&(m<50))
printf("mark grade is E");
else
printf(" FAIL " );
getch( );
}
5) SWITCH Statement:
Example :
Write a program to illustrate Switch case statement.
/* Program to illustrate Switch case statement */
#include<stdio.h>
void main ( )
{
int n;
clrscr ( ) ;
printf("enter the number") ;
scanf("%d", &n) ;
switch (n)
{
case 1: printf (" Monday ");
break;
case 2 : printf (" Tuesday ");
break;
case 3 : printf (" Wednesday ");
break;
case 4: printf (" Thursday ");
break;
case 5 : printf (" Friday ");
break;
case 6: printf (" Saturday ");
break;
case 7 : printf (" Sunday");
break;
default :printf("the entered number is not in specified limit") ;
break;
}
getch( ) ;
}
Example:
=> Write a program to illustrate switch case statement
/* Program to illustrate switch case statement*/
#include<stdio.h>
void main ( )
{
int n;
clrscr( ) ;
printf (" enter the alphabet") ;
scanf(" %d", &n) ;
switch (n)
{
case 1: printf ("A") ;
break;
case 2: printf("E") ;
break;
case 3: printf("I") ;
break;
case 4: printf ("O") ;
break;
case 5 :printf("U") ;
break;
default :printf (" the entered no is constant ") ;
}
getch( );
}
No comments:
Post a Comment