100% found this document useful (1 vote)
913 views

Report Writing On Loops

Loops in C programming allow code to be repeatedly executed. There are three main types of loops: for loops, while loops, and do-while loops. For loops execute a specific number of times based on an initialization, test, and update expression. While loops test a condition and execute the loop body until the condition is false. Do-while loops always execute the loop body at least once before checking the condition. Break and continue statements can be used to exit or skip portions of a loop early.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
913 views

Report Writing On Loops

Loops in C programming allow code to be repeatedly executed. There are three main types of loops: for loops, while loops, and do-while loops. For loops execute a specific number of times based on an initialization, test, and update expression. While loops test a condition and execute the loop body until the condition is false. Do-while loops always execute the loop body at least once before checking the condition. Break and continue statements can be used to exit or skip portions of a loop early.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MSPM’s

Deogiri Institute of Engineering and Management Studies, Aurangabad


Department of Basic Science & Humanities

Seminar Report On

“Loops In C Programming”

Presented By:

Mr. Amit Sanjay Bhujang

(Roll No: 1508, Branch:CSE)

Under the Guidance of

Prof. Swati Magare

(Assistant Professor, Dept. of BSH)

Academic Year 2020-21 (Sem II)


Loops In C

In any programming language including C, loops are used to execute

a set of statements repeatedly until a particular conditions is satisfied.

How it works
The Below diagram depicts a loop execution,

Loop Entry

Test false
running…… Conditi
on
?

true

Execute
loop

Out of loop
As per the above diagram, if the test condition is true, then the loop is
executed, and if it is false then the execution breaks out of the loop. After the
loop is successfully executed the execution again starts from the loop entry
and again checks for the test condition, and this keeps on repeating.

The sequence of the statement to be executed is kept inside the curly braces {}
known as the loop body. After every execution of bod, condition is verified,
and if it is found to be true the loop body is executed again. When the condition
check returns false, the loop body is not executed, and execution
Breaks out of the loop.

Types of Loop

There are mainly two types of loops:


Entry Controlled loops: In this type of loops the test condition is tested
before entering the loop body. for Loop and while loop are entry controlled
loops.

Exit Controlled Loops: In this type of loops the test condition is tested or
evaluated at the end of loop body. Therefore, the loop body will execute at
least once, irrespective of whether the test condition is true or false. do –
while loop is exit controlled loop.
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.

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 :


Example :- Program to print “hello world” 10 times.

Output :-
While Loop

While studying for loop we have seen that the number of iterations is known
beforehand, i.e. the number of times the loop body is needed to be executed is
known to us. while loops are used in situations where we do not know the
exact number of iterations of loop beforehand. The loop execution is
terminated on the basis of test condition.

Syntax:

We have already stated that a loop is mainly consisted of three statements –


initialization expression, test expression, update expression. The syntax of the
three loops – For, while and do while mainly differs on the placement of these
three statements.

initialization expression;
while (test_expression)
{
// statements

update_expression;
}

Flow Diagram:
Example :- Program to print “hello world” 10 times.

Output :-
do while loop

In do while loops also the loop execution is terminated on the basis of test
condition. The main difference between do while loop and while loop is in do
while loop the condition is tested at the end of loop body, i.e do while loop is
exit controlled whereas the other two loops are entry controlled loops.

Note: In do while loop the loop body will execute at least once irrespective of
test condition.

Syntax :-

initialization expression;
do
{
// statements
update_expression;
}
while (test_expression);

Note: Notice the semi – colon(“;”) in the end of loop.

Flow Diagram:
Example :- Program to print “hello world” 10 times.

Output :-
Jumping Out of Loops

Sometimes, while executing a loop, it becomes necessary to skip a part of the


loop or to leave the loop as soon as certain condition becomes true. This is
known as jumping out of loop.

1) Break Statement :-

When break statement is encountered inside a loop, the loop is immediately


exited and the program continues with the statement immediately following
the loop.

2) Continue Statement :-

It causes the control to go directly to the test-condition and then continue the
loop process. On encountering continue, cursor leave the current cycle of
loop, and starts with the next cycle.

You might also like