You are on page 1of 6

Programming in C

MODULE II
ITERATIVE CONTROL STRUCTURES AND ARRAYS

Looping Structures
In looping, a program executes the sequence of statements many times until the stated condition
becomes false. A loop consists of two parts, a body of a loop and a control statement. The control
statement is a combination of some conditions that direct the body of the loop to execute until the
specified condition becomes false. Depending upon the position of a control statement in a program,
a loop is classified into two types:
1. Entry controlled loop
2. Exit controlled loop

Entry Controlled
In an entry controlled loop, a condition is checked before executing the body of a loop. It is also
called as a pre-checking loop or pre-test loop. An entry control loop checks the condition at the time
of entry and if condition or expression becomes true then control transfers into the body of the loop.
Such type of loop controls entry to the loop that’s why it is called entry control loop.

While loop in the main looping statement available in this category:

While loop
The while loop, is the simplest looping structure. It is an entry controlled loop. If we have to execute
some statements repeatedly as long as certain condition become true we can use the while loop. In
the while loop the given expression will be check at the time of the loop entry, if given expression
becomes true then control will transfer into the loop body and the statement inside the loop body will
1
Programming in C
be executed. After the body of the loop is executed then control again goes back at the beginning and
again given expression will be checked to enter in to the loop. This process will continue until the
given expression become false. Once the condition becomes falsethe control goes out of the loop.
After exiting the loop, the control goes to the statements which are immediately after the loop. The
while loop contains only expression part, initialization will be done before the while loop.

Syntax:
while(condition)
{
//loop body
}

Example:
while( i <= 10)
{
printf("\n Value of I is %d", i) ;
i++ ; // modify counter value
}

Exit Controlled
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called
as a post-checking loop or post-test loop. An Exit Control Loop checks the condition for exit and if
given condition for exit evaluate to true, control will exit from the loop body else control will enter
again into the loop. Such type of loop controls exit of the loop that’s why it is called exit control
loop.

Do…While loop
Do …while loop is an exit control loop. It is a variation of while loop. When we want to execute or
perform some task at least for once, we can use do…while loop structure. In a while loop if the

2
Programming in C
expression becomes false at very first try then loop will never be executed. Since, do…while is of
type exit control loop it checks the condition at last so, first time the loop will be execute
unconditionally. If the condition for the exit becomes true then loop will be terminate otherwise it
will be executed for the next time. It has the following form:
Syntax:
do
{
//loop body
} while (expression);

The main difference between while loop and do… while is as follow.
WHILE LOOP DO…WHILE LOOP
It is an entry control loop. It is an exit control loop.
In while loop expression/condition is checked at In do… while loop condition is
the time of entry. checked at the time of exit.
In do while loop semicolon (;) at the
It does not have semicolon (;) at the end of the
end of the expression is
expression.
compulsory.
Syntax:
do
while(condition)
{
{
// loop body
// loop body
} while(condition);
}
Example:
do
while(i<=5)
{
{
printf(“\n%d”,i);
printf(“\n%d”,i);
i++;
i++;
} while(i<=5) ;
}

Counter Controlled
A counter controlled loop will exit after running a certain number of times. The count is kept in a
variable called an index or counter. When the index reaches a certain value (the loop bound) the loop
will end.

for loop
A for loop is a repetition control structure which allows us to write a loop that is executed a specific
number of times. The loop enables us to perform n number of steps together in one line.
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
In for loop, a loop variable is used to control the loop. First initialize this loop variable to some
value, then check whether this variable is less than or greater than counter value. If statement is true,
then loop body is executed and loop variable gets updated . Steps are repeated till exit condition
comes.
3
Programming in C
Initialization Expression:: In this expression we have to initialize the loop counter to some value. for
example: int i=1;
Test Expression: In this expression we have to test the condition. If the condition evaluates to true
then we will execute the body of loop and go to update expression otherwise we will exit from the
for loop. For example: i <= 10;
Update Expression:: After executing loop body this expression increments/decrements the loop
variable by some value. for example: i++;
Equivalent flow diagram for loop :

Nested Looping
The nesting of for loops can be done up-to
up to any level. The nested loops should be adequately indented
to make code readable. In some versions of 'C,' the nesting is limited up to 15 loops, but some
provide more. The nested loops are mostly used in array applications

Break Statement
The break statement is used mainly in the switch statement. It is also useful for immediately stopping
a loop. We consider the following program which introduces a break to exit a while loop:

#include <stdio.h>
int main() {
int num = 5;
while (num > 0) {
if (num == 3)
break;
printf("%d\n", num);
num--;
}
}

Output:
5
4

4
Programming in C
Continue Statement
When we want to skip to the next iteration but remain in the loop, we should use the continue
statement. For example:

#include <stdio.h>
int main() {
int nb = 7;
while (nb > 0) {
nb--;
if (nb == 5)
continue;
printf("%d\n", nb);
}}

Output:
6
4
3
2
1
So, the value 5 is skipped.

Arrays
The array is a data structure in C programming, which can store a fixed-size sequential collection of
elements of the same data type. All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to the last element.

In C language, an array can be One-Dimensional, Two-Dimensional and Multidimensional.

Defining an array
Syntax:
type array_name [size];
An array type can be any valid C data types, and array size must be an integer constant greater than
zero. Example:
int marks[5];

Initializing an array
Arrays can be initialized at declaration time as:
int marks[5]={45,48,22,38,10};
If we omit the size of the array, an array just big enough to hold the initialization is created.
int marks[]={45,48,22,38,10};

Accessing Array Elements


An element is accessed by indexing the array name. This is done by placing the index of the element
within square brackets after the name of the array.
double salary = balance[9];
The above statement will take the 10th element from the array and assign the value to salary variable.
5
Programming in C
Two Dimensional Arrays
A two-dimensional array is a list of one-dimensional arrays. To declare a two-dimensional array of
size [x][y], you would write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A two-
dimensional array can be considered as a table which will have x number of rows and y number of
columns. A two-dimensional array a, which contains three rows and four columns can be shown as
follows −

Thus, every element in the array a is identified by an element name of the form a[ i ][ j ], where 'a' is
the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each element in 'a'.

Initializing Two-Dimensional Arrays


Multidimensional arrays may be initialized by specifying bracketed values for each row. Following
is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements


An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array. For example −
int val = a[2][3];
The above statement will take the 4th element from the 3rd row of the array.

You might also like