0% found this document useful (0 votes)
13 views14 pages

Loops in C

The document provides an overview of loops in C programming, explaining their purpose and types, including entry-controlled (for and while loops) and exit-controlled (do-while loop) loops. It details the structure and syntax of each loop type, along with examples and flowcharts for better understanding. Additionally, it discusses infinite loops and their implications in programming.

Uploaded by

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

Loops in C

The document provides an overview of loops in C programming, explaining their purpose and types, including entry-controlled (for and while loops) and exit-controlled (do-while loop) loops. It details the structure and syntax of each loop type, along with examples and flowcharts for better understanding. Additionally, it discusses infinite loops and their implications in programming.

Uploaded by

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

www.sgcollege.edu.

in

Extends Heartfelt Welcome

Campus: NH-05, Chandigarh – Ludhiana State Hwy, Mohali (P.B.) -140413


TOPIC

Loops In C

PRESENTED BY PRESENTED TO

Asst Professor. Varinder Singh Class:- BCA


Department of Engineering 1st sem

www.sgcollege.edu.in
Loops
Loops in programming are used to repeat a block of code until the specified condition is met. A loop statement
allows programmers to execute a statement or group of statements multiple times without repetition of code.

www.sgcollege.edu.in
Why Looping ?
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;
}

www.sgcollege.edu.in
There are mainly two types of loops in C
I. Entry Controlled loops: In EntryProgramming:
controlled loops the test condition is checked before
entering the main body of the loop. For Loop and While Loop is Entry-controlled
loops.
II. 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.

www.sgcollege.edu.in
Loop Type Description

first Initializes, then condition check, then executes


for loop the body and at last, the update is done.

first Initializes, then condition checks, and then


while loop executes the body, and updating can be inside the
body.

do-while loop do-while first executes the body and then the
condition check is done.

www.sgcollege.edu.in
for Loop
for loop in C programming is a repetition control structure that allows
programmers to write a loop that will be executed a specific number of
times. for loop enables programmers to perform n number of steps
together in a single line.
•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.

www.sgcollege.edu.in
Example Flow Chart

1.#include<stdio.h>
2.int main(){
3.int i=0;
4.for(i=1;i<=10;i++)
{
5.printf(“Hello World\
n");
6.}
7.return 0;
8.}

www.sgcollege.edu.in
while Loop
While loop does not depend upon the number of iterations. In for loop the
number of iterations was previously known to us but in the While loop, the
execution is terminated on the basis of the test condition. If the test
condition will become false, then it will break from the while loop else
body will be executed.

Syntax:-
initialization_expression;

while (test_expression)
FlowChart:
{
// body of the while loop
-
update_expression;
}

www.sgcollege.edu.in
Example:-

#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("Hello World\
n");
i++;
}
return 0;
}

www.sgcollege.edu.in
do while Loop
The do-while loop is similar to a while loop but the only difference lies in the do-while
loop test condition which is tested at the end of the body. In the do-while loop, the loop
body will execute at least once irrespective of the test condition.

Syntax:-
initialization_expression;
do
{
FlowChart:
// body of do-while loop -
update_expression;

} while (test_expression);

www.sgcollege.edu.in
Example:-
// C program to illustrate
// do-while loop
#include <stdio.h>

// Driver code
int main()
{
// Initialization
expression
int i = 2;

do
{
// loop body
printf( "Hello World\
n");

// Update expression
i++;

// Test expression
} while (i < 1);

return 0;
} www.sgcollege.edu.in
Infinite Loop
An infinite loop is executed when the test expression never becomes false, and the body
of the loop is executed repeatedly. A program is stuck in an Infinite loop when the
condition is always true. Mostly this is an error that can be resolved by using Loop
Control statements.
// C program to demonstrate infinite
// loops using for loop
#include <stdio.h>

// Driver code
int main ()
{
int i;

// This is an infinite for loop


// as the condition expression
// is blank
for ( ; ; )
{
printf("This loop will run forever.\
n");
}

return 0;
}
www.sgcollege.edu.in
www.sgcollege.edu.in

You might also like