← Back to C++ Course | Chapter 4: Control Flow | Lesson 9 of 13

C++ का while Loop

एक while loop steps तब तक दोहराता है जब तक एक condition true रहती है, soup को गर्म होने तक हिलाने जैसा। यह हर round से पहले check करता है।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. loop variable update करना भूल जाना, एक infinite loop बनाते हुए।
  2. while (x < 5); के बाद एक semicolon add करना, loop body को empty बनाते हुए।
  3. < की बजाय <= इस्तेमाल करके एक off-by-one error पाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.