Loop Control Statements in C

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Loop Control Statements in C

We use the loop control statements in C language for performing various loop operations until
we find the condition given in a program to be true. The control comes out of a loop statement
when the condition given to us turns out to be false. Looping is basically a phase in which we
repeat the very same process multiple times unless it specifies any specific type of condition.

Table of Contents

● Use Of Loop Control Statements In C


● Types Of Loops In C
● Summary Of Types Of Loops In C:
● Types Of Loop Control Statements In C
● Summary Of Loop Control Statements In C
● Break Statement
● Syntax
● Example
● Output
● Continue Statement
● Syntax
● Example
● Output
● Goto Statement
● Syntax
● Example
● Output
● The Condition Of An Infinite Loop
● Practice Problems On Loop Control Statements In C
● FAQs

Use of Loop Control Statements in C


Consider the expression that says, “If a thing is worth doing, then it is worth doing multiple
times.” There are various real-life examples of this statement- like riding a horse, watching a
movie, eating your favourite meal, wearing the same clothes, etc. Similarly, in programming, we
sometimes have to perform the very same action/ program over and over again. This need is met
by a mechanism known as the loop. Sometimes, users would want to break out of any given
loop. It is known as loop control.

The loop control statements help in changing the execution of a loop from its original sequence.
Whenever the execution of the control statement takes place in a loop, the loop leaves the scope.
Thus, in this case, all the objects created automatically in the scope will get ultimately destroyed
in no time.

Types of Loops in C
We can handle various looping requirements in the C programming language with the use of the
following types of loops:
● while loop
● nested loop
● for loop
● do-while loop

Summary of Types of Loops in C:


Type of Description
Loop

for loop This kind of loop performs the execution of a sequence of various
statements various times. It then abbreviates the available code that
manages the variables of the loop.

while This type of loop is used for repeating any given statement or a group of
loop various statements whenever the condition we have is true. Thus, it will
perform the testing of the condition before the execution of the body of
the loop.

do…whil This loop is just like the while statement. The only exception is that the
e loop do-while statement performs the testing of the condition that is present
at the very end of a loop body.

nested We can make use of a single loop or multiple loops inside any other loop,
loops such as for, while, or even the do-while loop.

Types of Loop Control Statements in C


The C programming language provides support for various control statements. These are:

● goto statement
● continue statement
● break statement

Summary of Loop Control Statements in C


In the table available below, we have all the loop control statements in C language, along with its
description:

Type of Loop Description


Control Statements
in C

goto statement This type of statement transfers the control to any labeled
statement.

continue statement This type of statement will cause a loop to skip the remainder
present in its body and then retest its actual condition
(immediately) before reiterating it.

break statement This type of statement ultimately terminates the switch


statement and the loop statement. It then transfers the
statement execution after following the switch or loop
immediately.

Break Statement
There are the following two uses of the Break Statement in C programming language:

● Whenever a program encounters a break statement inside any given loop, there occurs an
immediate termination of that loop. After this, the program control ultimately resumes in
the very next statement that follows the given loop.
● We can use it for terminating a case used in the switch statement.

Also, when we are making use of the nested loops, the break statement comes in handy. It
terminates the execution of the loop that is innermost and then it starts the execution of the line
of code that is after (next) to the block.

Syntax
Here is the syntax used for the break statement in the C language:

break;

Example
Let us look at an example of the Break statement in C language,

#include <stdio.h>

