For-Loop:
for Loops का उपयोग code के एक block को repeat करने के लिए किया जाता है। अपने program के code के block को execute करने में सक्षम होना programming में सबसे basic लेकिन useful tasks में से एक है – कई program या website जो complex output (जैसे message board) उत्पन्न करते हैं, वास्तव में केवल एक ही task को कई बार execute कर रहे हैं,
Syntax :
for(variable initialization; condition; variable update){ // code to be execute when condition is true }
Variable initialization आपको या तो एक variable declare करने और इसे एक value की अनुमतिदेता है या पहले से available variable को एक value देता है। दूसरा, condition, program को बताती है कि condition true होने पर loop को खुद को repeat करना जारी रखना चाहिए। और variable updation आपको यह बताते है कि अब आपको variable को update करना है।
Again test expression का evaluation किया जाता है। यह process तब तक चलती है जब तक कि test expression false नहीं हो जाती। जब test expression false हो जाती है, तो loop end हो जाता है।
Example : Print the number from 1 to 10
#include<stdio.h> int main(){ int i; for(1=1;i<=10;i++){ printf("%d",i); } return 0;
While loop :
While loop parenthesis () के अंदर test expression का evaluation करता है। यदि test expression true है, तो loop के body के अंदर के condition को execute किया जाता है। फिर, test Expression का फिर से evaluation किया जाता है। यह process तब तक चलती है जब तक कि test expression का evaluation false नहीं हो जाता। यदि test expression false है, तो loop end (समाप्त) हो जाता है।
Syntax :
while(test expression){ // body of loop }
Example : Print number from 1 to 5
#include<stdio.h> int main(){ int i = 1; while(i<=5){ printf("%d",i); ++i; } return 0; }
Do-while Loop :
do-while एक important difference के साथ, while loop के समान है। do-while loop की body कम से कम एक बार execute होता है। After, test expression का evaluation किया जाता है। यह process तब तक चलती है जब तक किtest expression गलत न हो जाए। यदि test expression गलत है, तो loop end हो जाता है।
Syntax:
do{ // the body of loop } while(expression)
Example : Add the number until we enter 0.
#include <stdio.h>
int main() {
double number, sum = 0;
// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}