Skip to main content
WHILE LOOP EXAMPLE IN C LANGUAGE
Simple example on how while loop works.
Syntax:while(condition) {}Condition : A condition is assigned to tell while loop that till when it should continue its loop.Example to print 10 even numbers.#include<stdio.h>int main(){int num = 0;while(num <=10) //Repeat this block { if(num%2 == 0) { printf(" %d ",num); }num++;// This simply means num = num +1}return 0;}Output:
Description:
- In this example 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.
- while(num <=10) { }- this statement repeats whatever is written inside its block 11 times.
- i.e 0 to 10 . When it goes in while loop the 11th time then
- 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 рдХреЛрдг рдХреЛрдгрддреЗ рдЖрд╣реЗ рддреЗ рдирд┐рд╡рдбрд▓реЗ рдЖрд╣реЗ.
- while(num <=10) { } - рд╣реНрдпрд╛ рдХреЛрдб рдЪрд╛ рдЕрд░реНрде рдЕрд╕рд╛ рдЖрд╣реЗ рдХрд┐ рдЬреЛ рдкрд░реНрдпрдВрдд "num" рдЪреА рдХрд┐рдВрдордд резреж рдХрд┐рд╡рд╛ резреж рдкреЗрдХреНрд╖рд╛ рд▓рд╣рд╛рди рдЕрд╕реЗрд▓ рддреЛ рдкрд░реНрдпрдВрдд "{ }" рд╣реНрдпрд╛рдд рд▓рд┐рд╣рд┐рд▓реЗрд▓рд╛ рдХрдбреЗ рд╡рд╛рд░рдВрд╡рд╛рд░ рд░рди рд╣реЛрдгрд╛рд░.
- num++ - рд╣реНрдпрд╛ рдХреЛрдб рдореБрд│реЗ num рдЪреА рдХрд┐рдВрдордд рез рдиреЗ рд╡рд╛рдврддреЗ. (num = num+рез). рд╣рд╛ рдирд╛рд╣реА рд▓рд┐рд╣рд┐рд▓рд╛ рддрд░ "num" рдЪреА рдХрд┐рдВрдордд рддрд╕реАрдЪ рд░рд╛рд╣реАрд▓ рдЖрдгрд┐ while loop рдЪрд╛рд▓рддрдЪ рд░рд╛рд╣рдгрд╛рд░.рдореНрд╣рдгреВрди рд╣рд┐ рдХрд╛рд│рдЬреА рдШреЗрддрд▓реА рдкрд╛рд╣рд┐рдЬреЗ.
- if(num%2==0) - рд╣реНрдпрд╛ рдХреЛрдбрдЪрд╛ рдЕрд░реНрде рдЖрд╣реЗ рдХрд┐ "num" рдордзреНрдпреЗ рдЕрд╕рд▓реЗрд▓реНрдпрд╛ рдЕрдВрдХрд╛рд▓рд╛ рдЬреЗрд╡реНрд╣рд╛ реи рдирд┐ рднрд╛рдЧреАрддрд▓реЗ рддрд░ рдЙрд░реНрд╡рд░рд┐рдд рднрд╛рдЧ(remainder) рд╢реБрдиреНрдп рдорд┐рд│реЗрд▓. рдЬрд░ рд╢реБрдиреНрдп рдорд┐рд│рд╛рд▓рд╛ рддрд░ рддреЛ "num" even number рдЕрд╕рд╛ рдореНрд╣рдВрдЯрд▓рд╛ рдЬрд╛рдИрд▓.
Comments