int main () {

/* definition of the local variable */

int x = 20;

/* execution of the while loop */

while( x < 40 ) {

printf(“The available value of x is: %d\n”, x);

x++;

if( x > 30) {

/* termination of the loop with the use of the break statement */

Break;

return 0;

Output
The compilation and execution of the code mentioned above will produce a result as follows:

The available value of x is: 20

The available value of x is: 21

The available value of x is: 22

The available value of x is: 23

The available value of x is: 24

The available value of x is: 25

Continue Statement
This type of statement works very similarly to that of the break statement in the C programming
language. But here, instead of forcing the termination of the loop, it will rather force the next
iteration of the available loop. Thus, it will ultimately skip the code present in between.

The continue statement in the case of for loop will cause the execution of the conditional test as
well as the increment portions present in a loop. On the other hand, the continue statement will
ultimately cause the program control to pass on to the conditional tests, in the case of the
do-while loop and the while loop.
Syntax
Here is the syntax used for the continue statement in the C language:

continue;

Example
Let us look at an example of the Continue statement in C language,

#include <stdio.h>

int main () {

/* definition of the local variable */

int x = 10;

/* execution of the do loop */

do {

if( x == 15) {

/* skipping of the iteration */

x = x + 1;

continue;

printf(“The given value of a is: %d\n”, x);

x++;

} while( x < 20 );

return 0;

Output
The compilation and execution of the code mentioned above will produce a result as follows:

The given value of a is: 10

The given value of a is: 11

The given value of a is: 12

The given value of a is: 13

The given value of a is: 14

The given value of a is: 16

The given value of a is: 17


The given value of a is: 18

The given value of a is: 19

Goto Statement
This type of statement will provide a user with an unconditional jump (in the C programming
language) to a labeled statement from the ‘goto’ in the very same function.

Note – The usage of the goto statement is not very much advisable in any programming
language. It is generally discouraged because this type of statement makes it very difficult for
any user to trace the overall flow of any program. Thus, the program becomes very hard to
understand in this case- and eventually becomes very difficult to modify as well. But we must
remember that we can rewrite a program at any time if it has utilized the goto statement.

Syntax
Here is the syntax used for the goto statement in the C language:

goto label;

..

label: statement;

In this case, the label can easily be any type of plain text- but it cannot be a C keyword. Also, we
can set it anywhere in a C program either- below or above the goto statement.

Example
Let us look at an example of the goto statement in C language,

#include <stdio.h>

int main () {

/* definition of the local variable */

int x = 10;

/* execution of the do loop */

LOOP:do {

if( x == 15) {

/* skipping of the iteration */

x = x + 1;

goto LOOP;

}
printf(“The value of the variable x is: %d\n”, x);

x++;

}while( a < 20 );

return 0;

Output
The compilation and execution of the code mentioned above will produce a result as follows:

The value of the variable x is: 10

The value of the variable x is: 11

The value of the variable x is: 12

The value of the variable x is: 13

The value of the variable x is: 14

The value of the variable x is: 16

The value of the variable x is: 17

The value of the variable x is: 18

The value of the variable x is: 19

The Condition of an Infinite Loop


Any loop in a program will ultimately become an infinite loop in case the condition never happens
to be false. In such cases, we must make use of the for loops (traditionally). Just because we
don’t require any of the three expressions that basically constitute the for loop, we can ultimately
create an endless loop. We can do so if we leave the conditional expression to be empty.

#include <stdio.h>

int main () {

for( ; ; ) {

printf(“The available loop here will run forever in circles.\n”);

return 0;

Here, when there is no conditional expression, we assume it to be true. Thus, a person might
have an increment and initialization expression- but as a C programmer, they must use the for(;;)
construct (more commonly) for signifying a loop that might be infinite.

Note – We can easily terminate an infinite loop if we press the Ctrl + C keys.
Practice Problems on Loop Control Statements in C
1. Take a look at the following program:

#include <stdio.h>

int main () {

int z = 10;

while( z < 20 ) {

printf(“The value available for z is: %d\n”, z);

z++;

if( z > 15) {

break;

return 0;

Output:

The compilation and execution of the code mentioned above will produce a result as follows:

The value available for z is: 10

The value available for z is: 11

The value available for z is: 12

The value available for z is: 13

The value available for z is: 14

The value available for z is: 15

Is the output:

A. True

B. False

C. Both. It depends.

Answer – A. True

2. Take a look at the following program:

int main () {

int z = 10;

do {
if( z == 15) {

z = z + 1;

continue;

printf(“The value of the variable z is: %d\n”, z);

z++;

} while( z < 20 );

return 0;

Output:

The compilation and execution of the code mentioned above will produce a result as follows:

The value of the variable z is: 20

The value of the variable z is: 21

The value of the variable z is: 22

The value of the variable z is: 23

The value of the variable z is: 24

The value of the variable z is: 26

The value of the variable z is: 27

The value of the variable z is: 28

The value of the variable z is: 29

Is the output:

A. True

B. False

C. Both. It depends.

Answer – B. False.

The compilation and execution of the code mentioned above will produce a result as follows:

The value of the variable z is: 10

The value of the variable z is: 11

The value of the variable z is: 12

The value of the variable z is: 13

The value of the variable z is: 14


The value of the variable z is: 16

The value of the variable z is: 17

The value of the variable z is: 18

The value of the variable z is: 19

3. Take a look at the following program:

#include <stdio.h>

int main () {

int z = 10;

LOOP:do {

if( z == 15) {

z = z + 1;

goto LOOP;

printf(“value of z: %d\n”, z);

z++;

}while( z < 20 );

return 0;

What will be the output obtained after the compilation and execution of the code mentioned
above?

Answer – The output obtained will be as follows:-

The value of z will be: 10

The value of z will be: 11

The value of z will be: 12

The value of z will be: 13

The value of z will be: 14

The value of z will be: 16

The value of z will be: 17

The value of z will be: 18

The value of z will be: 19


FAQs

What is the relationship between the loops and the loop control
statement?

In programming, we sometimes have to perform the very same action/ program over and over
again. This need is met by a mechanism known as the loop. Sometimes, users would want to
break out of any given loop. It is known as loop control.

The loop control statements help in changing the execution of a loop from its original sequence.
Whenever the execution of the control statement takes place in a loop, the loop leaves the scope.
Thus, in this case, all the objects created automatically in the scope will get ultimately destroyed
in no time.

In simpler words, we use the loop control statements in C language for performing various loop
operations until we find the condition given in a program to be true. The control comes out of a
loop statement when the condition given to us turns out to be false.

Looping is basically a phase in which we repeat the very same process multiple times unless it
specifies any specific type of condition.

Why is the use of the Goto statement not very advisable?

The usage of the goto statement is not very much advisable in any programming language. It is
generally discouraged because this type of statement makes it very difficult for any user to trace
the overall flow of any program.

Thus, the program becomes very hard to understand in this case- and eventually becomes very
difficult to modify as well. But we must remember that we can rewrite a program at any time if it
has utilized the goto statement.

You might also like