What are loopsin C?
Loops in C programming are used to repeat a block of code until the
specified condition is met. It allows programmers to execute a
statement or group of statements multiple times without writing the
code again and again.
• Types of Loops in C
3.
Why do weneed loop in any programing
• there is often a need for repeating the same part of the code multiple times. For example, to print a text
three times, we have to use printf() three times as shown in the code:
• #include <stdio.h>
• int main() {
• printf( "Hello GfGn");
• printf( "Hello GfGn");
• printf( "Hello GfG");
• return 0;
• }
• Output
• Hello GfG
• Hello GfG
• Hello GfG
4.
Problem is
• Butif we say to write this 20 times, it will take some time to write statement. Now imagine writing it
100 or 1000 times. Then it becomes a really hectic task to write same statements again and again. To
solve such kind of problems, we have loops in programming languages.
• #include <stdio.h>
• int main() {
•
• // Loop to print "Hello GfG" 3 times
• for (int i = 0; i < 3; i++) {
• printf("Hello GfGn");
• }
• return 0;
• }
5.
for Loop
• Forloop is an entry-controlled loop, which means that the condition is checked before the loop's body
executes.
#include <stdio.h>
int main() {
// Loop to print numbers from 1 to 5
for (int i = 0; i < 5; i++) {
printf( "%d ", i + 1);
}
return 0;
}
Output : 12345
While loop
while Loopis also an entry-controlled loop in which the condition is checked before entering the body.
#include <stdio.h>
int main() {
// Initialization expression
int i = 0;
// Test expression
while(i <= 5) {
printf("%d ", i + 1);
// update expression
i++;
}
return 0;
}
1 2 3 4 5 6
do-while Loop
The do-whileloop is an exit-controlled loop, which means that the
condition is checked after executing the loop body. Due to this, the loop
body will execute at least once irrespective of the test condition.
Syntax:
do {
// Body of the loop
} while (condition);
Like while loop, only the condition is the part of do while loop syntax,
we have to do the initialization and updating of loop variable manually.
10.
#include <stdio.h>
int main(){
// Initialization expression
int i = 0;
do
{
// loop body
printf( "%d ", i);
// Update expression
i++;
// Condition to check
} while (i <= 10);
return 0;
}
Output:
0 1 2 3 4 5 6 7 8 9 10
Infinite Loop
An infiniteloop 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.
For Example
13.
Using for loopfor infinite result
#include <stdio.h>
int main () {
// This is an infinite for loop
// as the condition expression
// is blank
for ( ; ; ) {
printf("This loop will run forever.");
}
return 0;
}
14.
Output
• This loopwill run forever.
This loop will run forever.
This loop will run forever.
...
15.
Using while loop
#include<stdio.h>
int main() {
while (1)
printf("This loop will run forever.n");
return 0;
}
16.
Nested Loops
• Nestingloops means placing one loop inside another.
• The inner loop runs fully for each iteration of the outer loop.
• This technique is helpful when you need to perform multiple
iterations within each cycle of a larger loop, like when working with a
two-dimensional array or performing tasks that require multiple levels
of iteration.
17.
#include <stdio.h>
int main(){
// Outer loop runs 3 times
for (int i = 0; i < 3; i++) {
// Inner loop runs 2 times for each
// outer loop iteration
for (int j = 0; j < 2; j++) {
printf("i = %d, j = %dn", i, j);
}
}
return 0;
}
18.
output
• i =0, j = 0
• i = 0, j = 1
• i = 1, j = 0
• i = 1, j = 1
• i = 2, j = 0
• i = 2, j = 1
19.
Make program todisplay following
1. First 10 Natural Numbers Display
2.Sum of First 10 Natural Numbers
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55
3.Display Cube of Numbers Up to an Integer
• Input number of terms : 5
Expected Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125
20.
4.Multiplication Table fora Given Integer
Input the number (Table to be calculated) : 15
Expected Output :
15 X 1 = 15
...
...
15 X 10 = 150
21.
5. Right AngleTriangle Pattern with Asterisks
Write a program in C to display a pattern like a right angle triangle using
an asterisk.
The pattern like :
*
**
***
****
23.
6.Right Angle TrianglePattern with Numbers
Write a C program to display a pattern like a right angle triangle with a
number.
The pattern like :
1
12
123
1234
24.
For more practice
•Write a C program to display a right angle triangle of asterisks with
alternating characters on each row.
• Write a C program to display an inverted right angle triangle pattern
using asterisks.
• Write a C program to display a centered right angle triangle pattern
using asterisks.
• Write a C program to print a right angle triangle pattern and then its
mirror image horizontally.