C का while Loop
In this page:
while (condition) {
// body
// update the loop variable
}
while Loop क्या है?
एक while loop हर iteration से पहले, यहाँ तक कि बिल्कुल पहले वाले से भी पहले, अपनी condition check करता है -- अगर condition शुरुआत में false है, loop body बिल्कुल भी नहीं चलती, एक do-while loop के उलट जो चाहे कुछ भी हो एक बार हमेशा चलता है।
उदाहरण: What is a while Loop?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 10
int x = 10;
// Repeat while x < 5 is true
while (x < 5) {
// Print formatted text to the screen
printf("never runs");
}
// Print formatted text to the screen
printf("Loop skipped since condition was false");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
गिनते हुए ऊपर जाना
हर pass में बढ़ने वाले एक counter variable का इस्तेमाल करके, एक while loop numbers की एक ascending sequence print कर सकता है -- loop से पहले counter initialize करते हुए और हर iteration पर body के अंदर इसे increment करते हुए।
उदाहरण: Counting Up
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 1
int i = 1;
// Repeat while i <= 5 is true
while (i <= 5) {
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
गिनते हुए नीचे जाना
एक counter को एक high value पर initialize करके और हर iteration इसे बढ़ाने की बजाय घटाकर, same while structure नीचे की ओर गिनता है -- countdowns या एक range को reverse में process करने के लिए useful।
उदाहरण: Counting Down
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 5
int i = 5;
// Repeat while i >= 1 is true
while (i >= 1) {
// Print formatted text to the screen
printf("%d ", i);
i--;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
पहले N Numbers का Sum
एक while loop के अंदर एक running total accumulate करना -- हर pass पर current counter value को एक अलग sum variable में add करना -- पहले N integers के sum जैसी चीज़ compute करने का standard pattern है।
उदाहरण: Sum of First N Numbers
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int i = 1, sum = 0;
// Repeat while i <= 5 is true
while (i <= 5) {
sum += i;
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
Infinite Loop Prevention
अगर loop की condition कभी false नहीं होती -- आमतौर पर क्योंकि आप जिस variable पर यह depend करती है उसे update करना भूल गए -- loop हमेशा के लिए चलता है, program को freeze करते हुए; हमेशा double check करें कि loop के अंदर हर code path condition को false होने की तरफ move करता है।
उदाहरण: Infinite Loop Prevention
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare i and set it to 0
int i = 0;
// Repeat while i < 5 is true
while (i < 5) {
// Print formatted text to the screen
printf("%d ", i);
i++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- loop variable को update करना भूल जाना, एक infinite loop बनाते हुए।
while (x < 5);के बाद एक semicolon लगाना, loop body को empty बनाते हुए।<की बजाय<=इस्तेमाल करके एक off-by-one error पाना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: