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 में आपने C++ में array के बारे में पढ़ा और समझा कि कैसे array को define करते हैं single elements को access करते हैं। इस topic में आप सीखेंगे कि for loop और while loop की help से कैसे array को traverse कर सकते हैं।
C++ में Array को आप for loop और while loop दोनों से traverse कर सकते हो।
#include <iostream>
using namespace std;
int main()
{
string users[5] = {"Rahul", "Ravi", "Raju", "Ram", "Shyam"};
int index=0;
while( index < 5) {
cout << "Index " << index << " : " << users[index] << endl;
index++;
}
return 0;
}
Index 0 : Rahul Index 1 : Ravi Index 2 : Raju Index 3 : Ram Index 4 : Shyam
Note - Example में endl का use line break करने के लिए किया गया है।
ठीक इसी तरह आप for loop का use करके array traversing कर सकते हैं।
#include <iostream>
using namespace std;
int main()
{
string users[5] = {"Rahul", "Ravi", "Raju", "Ram", "Shyam"};
for(int index=0; index < 5; index++) {
cout << "Index " << index << " : " << users[index] << endl;
}
return 0;
}
Index 0 : Rahul Index 1 : Ravi Index 2 : Raju Index 3 : Ram Index 4 : Shyam
हालाँकि आप for loop की help से directly array element को भी access कर सकते हैं।
#include <iostream>
using namespace std;
int main()
{
string users[5] = {"Rahul", "Ravi", "Raju", "Ram", "Shyam"};
for(string name : users) {
cout << name << endl;
}
return 0;
}
Rahul Ravi Raju Ram Shyam
I Hope, अब आपके अच्छे से समझ आ गया होगा कि C++ में किस तरह से Array Traversing करते हैं।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook