Looping Statements in C
Looping Statements in C
Types of Loops in C
Depending upon the position of a control statement in a program,
looping statement in C is classified into two types:
While Loop in C
A while loop is the most straightforward looping structure. While
loop syntax in C programming language is as follows:
After exiting the loop, the control goes to the statements which are
immediately after the loop. The body of a loop can contain more
than one statement. If it contains only one statement, then the curly
braces are not compulsory. It is a good practice though to use the
curly braces even we have a single statement in the body.
In while loop, if the condition is not true, then the body of a loop will
not be executed, not even once. It is different in do while loop which
we will see shortly.
Do-While loop in C
A do…while loop in C is similar to the while loop except that the
condition is always executed after the body of a loop. It is also
called an exit-controlled loop.
Syntax of do while loop in C programming language is as follows:
Similar to the while loop, once the control goes out of the loop the
statements which are immediately after the loop is executed.
The critical difference between the while and do-while loop is that in
while loop the while is written at the beginning. In do-while loop, the
while condition is written at the end and terminates with a semi-
colon (;)
For loop in C
A for loop is a more efficient loop structure in ‘C’ programming. The
general structure of for loop syntax in C is as follows:
For example:
for (x = 0, y = num; x < y; i++, y--) {
statements;
}
Also, we can skip the initial value expression, condition and/or
increment by adding a semicolon.
For example:
int i=0;
int max = 10;
for (; i < max; i++) {
printf("%d\n", i);
}
Notice that loops can also be nested where there is an outer loop
and an inner loop. For each iteration of the outer loop, the inner
loop repeats its entire cycle.
2 x 0 = 0
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
The nesting of for loops can be done 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.
Break Statement in C
The break statement is used mainly in the switch statement. It is
also useful for immediately stopping a loop.
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.
Summary
Define loop in C: A Loop is one of the key concepts on
any Programming language. Loops in C language are
implemented using conditional statements.
A block of loop control statements in C are executed for
number of times until the condition becomes false.
Loops in C programming are of 2 types: entry-controlled and
exit-controlled.
List various loop control instructions in C: C programming
provides us 1) while 2) do-while and 3) for loop control
instructions.
For and while loop C programming are entry-controlled loops
in C language.
Do-while is an exit control loop in C.