Skip to main content
FOR LOOP EXAMPLE IN C LANGUAGE
Simple example on how "for loop" works.
Syntax:
for( initialisation ; condition ; incrementation or decrementation) { }
- Initialisation - Initialise any value
- Condition - Basically its till when the loop should continue. (Ex: number < 10)
- incrementation or decrementation : To increment or decrement any value.
Example to print 10 even numbers.
#include<stdio.h>
int main()
{
int num;
for(num=0;num<10;num++) //Repeat this block
{
if(num%2 == 0)
{
printf(" %d ",num);
}
}
return 0;
}
Output:
0 2 4 6 8
Description:
In this example is same as while loop.We first repeat some block of statements till the value of "num" variable reaches to 10.Then we check if the current number is even and print it.
for(num=0;num<10;num++) { }- this statement first assigns zero value to num.
Step 1:Then condition is checked.Pointer then enters the for loop block if condition is true / satisfied.
Step 2:It then executes everything inside the block as long as condition is satisfied.
Step 3:Then after the block is executed once value of num gets incremented.
Step 4: Then if condition is true Step 1 is repeated. If condition is false then pointer comes out from the for loop.
if(num%2==0) - This statement means that when number is divided by 2 then remainder is zero. This is simply to check if value in "num" variable is even or not.
num++ : This means num = num +1;
Description in marathi /рдорд░рд╛рдареАрдд рд╡рд░реНрдгрди:
рд╣рд╛ while loop рдЪрд╛ рдЙрджрд╣рд░рдг рдЖрд╣реЗ. рд╣реНрдпрд╛ рдЙрдзрд░реНрдирд╛рдд рез рддреЗ резреж рдЕрдВрдХрд╛рди рдордзреНрдпреЗ even numbers (2,4,6,8,10......) рдХреЛрдг рдХреЛрдгрддреЗ рдЖрд╣реЗ рддреЗ рдирд┐рд╡рдбрд▓реЗ рдЖрд╣реЗ.
for(num=0;num<10;num++) - рд╣реНрдпрд╛рдд рей рдШрдЯрдХ рдЖрд╣реЗрдд.
рез - рдкрд╛рд╣рд┐рд▓рд╛рдВрджрд╛ "num" рд▓рд╛ рд╢реБрдиреНрдп рдХрд┐рдВрдордд рдард░рд╡рд▓реА рдЖрд╣реЗ.
реи - рдордЧ рдЕрдЯ рдЕрд╢реА рдЖрд╣реЗ рдХрд┐ "num" рдЪреА рдХрд┐рдВрдордд резреж рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдЕрд╕рд▓реА рдкрд╛рд╣рд┐рдЬреЗ.
рей - рдЬрд░ рдЕрдЯ рдмрд░реЛрдмрд░ рдЕрд╕реЗрд▓ рддрд░ рддреНрдпрд╛ рдлреЙрд░ рд▓реВрдк рдордзреНрдпреЗ рдЬреЗ рд▓рд┐рд╣рд┐рд▓реЗрд▓рд╛ рдЕрд╕реЗрд▓ рддреЗ рд░рди рд╣реЛрдгрд╛рд░.
рек - рдлреЙрд░ рд▓реВрдк рдПрдХрджрд╛ рд░рди рдЭрд▓реНрдпрд╛рд╡рд░ "num" рдЪреА рдХрд┐рдВрдордд рез рдиреЗ рд╡рд╛рдврд╡рд▓реА рдЬрд╛рддреЗ.
рел - рдкрд░рдд рддреА рдЕрдЯ рддрдкрд╛рд╕рд▓реА рдЬрд╛рддреЗ. рдЬрд░ рдЕрдЯ рдмрд░реЛрдмрд░ рдЕрд╕реЗрд▓ рддрд░ рддрд┐рд╕рд░реА рдУрд│(рд╣реНрдпрд╛ рдкрд░рд┐рдЪреНрдЫреЗрдж) рдкреБрдиреНрд╣рд╛ рдкрд╛рд│рд▓реА рдЬрд╛рддреЗ.рдкрдг рдЬрд░ рдЕрдХ рдЪреБрдХреАрдЪреА рдЕрд╕реЗрд▓ рддрд░ рдкреЛрдЗрдВрддрд░ рд▓реВрдкрдЪреНрдпрд╛ рдмрд╛рд╣реЗрд░ рдпреЗрддреЛ.
num++ - рд╣реНрдпрд╛ рдХреЛрдб рдореБрд│реЗ num рдЪреА рдХрд┐рдВрдордд рез рдиреЗ рд╡рд╛рдврддреЗ. (num = num+рез). рд╣рд╛ рдирд╛рд╣реА рд▓рд┐рд╣рд┐рд▓рд╛ рддрд░ "num" рдЪреА рдХрд┐рдВрдордд рддрд╕реАрдЪ рд░рд╛рд╣реАрд▓ рдЖрдгрд┐ while loop рдЪрд╛рд▓рддрдЪ рд░рд╛рд╣рдгрд╛рд░.рдореНрд╣рдгреВрди рд╣рд┐ рдХрд╛рд│рдЬреА рдШреЗрддрд▓реА рдкрд╛рд╣рд┐рдЬреЗ.
if(num%2==0) - рд╣реНрдпрд╛ рдХреЛрдбрдЪрд╛ рдЕрд░реНрде рдЖрд╣реЗ рдХрд┐ "num" рдордзреНрдпреЗ рдЕрд╕рд▓реЗрд▓реНрдпрд╛ рдЕрдВрдХрд╛рд▓рд╛ рдЬреЗрд╡реНрд╣рд╛ реи рдирд┐ рднрд╛рдЧреАрддрд▓реЗ рддрд░ рдЙрд░реНрд╡рд░рд┐рдд рднрд╛рдЧ(remainder) рд╢реБрдиреНрдп рдорд┐рд│реЗрд▓. рдЬрд░ рд╢реБрдиреНрдп рдорд┐рд│рд╛рд▓рд╛ рддрд░ рддреЛ "num" even number рдЕрд╕рд╛ рдореНрд╣рдВрдЯрд▓рд╛ рдЬрд╛рдИрд▓.
Comments