[go: up one dir, main page]

0% found this document useful (0 votes)
11 views9 pages

Looping

Looping in c

Uploaded by

roveenasyam
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)
11 views9 pages

Looping

Looping in c

Uploaded by

roveenasyam
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/ 9

Looping

28 December 2022 12:38 AM

What is Loop in C?

• Looping Statements in C execute the sequence of statements many times until the stated condition
becomes false.

• A loop in C consists of two parts, a body of a loop and a control statement.

• The control statement is a combination of some conditions that direct the body of the loop to
execute until the specified condition becomes false.

• The purpose of the C loop is to repeat the same code a number of times.

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:
1. Entry controlled loop
2. Exit controlled loop
• In an entry control loop in C, a condition is checked before executing the body of a loop. It is also
called as a pre-checking loop.
• In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called
as a post-checking loop.

• The control conditions must be well defined and specified otherwise the loop will execute an infinite
number of times.
• The loop that does not stop executing and processes the statements number of times is called as
an infinite loop. An infinite loop is also called as an “Endless loop.”

• Following are some characteristics of an infinite loop:

○ No termination condition is specified.


○ The specified conditions never meet.

The specified condition determines whether to execute the loop body or not.
'C’ programming language provides us with three types of loop constructs:
1. The while loop
2. The do-while loop
3. The for loop

Sr. Loop Type Description


No.
1. While Loop In while loop, a condition is evaluated before processing a body of
the loop. If a condition is true then and only then the body of a loop is
executed.
2. Do-While In a do…while loop, the condition is always executed after the body
Loop of a loop. It is also called an exit-controlled loop.
3. For Loop In a for loop, the initial value is performed only once, then the
condition tests and compares the counter to a fixed value after each
iteration, stopping the for loop when false is returned.

1. while loop in C

The while loop is an entry controlled loop. It is completed in 3 steps.

• Variable initialization.(e.g. int x = 0;)


• condition(e.g. while(x <= 10))
• Variable increment or decrement ( x++ or x-- or x = x + 2 )
Syntax of while Loop:

How while loop works?


• The while loop evaluates the condition inside the parentheses ().

• If condition is true, statements inside the body of while loop are executed. Then, condition is
evaluated again.

• The process goes on until condition is evaluated to false.

• If condition is false, the loop terminates (ends).

Looping Page 1
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.

• In this loop, the statements the loop need to be executed at least once.

• After that, it checks the condition. If the condition is true, it will again have executed the loop; otherwise,
it will exit it.

• It is known as an exit-controlled loop.

• The while loop is performed only when the condition is true, but sometimes the statement must be
conducted at least once, so the do-while loop has to be used.

• The difference between while and do-while loop is that in the while loop, while is written in the
beginning, and do-while, the condition is mentioned at the end and ends with a semicolon (;).

Explanation of terminologies used:


• initialization : Generally we use an iterator variable (like int i = 0;) in the initialization statement to use
this variable in the update expression and in the exit condition of the loop.
• instruction(s) : It has some set of statements that we want to repeat and an update expression, these
are known as the body of the loop.
• update expression : In general, it is some increment/decrement statement of initialized variable
related to the condition at the bottom of the loop.
• condition : A boolean expression (either true or false) passed at the bottom of the loop to determine
if the loop should repeat itself or not.
Note : There should always be a semi-colon ; at the end of a do...while() loop, otherwise the compiler will
give a syntax error.

Looping Page 2
For loop in C
• For loop is an entry controlled loop i.e. the condition is checked before entering into the loop.

• So if the condition is false for the first time, the statements inside while loop may not be executed at all.

• In order to exit from a for loop, either the condition should be false or a break statement should be encountered.

• For loop is suitable to use when we have to run a loop for a fixed number of times.

Looping Page 3
Nested loop in C
• A loop inside another loop is called a nested loop.

• The depth of nested loop depends on the complexity of a problem.

• We can have any number of nested loops as required.

• Consider a nested loop where the outer loop runs n times and consists of another loop inside it.

• The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

Types of nested loops


 Nested while loop
 Nested do-while loop
 Nested for loop

Looping Page 4
Note: There can be mixed type of nested loop i.e. a for loop inside a while loop, or a while loop inside a do-while loop.

Looping Page 5
Looping Page 6
Looping Page 7
Looping Page 8
Looping Page 9

You might also like