If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
पिछले topic में हमें while loop, do while loop के बारे में पढ़ा इस topic में हम for loop के बार में पढ़ेंगे।
Actually किसी code of block को repeatedly run करने का सबसे easy way looping है , while loop, do while loop में अगर हम ध्यान से देखेंगे तो 3 steps को follow किया गया है -
means , while loop या do while loop use करने के लिए हम इन 3 steps को follow करके ही program implement करते थे। for loop में इन तीनो statements को अलग अलग लिखने की वजाय हम एक साथ लिखते है जिससे Looping और easy और understandable हो जाती है।
for(initialization ; condition ; increment / decrement) { //code of block }
तो Syntax में आप देख सकते हैं कि for loop में , हम तीन Expression देते हैं , जो कुछ इस तरह से run होते हैं।
first expression : for loop में initial expression हैं जहाँ हम किसी variable को define करते हैं ।
second expression conditional expression होता है और हर iteration में second expression execute होता , condition true होने पर ही loop में entry होती है otherwise हम loop से बाहर हो जाते हैं।
सबसे last में third expression रन होता है , जहां पर हम किसी variable को increment / decrement करते हैं। यह भी हर iteration के last में ही execute होता है। , हालाँकि यह Optional होता है , यह variable हम loop के अंदर भी increment / decrement कर सकते हैं।
#include <iostream>
using namespace std;
int main() {
int num = 1;
for(int num=1; num <=10; num++) {
cout << num << endl;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10
#include <iostream>
using namespace std;
int main() {
int number = 9;
for(int x=1; x<=10;x++)
{
cout << number << " x " << x << " = " << x*number << endl;
}
return 0;
}
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 9 x 10 = 90
ठीक Nested if else या nested while loop की तरह ही हम nested for loop (For Loop Inside Another For Loop) भी use कर सकते हैं।
#include <iostream>
using namespace std;
int main() {
int num = 1;
for(int x=1;x<=5; x++) {
for(int y=1; y<=x; y++) {
cout << y << " ";
}
cout << endl;
}
return 0;
}
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook