Control structure in C
Control structures are those
programming constructs, which control the flow of program statement execution
in a program. It also specifies the order of statements in the program.
In most of the programs, program
statements were executed in the same order in which they were written. Each
instruction was executed only once. This is not enough in programming. Sometime
we may have to execute program statements based on the given condition, we may have to execute program statements repeatedly, sometimes we may have to
choose an option and perform the task accordingly. To carry out all these tasks
and other similar tasks, program statements must be executed in a controlled way
and it can be done using a control statement.
Mainly control structures are
classified into the following three categorized
1. Branching
(selective control statement)
2. Looping
(Repetitive control statement )
3. Jumping
(unconditional control statement )
Besides these, if there is no
looping/branching/ jumping then the structure is called sequential structure.
In sequential control structure, program statements are executed in a sequence
that is one after another.
In selective control structure, the selection is made based on condition. We have options to go when the
given condition is true or false. The flow of program statement execution is
totally directed by the result obtained from the checking condition. Hence, program
statements using selective control structures are also called conditional
statements. This type of control structure is mainly used for decision-making.
It can mainly be categorized into two types.
Types of selective control
statement
1. Conditional
statement
2. Switch case statement
Conditional statement / Control statement
It is the most common decision making
control structure which controls the flow of program statement execution based
on the condition checked. It can be used in different forms.
Types of control statement
1. if
statement
2. if else
statement
3. if else
if statement (Multipath conditional statement/ if-else ladder)
4. nested if
else statement
if
statement
This is the simplest form of
conditional statement in which statements are executed if the test expression
(condition) is true. When the condition is false there is no option to go within
this structure; in such a situation control must get out from the structure will
be executed. Read more
if
else statement
This is another form of selective
control structure which can handle both expected as well as unexpected
situation. In this control structure, statements written in the body part of if are executed if the condition is true
otherwise, statements written in the body part of else are executed. This is
appropriate where we have to check the only condition. Read more
if
else if statement
When we have two or more
conditions to be checked in a series we can use if else if statement. It is
also known as multiple conditional statements/ multipath conditional statement /
if-else ladder. Read more
nested if-else statement
An entire
if-else statement is written within the body of if part or else part of
another if-else statement is called a nested if-else statement. It is used when a
conditional is to be checked inside another condition at a time in the same
program to make a decision.
Switch
case statement
C switch case statement is a
multipath decision-making statement that allows selection and execution of a
particular block of statements from several blocks of a statement based upon the
value of expression which is
included within the switch statement and branches accordingly. The expression must be of an integral value. Read more
Looping
Looping is the process of
executing the same program statement or block of program statement repeatedly
for a specified number of times or till the given condition is satisfied. Loop the structure is used to carry out looping.
Types of looping
1. for loop
2. while
loop
3. do-while
loop
for loop
It is the most common
type of loop which is used to execute program statement or block of program
statements repeatedly for a certain time. It is a definite loop. Main
it consists of three expressions: initialization, condition, increment, or
decrement. The initialization defines the loop starting point, the condition
defines the loop stopping points and the counter helps to increment and decrement to a value of a counter variable. Read more
while loop
while loop executes
the program statements repeatedly until the given condition is true. It checks the condition at first; if it is found true it executes the statements written in
its body part otherwise it just gets out from the loop structure. It is also
known as entry control or pre-test loop. Read more
do-while loop
It also executes the program statement repeatedly until the given condition is true. It executes the
program statements once at first then only the condition is checked. If the condition
is found true then it executes the program statement again, otherwise, it gets
out from the loop structure. As it checks the condition at last it is also known to post-test loop or exit loop. Read more
Jumping
Jumping statements are
particularly used to jump the execution of program statements from one place to
another place inside a program. These statements may execute the same program
statement repeatedly or skip some program statements. Following are the jumping
statements defined in the C programming language.
1. break
2. continue
3. goto
break statement
As its name implies, it is used
to break the normal flow of program statement execution in loop and switch case
statement. It allows us to exist from the innermost enclosing loop or switch case
statement as a certain condition is satisfied.
Example
|
#include<stdio.h> #include<conio.h> void main() { int i; for(i=1;i<=5;i++) { if (i==4) { break; printf("%d\t",i); } } getch(); } |
The
program output is
When the value of i becomes then the break is encountered and control is passed to outside the loop structure
Continue statement
As its name implies, it is used to
continue the normal flow of program statement execution in the loop; skipping particular
iteration in the loop as soon as a certain condition is satisfied. When continue
is encountered in the program (loop body) then the particular iteration is
skipped and the loop will be continued with the next iteration.
Example
|
#include<stdio.h> #include<conio.h> void main() { int i; for(i=1;i<=5;i++) { if (i==4) { continue; printf("%d\t",i); } } getch(); } |
Output
of this program
When the value of i becomes 4
continue is encountered then that iteration for which is equal to 4 is skipped
and it continues with i equals 5
Also, check



0 Comments
Please do not enter any spam link on comment box