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

C++ का for Loop

एक for loop steps को एक set number of times दोहराता है, गिनते हुए दस jumping jacks करने जैसा। start, stop, और counting सब एक line में बैठते हैं।
Syntax
cpp
for (initialization; condition; update) {
  // body
}

For Loops का Introduction

एक for loop initialization, एक condition, और एक update step को एक line में pack करता है — for (int i = 0; i < 10; i++) — इसे natural choice बनाते हुए जब भी आपको पहले से exactly पता हो कि आपको कुछ कितनी बार दोहराना है।

एक while loop के मुकाबले, तीनों parts को साथ रखना loop के start, stop, और progress conditions को एक नज़र में देखना आसान बनाता है।

उदाहरण: Introduction to For Loops

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 10; i++) {
		// Print to the console with cout
		std::cout << i << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Loop Steps

Update step सिर्फ simple +1 increments तक सीमित नहीं है — आप हर pass counter में कोई भी amount add कर सकते हैं, multiply कर सकते हैं, या divide कर सकते हैं, जो loop को linearly की बजाय geometrically step करने में बदल देता है।

यह exactly वह तरीका है जिससे binary search जैसे algorithms, जो बार-बार एक value को आधा या दोगुना करते हैं, अपना loop structure करते हैं।

उदाहरण: Loop Steps

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 0; i < 20; i += 5) {
		// Print to the console with cout
		std::cout << i << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested For Loops

एक for loop को दूसरे के अंदर रखना inner loop को outer loop की हर single iteration के लिए पूरी तरह चलने देता है, जो exactly एक 2D grid process करने के पीछे का pattern है — outer loop rows में चलता है जबकि inner loop हर row के अंदर columns में चलता है।

Iterations की total संख्या दोनों loops के counts का product है।

उदाहरण: Nested For Loops

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Loop: repeat while the condition holds
	for (int i = 1; i <= 2; i++) {
		// Loop: repeat while the condition holds
		for (int j = 1; j <= 2; j++) {
			// Print to the console with cout
			std::cout << i << "," << j << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arrays पर Loop करना

एक for loop के counter को एक array index की तरह इस्तेमाल करना, जैसे for (int i = 0; i < size; i++) cout << arr[i];, C++ में sequence में हर element visit करने के सबसे common patterns में से एक है।

यह ज़रूरत पड़ने पर आपको index खुद देता है, कुछ alternatives के उलट जो सिर्फ value देते हैं।

उदाहरण: Looping Over Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {10, 20, 30};
	// Declare size and set it to 3
	int size = 3;
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		// Print to the console with cout
		std::cout << arr[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Range-based For Loop

एक range-based for loop, for (int x : arr) की तरह लिखा गया, आपके किसी index manage किए बिना सीधे हर element की value पर iterate करता है, जो off-by-one bugs की पूरी category हटा देता है।

जब भी आपको actually index की ज़रूरत न हो, सिर्फ values की, यह modern C++ में preferred style है।

उदाहरण: Range-based For Loop

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {10, 20, 30};
	for (int x : arr) {
		// Print to the console with cout
		std::cout << x << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// 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. एक array length के साथ <= इस्तेमाल करना, जैसे i <= n, जो अंत से एक element आगे पढ़ता है।
  2. for (...); के बाद एक semicolon add करना, जो loop body को empty बना देता है।
  3. for (int i = 0, i < 5, i++) में semicolons की बजाय commas या colons इस्तेमाल करना।

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.