C Programming UNIT 3.1 Loops
C Programming UNIT 3.1 Loops
Language
UNIT 3
Unit 3 syllabus
Loop concepts
Arrays
Functions
C loops
You may encounter situations, when a
block of code needs to be executed
several times. In general, statements are
executed sequentially: The first statement
in a program is executed first, followed
by the second, and so on.
A loop statement allows us to execute a
statement or group of statements
multiple times.
// C program to illustrate need of loops
#include <stdio.h>
int main()
{ printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
return 0;
}
There are mainly two types of loops in C Programming:
1. Entry Controlled loops: In Entry controlled loops the test condition is
checked before entering the main body of the loop. For Loop and
While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is
evaluated at the end of the loop body. The loop body will execute
at least once, irrespective of whether the condition is true or
false. do-while Loop is Exit Controlled loop.
C programming language provides the following types of loops to
handle looping requirements
Syntax
The syntax for a break statement in C is as follows −
break;
Break statement flowchart
Break statement example
#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{ printf("value of a: %d\n", a);
a++;
if( a > 15)
{break; } }
return 0;
}
Continue statement in C
The continue statement in C programming works somewhat like
the break statement. Instead of forcing termination, it forces the next
iteration of the loop to take place, skipping any code in between.
For the for loop, continue statement causes the conditional test and
increment portions of the loop to execute. For
the while and do...while loops, continue statement causes the
program control to pass to the conditional tests.
Syntax
The syntax for a continue statement in C is as follows −
continue;
Continue flowchart
Continue example
#include <stdio.h>
int main ()
{
int a = 10; /* local variable definition */
do { if( a == 15) /* do loop execution */
{
a = a + 1; /* skip the iteration */
continue; }
printf("value of a: %d\n", a);
a++; }
while( a < 20 );
return 0;
}
goto statement in C
A goto statement in C programming provides an unconditional jump
from the 'goto' to a labeled statement in the same function.
Syntax
The syntax for a goto statement in C is as follows −
goto label;
.. .
label: statement;
NOTE − Use of the goto statement is highly discouraged in any
programming language because it makes it difficult to trace the
control flow of a program, making the program hard to understand
and hard to modify. Any program that uses a goto can be rewritten to
avoid them.
Goto statement flowchart
for loop in C
A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
Syntax
The syntax of a for loop in C programming language is −
for ( init; condition; increment )
{
statement(s);
}
Here is the flow of control in a 'for' loop −
1. The init step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required
to put a statement here, as long as a semicolon appears.
2. Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and the
flow of control jumps to the next statement just after the 'for' loop.
3. After the body of the 'for' loop executes, the flow of control jumps
back up to the increment statement. This statement allows you to
update any loop control variables. This statement can be left blank,
as long as a semicolon appears after the condition.
4. The condition is now evaluated again. If it is true, the loop executes
and the process repeats itself (body of loop, then increment step,
and then again condition). After the condition becomes false, the
'for' loop terminates.
For loop
flowchart
For loop example
C program for printing a multiplication table for a
given number
While loop in c
A while loop in C programming repeatedly executes a target
statement as long as a given condition is true.
Syntax
The syntax of a while loop in C programming language is −