Control statements decide the flow of execution. We know
that C language is a procedural language, staring the execution with main
function. Not all the times we need the sequential execution. The program
should be executed based upon conditions; should be executing certain statements
for several times. Based upon where the control is transferred, there are two
types of control statements. They are Iterative control statements and Non-iterative
control statements.
The iterative control statements are used to execute single
statement or set of statements several times. The non-iterative control
statements just shift the flow of execution.
C consists of three iterative control structures. The following
are the syntaxes and examples of each of these iterative control structures.
Control statements in C language |
while control statement:
syntax:
initialization
while(condition)
{
statements
}
Eg:
i=0;
while(i<10)
{
printf(“Hello world\n”);
i++;
}
do..while control statement:
syntax:
syntax:
initialization
do{
statements
}while(condition);
Eg: i=0;
do{
printf(“Hello world\n”);
i++;
while(i<10);
for control statement:
syntax:
for(expression1;expression2;expression3)
{
statements
}
Eg:for(i=0;i<10;i++)
printf(“Hello world\n”);
Out of the three iterative control statements, for is the
most commonly used control statement. Based upon the context, while and
do..while control statements are used. It is important to note the minimum
number of times each of these control structures are executed. Both the for
loop and while loop are executed zero time; whereas do..while loop is executed
once before validating the condition.
Non-iterative control statements:
These control statements just pass the flow of execution to
some other instruction as the programmer requires. There are two types of non-iterative
control statements. They are conditional and unconditional control statements.
The conditional control statements pass the control of
execution upon satisfying a given condition; whereas the unconditional control
statements do the same without any condition.
The conditional control statements are if, if..else and
switch..case. The unconditional control statements are goto, break, continue
and return.
Conditional control
statements:
The following are the syntaxes and examples of each of these
conditional control statements.
if control structure:
This is used to check if a certain condition is met, as
shown below.
Syntax:
if(condition)
{
statements
}
Eg:if(a==10)
printf(“a is 10\n”);
if..else control statement:
If there are only two outcomes of a particular expression,
then if..else is used.
Syntax:
if(condition)
{
statements
}
else
{
statements
}
Eg:if(a<0)
printf(“Negative number”);
else
printf(“Non-negative number”);
The if and if..else control statemetns can be nested as
shown below. The most common nesting format is else..if ladder i.e., a if block
is nested within the else block, as shown below.
Out of all the conditions mentioned in the if block, only
one is executed.
Syntax:if(condition1)
{
statements
}
else if(condition2)
{
statements
}
else if(condition3)
{
statements
}
…
Eg:
if(a<0)
printf(“Negative value”);
else if(a==0)
printf(“Zero value”);
else
printf(“Positive value”);
switch..case control statement:
The switch..case control statement is used when there are multiple results of an expression. The main advantage with switch..case is found in menu driven applications to get the user response. Based upon the result of the condition written in switch, respective case is executed. Remember that every case is to mentioned with break statement, that is useful in breaking the switch, otherwise all the cases after the given case are executed.
The switch..case control statement is used when there are multiple results of an expression. The main advantage with switch..case is found in menu driven applications to get the user response. Based upon the result of the condition written in switch, respective case is executed. Remember that every case is to mentioned with break statement, that is useful in breaking the switch, otherwise all the cases after the given case are executed.
Syntax:
switch(choice)
{
cases and statements
}
Consider the following example program.#include<stdio.h>
main()
{
int ch,num1,num2;
do{
switch(ch)
{
case 1:printf("Sum is %d\n",num1+num2);
case 2:printf("Subtraction is %d\n",num1-num2);
case 3:printf("Product is %d\n",num1*num2);
default:printf("Invalid choice..\n");
case 4:printf("Division is %d\n",num1/num2);
case 5:printf("Mod is %d\n",num1%num2);
}
printf("1:Add\t2:Subtract\t3:Product\t4:Divide\t5:Modulus\t6:Quit\n");
scanf("%d",&ch);
printf("Enter 2 numbers:");
scanf("%d%d",&num1,&num2);
}while(ch<=5);
}
In this program, observe that the do..while loop is executed
first with garbage value with ch and so the default case is executed. Also,
observe that default case is mentioned among the cases.
Initially, ch contains garbage value. Hence, the default
case is executed. At the same time, all the cases beneath the default case. This
is because, switch cannot exit after executing the respective case. It is to be
taken care by the programmer by mentioning break at the end of every case. Irrespective
of whether it is default case or any other case, the last case does not need break.
#include<stdio.h>
main()
{
int ch,num1,num2;
do{
switch(ch)
{
case 1:printf("Sum is %d\n",num1+num2);break;
case 2:printf("Subtraction is %d\n",num1-num2);break;
case 3:printf("Product is %d\n",num1*num2);break;
case 4:printf("Division is %d\n",num1/num2);break;
case 5:printf("Mod is %d\n",num1%num2);break;
default:printf("Invalid choice..\n");
}
printf("1:Add\t2:Subtract\t3:Product\t4:Divide\t5:Modulus\t6:Quit\n");
scanf("%d",&ch);
printf("Enter 2 numbers:");
scanf("%d%d",&num1,&num2);
}while(ch<=5);
}
The switch..case always jumps to the case when the switch condition is true, ignoring all other statements. Check the following example
#include<stdio.h>
main()
{
int a=10;
switch(1)
{
a++;
case 1:printf("This is case 1\n");break;
default:printf("This is default case\n");
}
printf("a:%d\n",a);
}
*Remember that the choice can be an expression. Though the choice expression returns a floating point, the integral value is only considered. Also, the choice of a case should be integral type (recollect that char is also considered as integral datatype).
Try out the following program with different ‘choice’ values and observe the output.
#include<stdio.h>
main()
{
int a=10,b=6;
switch(1)//swithc(1.5)//switch(a/b)
{
case 1:printf("This is case 1\n");break;
default:printf("This is default case\n");
}
}
Unconditional iterative statements:
These iterative statements branch unconditionally.
goto control statement:
Syntax:
goto label;
This just branches the flow of execution with a given label. Check out the following program.
#include<stdio.h>
main()
{
label:printf("Hello world..\n");
printf("How are you?\n");
goto label;
}
This program executes infinitely, as there is no condition
to break the execution. Modify the program as shown below. Then observe the
output.
#include<stdio.h>
main()
{
int i=0;
label:
if(i<5)
printf("Hello world..\n");
printf("How are you?\n");
i++;
goto label;
}
break control statement:
One of the uses of break statement was observed previously. The break control statement is also used to break a loop. Check out the following program.
#include<stdio.h>
main()
{
int i;
for(i=0;i<100;i++)
{
printf("%d\n",i);
if(i>5)
break;
}
}
continue control statement:
The continue control statement is used to skip the executing
of certain statements. See the following example.
#include<stdio.h>
main()
{
int i;
for(i=0;i<100;i++)
{
if(i<5)
continue;
printf("%d\n",i);
}
}
return control statement:
The return control statement is used just to return to the
calling function. See the following example.
#include<stdio.h>
int print(int i)
{
i=i+10;
return i;
}
main()
{
int i=10;
printf("Before return:%d\n",i);
i=print(i);
printf("After return:%d\n",i);
}
Observe that the value of i is being returned to the main
function, which is the calling function. The complete discussion about functions
is mentioned in further sessions.
0 comments:
Post a Comment