← Back to C Course | Chapter 4: Control Flow | Lesson 7 of 11

C का while Loop

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

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

गिनते हुए ऊपर जाना

हर pass में बढ़ने वाले एक counter variable का इस्तेमाल करके, एक while loop numbers की एक ascending sequence print कर सकता है -- loop से पहले counter initialize करते हुए और हर iteration पर body के अंदर इसे increment करते हुए।

उदाहरण: Counting Up

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

गिनते हुए नीचे जाना

एक counter को एक high value पर initialize करके और हर iteration इसे बढ़ाने की बजाय घटाकर, same while structure नीचे की ओर गिनता है -- countdowns या एक range को reverse में process करने के लिए useful।

उदाहरण: Counting Down

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

पहले 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

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

Infinite Loop Prevention

अगर loop की condition कभी false नहीं होती -- आमतौर पर क्योंकि आप जिस variable पर यह depend करती है उसे update करना भूल गए -- loop हमेशा के लिए चलता है, program को freeze करते हुए; हमेशा double check करें कि loop के अंदर हर code path condition को false होने की तरफ move करता है।

उदाहरण: Infinite Loop Prevention

c
// 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;
}
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 लगाना, 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.