KEMBAR78
Loop in C | PDF | Control Flow | Software Engineering
0% found this document useful (0 votes)
28 views3 pages

Loop in C

The document explains loops in C programming, highlighting their benefits such as code reusability, efficiency, and structured control flow. It details the two types of loops: entry controlled (for and while loops) and exit controlled (do-while loop), along with their syntax and execution flow. Additionally, it compares while and do-while loops, emphasizing their differences in execution and condition checking.

Uploaded by

subhadip01d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Loop in C

The document explains loops in C programming, highlighting their benefits such as code reusability, efficiency, and structured control flow. It details the two types of loops: entry controlled (for and while loops) and exit controlled (do-while loop), along with their syntax and execution flow. Additionally, it compares while and do-while loops, emphasizing their differences in execution and condition checking.

Uploaded by

subhadip01d
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C – Loops

Loops in programming are used to execute a statement or a group of statements repeatedly until the
specified condition is met.
Benefits of loops:
 Code Reusability:
Loops allow you to write a block of code once and execute it multiple times, reducing
redundancy and making the program shorter and easier to maintain.
 Efficiency:
By automating repetitive tasks, loops minimize the need for manual repetition, leading to more
efficient code and faster execution.
 Structured Control Flow:
Loops provide a structured approach to controlling the flow of operations, allowing you to
easily manage the execution of code blocks based on specific conditions.
 Data Structure Iteration:
Loops are particularly useful for iterating through data structures like arrays, strings, and lists,
enabling you to access and manipulate elements efficiently.
 Flexibility:
Loops can be adapted to handle various data sizes and dynamic conditions by adjusting loop
conditions, adding control variables, or using break and continue statements.
 Readability:
Well-structured loops make code easier to understand and maintain, especially when dealing
with complex logic or large datasets.

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 supports three looping statements.


 for loop
 while loop
 do…while loop
for loop: The general syntax is:
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
In for loop, a loop variable is used to control the loop. Firstly we initialize the loop variable with some
value, and then check its test condition. If the condition is true then the body of for loop will be
executed. Steps will be repeated till the exit condition becomes true. If the test condition will be false
then it will stop.
 Initialization Expression: In this expression, we
assign a loop variable or loop counter to some
value. for example: int i=1;
 Test Expression: In this expression, test
conditions are performed. If the condition
evaluates to true then the loop body will be
executed and then an update of the loop variable
is done. If the test expression becomes false then
the control will exit from the loop. for example,
i<=9;
 Update Expression: After execution of the loop
body loop variable is updated by some value. It
could be incremented, decremented, multiplied,
or divided by any value.

Fig. Flowchart of for loop.


while loop:
In C, a while loop repeatedly executes a block of code as long as a specified condition remains true,
making it useful for tasks where the number of iterations isn't known beforehand.
Syntax:
while (condition)
{
// Code to be executed repeatedly
}
 while keyword: Indicates the start of the while loop.
 condition: An expression that evaluates to either true or false. The loop continues as long as the
condition is true.
 { }: Enclose the code block that will be executed repeatedly.
Fig. Flowchart of while loop.

do while loop:
In C, a do-while loop is a control structure that ensures a block of code executes at least once,
regardless of the condition, and then checks the condition after the execution, repeating the loop if the
condition is true.
Syntax: do {
// Code to be executed
} while (condition);
 The code within the do block is executed first.
 Then, the condition in the while statement is evaluated.
 If the condition is true, the loop repeats from the do statement.
 If the condition is false, the loop terminates.

Fig. Flowchart of do-while loop.

Difference between while and do-while loops:


while do-while

Condition is checked first then statement(s) is Statement(s) is executed atleast once, thereafter
executed. condition is checked.

It might occur statement(s) is executed zero


At least once the statement(s) is executed.
times, If condition is false.

No semicolon at the end of while.


Semicolon at the end of while. while(condition);
while(condition)

Variable in condition is initialized before the variable may be initialized before or within the
execution of loop. loop.

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition)
do {
{
statement(s);
statement(s);
} while(condition);
}

You might also like