Control Structures
Sequence control statements
|
This type of statements insured that
the instruction in a program is executed in the same order. In which they
appear in the program.
|
Decision control & conditional
statements
|
Decision control statements and Conditional Statements
allow the computer to take decision. And force to work under given condition:
|
Decision control statements:
|
It gives the control to the computer
for taking the decisions.
|
Two decisions control instruction
which are implemented in C are following:
|
a) The if statements.
|
b) The if-else statements.
|
General form of if statements
is:
|
if (condition is true) execute this statement;
|
Here we can add else statement also
as follow:
|
else
|
printf("you have enter number
greater than 50");
|
}
|
Case control statements
|
The Case control statements
allow the computer to take decision as to be which statements are to be
executed next. It is a multi way decision construct facilitate number of
alternatives.
|
C has multi way decision statement
known as switch statements. It tests the value of a given variable or
expression against a list of case value and when a match found a block of
statement associated with the case is executed.
|
Syntax of switch statement:
|
switch(expression)
|
{
|
case constant_1:
|
statements;
|
case constant_2:
|
statements;
|
case constant_n:
|
statements;
|
default:
|
statements;
|
}
|
Explanation:
|
First in expression parentheses we
give the condition.
|
This condition checks to match, one
by one with case constant.
|
If value match then its statement
will be executed. Otherwise the default statement will appear.
|
Repitition & loop control
statements
|
This type of statements helps the
computer to execute a group of statements repeatedly.
|
This allows a set of instruction to
be performed until a certain condition is reached.
|
There are three types of loops in C:
|
1. for loop
|
2. while loop
|
3. do-while loop
|
The for loop
|
There are three parts of for
loop:
|
a)counter initialization.
|
b)check condition
|
c)modification of counter.
|
Syntax:
|
for (variable initialize; check condition;
modify counter)
|
{
|
statements 1;
|
-----------;
|
-----------;
|
statements n;
|
}
|
Explanation:
|
1. The initialization is usually an
assignment that is used to set the loop control variable.
|
2. The condition is a relational
expression that determines when the loop will exit.
|
3. The modify counter defines
how loop control variables will change each time the loop is
repeated.
|
These three sections are separated
by semicolon (;).
|
The for loop is
executed as long as the condition is true. When, the condition becomes false
the programe execution will resume on the statement following the block.
|
Advantage of for loop over
other loops:
|
All three parts of for loop (i.e.
counter initialization, check condition, modification of counter) are
implemented on a single line.
|
Something more about for loop:
|
1. for
(p=1,n=2;n<17 b="" n=""> we can assign multiple variable together
in for loop.
|
2. for
(n=1,m=50;n<=m;n=n+1,m=m-1):-The increment section may also have more
than one part as given.
|
3. for
(i=1,sum=0;i<20 amp="" b="" i="" sum="">The test condition may have
any compound relation as given.
|
4. for
(x=(m+n)/2;x>0;x=x/2):-It is also permissible to use expressions in
the assignment statements of initialization and increment section as given.
|
5. for (;m!=100;):-we can
omitted the initialization and increment section to set up time delay.
|
while loop
|
It is a primitive type looping
control because it repeats the loop a fixed no. of time. It
is also called entry controlled loop statements.
|
Syntax:
|
while (test_condition)
|
{
|
body of loop
|
}
|
Explanation:
|
The test condition is evaluated if
the condition is true, the body of loop will be executed.
|
The do-while loop
|
The minor Difference between the
working of while and do-while loop is the
place where the condition is tested.
|
The while tests the condition before
executing any of the statements within the while loop
|
As against this, the do-while
loop tests the condition after having executed the statement within
the loop.
|
syntax:
|
do
|
{
|
body of loop;
|
}
|
while (test condition);
|
Some more statements
|
The break Statement
|
We have already met break in the
discussion of the switch statement. It is used to exit from a loopor
a switch, passing control to the first statement beyond the loop or
a switch.
|
With loops, break can be
used to force an early exit from the loop, or to implement a loop with
a test to exit in the middle of the loop body.
|
A break within a loop should
always be protected within an if statement which provides the test to control
the exit condition.
|
The continue Statement
|
This is similar to break but is
encountered less frequently. It only works within loops where its
effect is to force an immediate jump to the loop control
statement.
|
In a while loop, jump
to the test statement.
|
In a do while loop, jump
to the test statement.
|
In a for loop, jump to
the test, and perform the iteration (looping).
|
Like a break, continue should be
protected by an if statement. You are unlikely to use it very often.
|
The goto Statement
|
C has a goto statement which permits
unstructured jumps to be made. It requires a label in order to identify the
place where the branch is to be made.
|
A label is any valid variable name,and
must be followed by a colon(:).
|
Syntax:
|
goto label;
|
..................
|
label:
|
statement;
|
The label can be any where in the
program either before or after the goto label; statement.
|
The exit() function
|
This function is used for terminating
the execution of C program.
|
Syntax:
|
exit(int status);
|
More Topics :