0% found this document useful (0 votes)
2 views17 pages

Looping Statemments in c Pptx

Looping statements in C are used to repeat a block of code based on a specified condition, enhancing code efficiency. The three primary types of loops are for loop, while loop, and do-while loop, each serving different scenarios for iteration control. Additionally, control statements like break and continue can manage loop execution flow, and nested loops allow for loops within loops.
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)
2 views17 pages

Looping Statemments in c Pptx

Looping statements in C are used to repeat a block of code based on a specified condition, enhancing code efficiency. The three primary types of loops are for loop, while loop, and do-while loop, each serving different scenarios for iteration control. Additionally, control statements like break and continue can manage loop execution flow, and nested loops allow for loops within loops.
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/ 17

LOOPING

STATEMENTS
INTELLE
LEARN
What is a Looping Statement
A looping statement in C in C? is used to repeat a
programming
block of code multiple times, depending on a specified
condition. Loops allow programmers to execute a set of
instructions as long as a condition is true, making code
more efficient by avoiding the need to write repetitive
tasks manually.
C provides three primary types of loops:
1.for loop
2.while loop
3.do-while loop
1.FOR LOOP

The for loop is used when the number of iterations is


known beforehand. It consists of three parts: initialization,
condition,
Syntax: and increment/decrement.

for (initialization; condition; increment/decrement) {


// code to be executed in each iteration
}
• Initialization: This step runs once at the start of the
loop.
• Condition: The loop runs as long as this condition is
true.
• Increment/Decrement: Updates the loop control
variable after each iteration.
Exampl
e: <stdio.h>
#include

int main() {
int i; Outpu
for (i = 1; i <= 5; i++) { t:
Iteratio
printf("Iteration %d\n", i); n1
} Iteratio
n2
return 0;
}
Iteratio
n 3 with i = 1 and continues
In this example, the loop starts
until i <= 5. After each iteration, i is incremented by 1.
Iteratio
n4
2. WHILE LOOP
The while loop is used when the number of iterations is
not known beforehand and depends on a condition. It
keeps executing the loop as long as the condition is true.

Syntax:
while (condition) {
// code to be executed in each iteration
}
Exampl
e:<stdio.h>
#include

int main() {
int i = 1;
Outpu
while (i <= 5) {
printf("Iteration %d\n", i); t:
Iteratio
i++; // Increment the n1
control variable
}
Iteratio
n2
return 0;
Iteratio
}
n 3the condition i <= 5 before each
In this case, the loop checks
iteration. Once i exceeds 5, the loop stops.
Iteratio
n4
3. DO WHILE LOOP
The do-while loop is similar to the while loop, but the key
difference is that the condition is checked after the block
of code is executed. This guarantees that the loop runs at
least once, even if the condition is initially false.
Syntax:

do {
// code to be executed
} while (condition);
Exampl
e:
#include <stdio.h>

int main() {
int i = 1; Outpu
do { t:
Iteratio
printf("Iteration %d\n", i); n1
i++; // Increment the Iteratio
control variable
n2
} while (i <= 5);
Iteratio
return 0; n3
Here, the loop executes the block of code first and checks the
} condition later. This ensures that the loop runs at least once,
Iteratio
even if the condition becomes false after the first iteration.
n4
4. CONTROL STATEMENTS IN
LOOPS
Sometimes, you need more control over loop execution. C
provides control statements like break and continue to
manage the flow of loops.

1. break Statement
• The break statement is used to exit a loop
prematurely, usually when a certain condition is met.
• It causes the loop to terminate immediately,
regardless of the original loop condition.
Syntax:

break;
Exampl
e:<stdio.h>
#include

int main() {
int i;
Outpu
for (i = 1; i <= 10; i++) {
if (i == 5) { t:
Iteratio
break; // Exit the loop when n1
i is 5
} Iteratio
printf("Iteration %d\n", i); n2
}
Here, the loop terminates Iteratio
when i equals 5, so only iterations
from 1 to 4 are printed.
return 0; n3
}
Iteratio
n4
2. continue Statement
• The continue statement skips the rest of the code
inside the loop for the current iteration and proceeds
to the next iteration.
• It doesn’t terminate the loop; it just skips to the next
Syntax:
iteration.

continue;
Exampl
e:<stdio.h>
#include

int main() {
int i;
Outpu
for (i = 1; i <= 5; i++) {
if (i == 3) { t:
Iteratio
continue; // Skip the n1
iteration when i is 3
} Iteratio
printf("Iteration %d\n", i); n2
}
Iteratio
Here, the loop skips printing "Iteration 3" and continues
with the next iterations.
return 0; n4
}
Iteratio
n5
5. NESTED LOOPS

A nested loop is a loop inside another loop. The inner loop


will complete all its iterations for every single iteration of
the outer loop.
Exampl
e:<stdio.h>
#include

int main() {
int i, j;
Outpu
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 2; j++) { i =t:
1, j
printf("i = %d, j = %d\n", i, =1
j);
} i = 1, j
} =2
return 0; i = 2, j
} =1
In this example, for each iteration of the outer loop (with i values from 1
i =1 2,
to 3), the inner loop (with j values from j runs completely.
to 2)
=2
6. DIFFERENCE BETWEEN
LOOPS
1.for Loop:
• Best used when the number of iterations is known in
advance.
• All loop control statements (initialization, condition,
increment) are located in one place.
1.while Loop:
• Used when the number of iterations is not known
beforehand.
• The loop might not run at all if the condition is false
from the beginning.
1.do-while Loop:
• Ensures that the loop body is executed at least once,
even if the condition is false initially.
Thank
you!!

You might also like