For Loop in C
For Loop in C
T Statement T
Condition Statement
list Condition
list
F F
Types of Loop
There are three methods by way of which we can repeat a part of a program.
They are:
(a) Using a for statement
(b) Using a while statement
(c) Using a do-while statement
statement_list;
}
The for Loop
The syntax of a for loop in C programming language is −
3. After the body of the 'for' loop executes, the flow of control jumps back up to the
increment/modication statement.
This statement allows you to update any loop control variables.
4. The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the 'for' loop terminates.
Working of for loop in C
1 int main() {
2 int i, sum = 0;
3 for(i = 0; i < 11; i++) {
4 printf(“%d”, i);
5 sum = sum + i;
6 }
7 printf(“%d”, sum);
8 return 0;
9 }
The for loop
for(control-variable initialization; continuation condition; increment/decrement-
control variable ) {
// body of loop
}
e
}
Code to display integers from 1 -10
#include<stdio.h>
Note: The curly brackets { } , can be
skipped if the for loop has only 1
statement.
int main() { If the brackets are missing then
int i; ONLY very next statement will be
iterated
for(i = 1; i <= 10; i++)
printf("%d\n", i);
}
Try following now:
Factorial of n is n*(n-1)*(n-2)*...2*1
int i, n, f=1;
printf(“Enter a number to find factorial”);
scanf(“%d”,&n);
for (i=1; i<=n; ++i) {
f = f* i;
}
printf(“The factorial of %d is %d ”,n,f);
Additional features of for loop
The comma operator
We can give several statements separated by commas in place of
“Initialization”, “condition”, and “updating”.
Example
for (fact=1, i=1; i<=10; i++)
fact = fact * i;
}
for(expr1; expr2; expr3)
Most commonly, expr1 and expr3 are assignments or function calls and
expr2 is a relational expression.
Any of the three parts can be omitted, although the semicolons must
remain.
• Break statement are used for terminates any type of loop e.g, while loop, do while loop or for loop.
• The break statement terminates the loop body immediately and passes control to the next statement
after the loop.
• In case of inner loops, it terminates the control of inner loop only.
Continue Statement in C
do {
printf(“Will you pass this exam?”);
scanf(“%c”,&ans);
printf(“Great?”);
break;
} while (true);
....
24
break/ continue
int i = 0; int i = 0; int i = 0;
for(; ;) { for(; i <= 10;) { for(; ; i =i + 1) {
if(i > 10) printf(“%d”, i); if(i > 10)
break; i = i +1; break;
else } else
continue; continue;
printf(“%d”, i); printf(“%d”, i);
i = i +1;
} }
Write a code to keep accepting numbers and adding them till the sum
exceeds 100
include<stdio.h>
int main() {
int i, sum,n;
for(i = 1, sum = 0; ; i++){
scanf("%d", &n);
sum += n;
if(sum <= 100)
continue;
else
break;
}
printf("Total numbers read = %d\nSum = %d", i, sum);
}
Nested loops (loop in a loop)
The syntax for a nested for loop statement in C is as follows −
27
Nested loops (loop in a loop)
In Nested loop one loop is place within another loop body.
When we need to repeated loop body itself n number of times use nested loops.
Nested loops can be design upto 255 blocks.
printf(“%d%d”,a,b); //a=4,b=7
b
for (i = 0; i < a; i++){
for (j=0; j<b; j++) { *******
printf(“*”);
} a
*******
printf(“\n”); *******
} *******
28
Nested loops example
b
*
int a,b;
Printf(“Enter two values”);
a **
scanf(“%d%d”,&a,&b); ***
****
for (i = 0; i < a; i++) {
for (j = 0; j < b; j++) {
if (j > i)
break;
printf(“*”);
}
printf(“\n”);
}
Nested loops example
int a,b; b
scanf(“%d%d”,&a,&b); *************
a ************
for (int i = 0; i < a; i++) { ***********
**********
for (int j=0; j<b; j++) {
if (j < i)
printf(“ ”);
else
printf(“*”);
}
printf(“\n”);
}
Nested loops example
int a,i,j;
scanf(“%d”,a);
*
for (i = 0; i < a; i++) {
for (j=0; j<a; j++) { ***
if (j < a-i) *****
printf(" ");
else printf("*"); *******
}
*********
/* C Code Segment*/
int start , stop = 11, step = 1;
for(start = 1; start < stop; start = start +1)
printf(“%d”, start);
Syntax:
2. Write a C Program to reverse a given number. (For e.g., user entered 54823 so its reverse is 32845)
3. Write a C Program to find product of digits of a number. (For e.g., user entered 2534, product of
digits is 2 x 5 x 3 x 4 = 120 and user should not enter 0 as a digit
4. Sum of series :