लूप (Loop) एक प्रोग्रामिंग संरचना है जिसका उपयोग किसी कोड ब्लॉक को बार-बार दोहराने (Repeat) के लिए किया जाता है। जब तक कोई शर्त (Condition) सही रहती है, तब तक लूप चलता रहता है।
for लूप का उपयोग तब किया जाता है जब हमें पता होता है कि लूप कितनी बार चलेगा।
for(initialization; condition; increment/decrement) { // statements }
#include <iostream> using namespace std; int main() { for(int i = 1; i <= 5; i++) { cout << "Hello World" << endl; } return 0; }
while लूप तब उपयोग होता है जब हमें पहले से नहीं पता कि लूप कितनी बार चलेगा।
while(condition) { // statements }
#include <iostream> using namespace std; int main() { int i = 1; while(i <= 5) { cout << i << endl; i++; } return 0; }
do-while लूप में कोड पहले कम से कम एक बार चलता है, फिर शर्त जांची जाती है।
do { // statements } while(condition);
#include <iostream> using namespace std; int main() { int i = 1; do { cout << i << endl; i++; } while(i <= 5); return 0; }
C++ में लूप का प्रयोग कोड को दोहराने के लिए किया जाता है, जिससे प्रोग्राम छोटा, कुशल और पढ़ने में आसान बनता है। सही लूप का चयन कोड की गुणवत्ता को बेहतर बनाता है।