C++ का while Loop
In this page:
while (condition) {
// body
// update the loop variable
}
Basic while Loop
एक while loop अपनी condition true रहने तक repeatedly अपनी body चलाता है, हर single iteration से पहले, बिल्कुल पहले वाले से भी पहले, condition दोबारा check करते हुए — इसलिए अगर condition शुरुआत में false हो, loop body बिल्कुल भी नहीं चलती।
यह while को सही choice बनाता है जब आपको पहले से exactly नहीं पता कि आपको कितनी बार कुछ दोहराना होगा।
उदाहरण: Basic while Loop
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 0
int count = 0;
// Repeat while count < 5 is true
while (count < 5) {
// Print to the console with cout
std::cout << count << std::endl;
count++;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Decrementing Loop Conditions
हर iteration loop के अंदर एक loop counter से subtract करना, जैसे count--, आपको ऊपर की बजाय एक target की ओर नीचे count करने देता है, जो exactly वह तरीका है जिससे countdown timers और data में reverse iteration typically implement होते हैं।
Loop की condition फिर आमतौर पर zero या किसी lower bound तक पहुँचने को check करती है।
उदाहरण: Decrementing Loop Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 5
int count = 5;
// Repeat while count > 0 is true
while (count > 0) {
// Print to the console with cout
std::cout << count << std::endl;
count--;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Loop Control के लिए Boolean Flags इस्तेमाल करना
एक boolean variable को loop की condition की तरह इस्तेमाल करना, जैसे while (keepGoing), loop body के अंदर code को उस flag की value बदलकर dynamically decide करने देता है कि iteration जारी रहनी चाहिए या नहीं।
यह उन loops के लिए एक common pattern है जिनका stopping point एक simple counter की बजाय किसी runtime event पर depend करता है।
उदाहरण: Using Boolean Flags for Loop Control
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare keepGoing and set it to true
bool keepGoing = true;
// Declare count and set it to 0
int count = 0;
// Repeat while keepGoing is true
while (keepGoing) {
// Print to the console with cout
std::cout << count << std::endl;
count++;
// Check whether count >= 3
if (count >= 3) {
keepGoing = false;
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Numeric Limit Iterations
हर pass एक fixed amount add करने की बजाय एक loop variable को multiply या divide करना linear की बजाय exponential progress produce करता है — binary search जैसे algorithms के लिए useful जो बार-बार एक search range को आधा करते हैं, या समय के साथ compounding growth simulate करने के लिए।
उदाहरण: Numeric Limit Iterations
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare value and set it to 1
int value = 1;
// Repeat while value < 100 is true
while (value < 100) {
// Print to the console with cout
std::cout << value << std::endl;
value *= 2;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
break Statement इस्तेमाल करना
एक while loop के अंदर break call करना तुरंत loop से पूरी तरह exit हो जाता है, बाकी बची कोई भी iteration skip करते हुए और सीधे इसके बाद वाले code पर jump करते हुए।
यह एक specific condition मिलते ही जल्दी रोकने के लिए useful है, आगे के passes पर loop की main condition check करते रहने की बजाय जो wasted work होता।
उदाहरण: Using the break Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare count and set it to 0
int count = 0;
// Repeat while count < 10 is true
while (count < 10) {
// Check whether count == 3
if (count == 3) {
// Leave the switch/loop
break;
}
// Print to the console with cout
std::cout << count << std::endl;
count++;
}
// 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 add करना, loop body को empty बनाते हुए।<की बजाय<=इस्तेमाल करके एक off-by-one error पाना।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: