For loop in C
For loop in C
Hello! In today’s blog we will
learn about looping in C with help example.
Imagine you want to print your
name 100 times on screen through code, so what will you do you will write print
line 100 times, but what if I say no need to write hundred line of code instead
of writing hundred line you can do that work in two lines. you will say are you
kidding me no I am not joking that’s correct that you can do that complicated work
in such a less number of line.
For that we use looping, in programming,
a loop is used to repeat a block of code until the specified condition is met.
![]() |
| for loop.smartengineer |
C programming has three type of loops:
1.for loop
2.while loop
3.do…while loop
We will learn about for loop
in this blog. In the next blog we will learn about while and do…while loop.
For Loop:-
The syntax of the for loop is:
for (initialization; test
expression; update expression)
{
// statements inside the body of loop
}
Working of for loop?
1.the initialization statement
is to be run only once.
2.after this test expression
is evaluated if the test expression is false then for loop is going to terminated
.
3. however, if the test
expression is evaluated to true, statements inside the body of for loop are
executed, and the update expression is updated.
4.after this again the test
expression is evaluated.
This process goes on until the
test condition is going to be false. when the test condition is false, loop
terminates.
Some very simple example using for loop
1.#include<stdio.h>
main()
{
int n;
for(n=10;n>0;n--)
{
printf("%d \n",n);
}
return 0;
}
2.// Program to calculate the
sum of first n natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer:
");
scanf("%d", &num);
// for loop terminates when num is less
than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
3. // for print table of 1....
#include<stdio.h>
int main(){
int i=0;
for(i=1;i<=10;i++){
printf("%d
\n",i);
}
return 0;
}
Advantages of using for loop:-
1.It provides code
reusability.
2.Using loops, we do not need
to write the same code again and again.
3.Using loops, we can traverse
over the elements of data structures.
For practice purpose I am dropping some question that you can try by for loop.
1.Write a program in C to
display the first 10 natural numbers.
2. Write a program in C to read 10 numbers from keyboard
and find their sum and average.
3.write a program in c for calculating
factorial of a number given by user.
4. Write a program to display the pattern like right
angle triangle using an asterisk.
5. Write a program to display the n terms of even natural
number and their sum.
MCQs based on the for loop:-
1) Choose a right C Statements.
b) Loop is usually executed as long as a condition is
met.
c) Loops usually take advantage of Loop Counter
d) All the above.
answer-d
2) Loops in C Language are implemented using.
b) For
c) Do While
d) All the above
answer-d
3) Which loop is faster in C Language, for, while or
Do While.?
b) while
c) do while
d) All work at same
answer-d
4. The C code ‘for(;;)’ represents an infinite loop.
It can be terminated by?
a) break
b) exit(0)
c) abort()
d) terminate
answer-a
5.Which for loop has range of similar indexes of ‘i’
used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)
answer-d
6. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int k =
0;
for (k)
printf("Hello");
}
a) Compile time error
b) hello
c) Nothing
d) Variess
answer-a
7. What will be the output of the following C code?
#include <stdio.h>
void main()
{
double k
= 0;
for (k =
0.0; k < 3.0; k++)
printf("Hello");
}
a) error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
answer-b
8. What will be the output of following program ?
#include <stdio.h>
void main()
{
char cnt=0;
for(;cnt++;printf("%d",cnt)) ;
printf("%d",cnt);
}
a)0 1 2 ... infinite times
b)0 1 2 ... 127
c)0
d)1
answer-d
9. Which is not a loop structure?
a) For
b) Do while
c) While
d) Repeat Until
answer- d
10. What is the final value of x when the code int x;
for(x=0; x<10; x++) {} is run?
a) 10
b) 9
c) 0
d) 1
answer-a
Read about my previous blog the switch statement....

Informative 👏👏
ReplyDeletevery informative content ....
ReplyDelete