A program cannot always be a sequential set of statements consisting of only assignment and input/output statements. While developing programs to solve any problem, it is often necessary to carry out a logical test and depending upon the outcome, a new course of action is taken. Like most programming languages, C supports such conditional execution with the help of if..else or switch..case statements. Similarly, for executing a group of statements repetitively C provides a set of statements such as for statement, while-do statement, do-while statement. These classes of statements are called control statements.
5.1 if....else STATEMENT
The syntax of if statement/or if else statement which requires evaluation of a condition and branching according to the outcome of the decision test is written as follows :
if(
{
}
else
{
}
Since C treats logical values as integer type with value 0 for false and non-zero for true, the
Example 5.1 :
# include
main()
{
int min,x,y;
printf(“Enter the value of x and y :”);
scanf(“%d %d”,&x,&y);
fflush(stdin);
/*Computes minimum of x and y */
if(x
min = x;
else
min = y;
printf(“%d is the minimum of %d and %d\n”,min,x,y);
}
The if statements can be nested as in
if e1 s1;
else if e2 s2;
else s3;
or,
if e1
if e2
s1;
else s2;
else s3;
Example 5.2 :
# include
#define TRUE 1
#define FALSE 0
main()
{
int EQUAL,min,x,y,max;
printf(“Enter the value of x and y :”);
scanf(“%d %d”,&x,&y);
fflush(stdin);
/*Computes minimum of x and y */
if(x == y)
{
EQUAL=TRUE;
min=max=x;
}
else
{
EQUAL=FALSE;
if(x > y)
{
max=x;
min=y;
}
else
{
max=y;
min=x;
}
}
if(!EQUAL)
printf(“Minimum : %d Maximum : %d\n”,min,max);
else
printf(“Both x and y are equal\n”);
}
1. Identify the mistakes in the following program.
# include
main()
{
float basic;
printf(“Enter basic :”);
scanf(“%f”,&basic);
fflush(stdin);
if(basic = 0)
printf(“Invalid input\n”);
else
printf(“Basic is %.2f”,basic);
}
2. Predict the output of the following C program
# include
main()
{
int a, b, c;
scanf (“%d %d %d”, &a, &b, &c);
if (a >b)
{
a + = b;
a++;
}
if(a >c)
a * = c;
else
c - = (a + b);
printf (“%d%d%d \n”, a,b,c);
}
5.2 SWITCH STATEMENT
The switch statement is a generalisation of the if statement.
The syntax is
switch
statement
The switch statement is a compound statement which specifies alternate course of actions. Each alternative is expressed as a group of one or more statements which are identified by one or more labels called case labels. The following two different programs, intended to perform the same task, illustrate how nested if can be replaced by a switch....case construct.
/*program having nested if statements */
# include
main()
{
char category;
printf(“Enter Category :”);
category=getchar();
fflush(stdin);
if(category == ‘B’)
{
printf(“B.TECH Students \n”);
/* B. TECH Processing */
}
else if(category == ‘M’)
{
printf(“M.Sc. Student \n”);
/* M.Sc. Processing */
}
else
if(category == ‘T’)
{
printf(“M.TECH Student \n”);
/* M.TECH Processing */
}
else
if(category == ‘P’)
{
printf(“Ph. D. Student \n”);
/* Ph. D. Processing */
}
else
{
printf(“ERROR\n”);
/* Error Processing */
}
}
/* program with switch case construct */
# include
main()
{
char category;
printf(“Enter Category :”);
category=getchar();
fflush(stdin);
switch(category)
{
case ‘B’ :
printf(“B.TECH Students \n”);
/* B. TECH Processing */
break;
case ‘M’ :
printf(“M.Sc. Student \n”);
/* M.Sc. Processing */
break;
case ‘T’ :
printf(“M.TECH Student \n”);
/* M. TECH Processing */
break;
case ‘P’ :
printf(“Ph. D. Student \n”);
/* Ph. D. Processing */
break;
default :
printf(“ERROR\n”);
/* Error Processing */
}
}
5.3 REPETITIVE STATEMENTS IN C
C supports following three types of repetitive statements.
1. while(
{
<>
}
2. do
{
} while(
3. for (expression1; expression2; expression3)
{
<>
}
In the while and do while statements the
Consider the following program for computation of average which is shown to have been implemented using three types of repetitive statements.
Example 5.4 :
# include
main ()
/* calculate the average of a set of items; The variable maxitem determines the total number of items*/
{
int maxitem, n = 1;
float data, mean, sum =0;
printf(“How many items?”);
scanf (“%d”, &maxitem);
/* consider the following repetitive statements which accept numbers from the user as input once in each pass and calculate sum of all these numbers. The use of three different repetitive constructs are demonstrated here */
{ scanf(“%f”,&data); sum += data; } |
do { scanf(“%f”,&data); sum += data; ++n; } while(n <= maxitem); |
while(n <= maxitem) { scanf(“%f”,&data); sum += data; ++n; } |
mean=sum / maxitem;
printf(“\n The average is %f\n”,mean);
}
Example 5.5 :
/* Program to check whether the number provided as input is a palindrome or not */
# include
main()
{
int number,digit,reverse=0,store_num;
printf(“\nAccept any number :”);
scanf(“%d”,&number);
fflush(stdin);
store_num=number;
do{
digit=number % 10;
reverse=(reverse * 10) + digit;
number /= 10;
}while(number != 0);
if(number == reverse)
printf(“The number is a palindrome\n”);
else
printf(“The number is not a palindrome\n”);
}
Comma operator :
This operator is used along with the for statement to handle multiple indices. Thus for (expression1a, expression1b; expression2; expression) statement is used to initialise two separate indices through expression1a and 1b. Similarly for (expression1; expression2; expression3a, expression 3b) can be used to alter the values of the indices based on expression 3a and expression 3b.
Example 5.6 :
for (i=1, j=2; i<5,>
{
.......
}
The break statement
break;
This statement is used to terminate loops or to exit from a switch.
The continue statement is used to bypass the remaining statements in a loop i.e., these statements are skipped and next pass begins. It is used with while, do - while or for statements. 3. Predict the output of the following C programs.
a.
# include
main()
{
int i=0,x=0;
for(i=1;i <>
{
if(i % 2 == 1)
x += i;
else
x--;
printf(“%d ”,x);
}
printf(“\nx = %d”,x);
}
main()
{
int a, b, c=0;
for(a=0; a <>
for(b=0; b <>
{
c += (a + b - 1);
printf(“%d”,c);
}
printf(“\nc=%d”, c);
}
c. # include
main()
{
int a,b,c,i=0;
for(a=0;a <>
for(b=0;b <>
{
switch(a + b )
{
case 0:
i += 1;
break;
case 1: i + = 2;
case 2: i + = 3;
case 3:
i += 4;
break;
default :
i += 5;
}
printf(“%d ”,i);
}
printf(“\n i = %d”,i);
}
0 comments:
Post a Comment