for Loop in C programming language
In this article, I will explain usage of for loop in C. You will understand the syntax of for loop and learn how for loop works in C language. Let’s get started.
In while and do-while statements, we need to write logic to repeatedly execute a block of statement by initializing a counter and incrementing it after each set of steps. This sometime looks tedious and decreases the readability of the program. The readability of the C program can be improved by using C for loop. Using for loop in C, we can merge all the three parts i.e. assignment, condition checking and increment/decrementing into one.
C for loop Syntax:
for(initialization; test condition; increment/decrement){ /*block of statement*/ }
Initialization: setting up a loop counter to initial value
test condition: Testing the loop counter to determine whether the loop needs to be executed or not.
increment/decrement: Increment or decrement the loop counter value
Explanation:
The for loop is executed as follows:
-
- The initial counter value is initialized. This initialization is done only once for the entire for loop
- After the initialization, test condition is checked. Test condition can be any relational or logical expression. If the test condition is satisfied i.e. the condition evaluates to true then the block of statement inside the for loop is executed
- After the execution of the block of statement, increment/decrement of the counter is done. After performing this, the test condition is again evaluated. The step 2 and 3 are repeated till the test condition returns false
The example of while loop (while Loop in C) can re-written using for loop in C as follows:
#include<stdio.h> #include<stdlib.h> int main() { int i; for(i=1; i<=5;i++) { printf("%d This will be repeated 5 times\n", i); } printf("End of the program"); return 0; }
Output:
1 This will be repeated 5 times 2 This will be repeated 5 times 3 This will be repeated 5 times 4 This will be repeated 5 times 5 This will be repeated 5 times End of the program
In the above program, value 1 is assigned to i. This loop will be executed till the value of i is less than or equal to 5.
Note:It is necessary to write the semicolon in for loop as shown in the below:
for(i=0; i<10; i++)
Consider,
for(i=0; i<10;) { printf(“Interment/decrement not used above”) i=i+1; }
In the above program, Increment is done within the body of for loop. Still semicolon is necessary after the test condition.
Consider,
i=0; for(; i<10; i++) { printf(“Interment/decrement not used above”) }
In the above program, Initialization is done before the start of for loop in C. Still semicolon is necessary before the test condition.
Note: We can initialize multiple variables in initialization section but only one test condition is allowed.
i ant prog. for o/p like
* * *
*
* * *
and
7 8 9
7 8
7
for(i=9;i>=7;i–)
{
for(j=7;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
for(i=0; i<10;){
printf(
Yeah its valid increment/decrement can be used anywhere inside body of the loop ….
/* program for 7 8 9
7 8
7 */
#include
#include
void main()
{
int i,j,n;
clrscr();
for(i=1; i<=3;i++)
{
n=7;
for(j=i; j<=3;j++)
{ printf(“%d”,n);
n++;
}
}
getch();
}
/* program for 7 8 9
7 8
7 */
#include
#include
void main()
{ int i,j,n;
clrscr();
for(i=1;i<=3;i++) {
n=7;
for(j=i;j<=3;j++)
{ printf(“%d”,n);
n++;
}
printf(“\n”);
} getch();
}
the program for 7 8 9 is not excutinf Turbo C
please provide me the Correct answer…
No that correct sorry and can please explain each line how it is excuting
* * *
* *
*
how to get this output
Hello,
You can work on the below logic:
for(i=2; i>=0; i–){
for(starsperline = 3; starsperline>=1; starsperline –){
print *;
}
print new line;
}
The above is just an logic… correct the syntax and run the program. Any issues let me know…
Thanks,
LearnCOnline
if(i=1;i<=5;i++)
what will be the o/p?
can you please put some theory about..Files and files handling modes,, Unions more theories about loop…..
i want coding for finding gcd of three numbers…
i have an doubt for the program printing *
in ur program actually starsperline must be depend upon i something like starperline= i+1. i am not sure but have an doubt
i want coding from for loop 1 9 2 8 3 74 6 5 5 6 4 7 3 8 2 9 1
this count 1 to 9 and in back word 9 to 1….do you know how?
i wait your answer……………………?
for(i=0,j=10;i<10&&j<0;i++,j++)
pf("%d%d",i,j);
use this code to get output as 192837465564738291:——->
#include
#include
void main()
{
int i,j;//declare two variables
clrscr();//to clear previous result
for(i=1,j=9;i<=9,j>=1;i++,j–)//i will execute 1 to 9 and j will 9 to 1
{
printf(“%d%d”,i,j);//print the result of i and j
}
getch();//to hold the output screen
}
sir i want to know that what is difference in use do ,for and while statement/case please tell me i am waiting for your answer.
I want a code to appear in this format
54321
5432
543
54
5
by use of C program
@Naomi: how about this?
#include
void main(void)
{
int i, j;
int k = 1;
for (i = 1; i <= 5; i++) {
for (j = 5; j >= k; j–) {
printf(“%d”, j);
}
k++;
printf(“\n”);
}
}
how this program is execute tell me in detail
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
using for loop
thank u Kamboj.iam new to this.your explained line by line.i want out put like this
1
2 1 2
3 2 1 3
can u explain line by line coding of above out put
@Christopher
without taking k variable we can write code for
54321
5432
543
54
5
#include
#include
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j–)
printf(“%d”,j);
printf(“\n”);
}
getch();
}
try this code, that will help :
#include
int main()
{
int i,j;
for(i=1;i<=5;i++){ for(j=1;j<=i;j++)
//(i=1,j=9;i<=9,j>=1;i++,j–)//
printf(“%d \n”,j);
}
}
@abhishek kumar :
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf(“%d”,i);
}
printf(“\n”);
}
write program using for loop?
* * *
*
* * *
which codes should I use to have the output of:
1x2x3x4x5x6x7x8x9x10
and to get its factorial?
give me an example of if statement
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
viper0
astlem 🙂
#includ
main()
{
int i,g;
i=2;
g=12;
if(i==g)
printf(“its not right ofcorse”);
if(i<g)
printf("that is rigth !\n");
}
lala
take this 🙂
#include
int main()
{
int i,j,fact=1;
for(i=1,j=1;i<11,j<11;i++,j++)
{
printf("%d*",i); \\this line for print the factory
fact *=j;
}
printf("\n");
printf("fact=%d",fact);
}
Surbhi
>>
#include
main()
{
int i,j,k;
for(i=2,k=1;i<7,k<6;i++,k++)
{
for(j=1;j<i;j++)
printf("%d\t",j*k);
printf("\n");
}
}
how about this..please help me.
Write a program that asks the user to type 5 random integers below 10 and identify the largest value.
You can use the below logic.
Accept 5 integers from the user and store it in an array. Once you have the data in an array you can apply sorting algorithm on it. Sort an array in ascending or descending order. If sorted in ascending order, the last element would be largest.
Thanks,
Aptuts Team.
http://www.aptuts.com
Online Webinar C Coaching
how to get this output
*
**
***
You can visit the below link for the solution:
https://www.learnconline.com/2015/05/c-program-to-display-right-angled-triangle-of-star-asterisk.html
#include
#include
void main()
{
int i,j;
clrscr();
if(i=1;i<=3;i++)
{
if(j=1;j<i;j++)
{
printf("*");
}
printf(" ");
getch();
}
how about this
1
234
5678
876
543
1