C का for Loop
In this page:
for (initialization; condition; update) {
// body
}
for Loop क्या है?
एक for loop एक variable की initialization, इसकी exit condition, और इसका update step एक single line में combine करता है, अलग statements में बिखरे होने की बजाय loop की पूरी lifecycle को एक नज़र में visible बनाते हुए।
उदाहरण: What is a for Loop?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Standard Counter
एक for loop एक natural choice है जब भी आपको पहले से exactly पता हो कि आप कुछ कितनी बार दोहराना चाहते हैं, क्योंकि iterations की fixed संख्या को सीधे loop की condition में bake किया जा सकता है।
उदाहरण: Standard Counter
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 1; i <= 5; i++) {
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Even Numbers Print करना
एक for loop की initial value और इसके update step को adjust करना (उदाहरण के लिए 1 की बजाय 2 से increment करना) आपको loop body के अंदर एक extra if check की ज़रूरत के बिना एक range में सिर्फ even numbers print करने देता है।
उदाहरण: Printing Even Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i <= 10; i += 2) {
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Sequences Sum करना
loop के बाहर declared एक accumulator variable, हर pass पर loop counter की value add करके update होता है, एक for loop इस्तेमाल करके numbers की एक sequence sum करने का standard pattern है।
उदाहरण: Summing Sequences
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare sum and set it to 0
int sum = 0;
// Loop: repeat while the condition holds
for (int i = 1; i <= 5; i++) {
sum += i;
}
// Print formatted text to the screen
printf("%d", sum);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Decremental For Loop
एक for loop से नीचे गिनने के लिए, counter को एक high value पर शुरू करें, एक greater-than-or-equal condition इस्तेमाल करें, और हर pass पर increment की बजाय decrement करें -- same three-part structure, बस reverse में चलते हुए।
उदाहरण: Decremental For Loop
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 5; i >= 1; i--) {
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक array length के साथ
<=इस्तेमाल करना, जैसेi <= n, अंत से एक element आगे पढ़ते हुए। for (...);के बाद एक semicolon लगाना, जो body को empty बना देता है।- parentheses के अंदर semicolons की बजाय commas इस्तेमाल करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: