JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

C++ : For Loop ( Advance Concept ) in hindi , learn about for loop in c++ language in advanced level

learn about for loop in c++ language in advanced level , C++ : For Loop ( Advance Concept ) in hindi  :-
इससे पहले के article मे , for loop के basic structure को discuss किया है जिसमे for loop मे तीन भाग होते है |
1. initial : इसमें for loop के control variable को initial किया जाता है |
2. Condition : इसमें for loop के control करने के condition को check किया जाता है | जब ये condition true होती है तब तक loop की body execute होगी |
3. Increment / Decrements : इसमें for loop के लिए control variable को  Increment / Decrements किया जाता है |
4.For looping body :इसमें for loop मे perform करने के लिए task को perform करने के लिए किया जाता है |
इस article मे , for loop मे advance concept को discuss किया जाता है |
Changing step size
अभी तक हमने देखा है की for loop मे increment / decreements के लिए step size ‘1’ होती है लेकिन ये जरुरी नहीं है की step size ‘1’ हो | अतः प्रोग्राम के requirement के अनुसार step size को change किया जा सकता है |इसके लिए निन्म statement को consider किया जाता है |
int i = i+2;
यहा पर
i : control variable है |
2 : step size है |
उदाहरण के लिए :
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
cout<<“Even number Under 20 :”<,endl;
for(i=0;i<=20;i=i+2)
{
cout<<i<,endl;
}
getch();
}
इस उदाहरण मे even number को display किया जाता है | दो even number मे ‘2’ का अंतर होता है इसलिए इसमें for loop मे step size ‘2’ के साथ even number को display किया जाता है |
इसका आउटपुट होगा :
Even number Under 20 :
2 4 6 8 10 12 14 16 18 20
String inside for loop
for loop का use string के प्रत्येक character को access और display करने की डायरेक्ट सुविधा देता है | उदाहरण के लिए :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
string name ;
cout<<“Enter String :”<,endl;
cin>>name;
cout<<“Reverse Order”;
for(i=strlen(name);i>0;i–)
{
cout<<name[i];
}
getch();
}
इस उदाहरण मे string को reverse order मे display किया गया है | इसके लिए इस उदाहरन मे string.h header file को include किया गया है | जिससे string के pre define function string और strlen() को use किया जा सकता है |
और for loop मे
initial : control variable को string name के length से initial किया गया है |
condition : ये loop जब तक चलता है तब तक i की value ‘0’ से बड़ी होती है |
increment : इसमें i मे ‘1’ का increment किया जाता है |
इसका आउटपुट होगा :
Enter String : Parth
Reverse Order
htrap
Pre Increment / Decreament
Increment / Decreament को loop मे use करने से पहले भी Increment / Decreament किया जा सकता है | इसे pre Increment / Decreament कहते है | अभी तक सभी loop मे Increment / Decreament को loop के एक बार use करने के बाद Increment / Decreament किया गया है | लेकिन Increment / Decreament को loop के execute होने से पहले भी Increment / Decreament भी possible है |
इसका उदाहरन निन्म है |
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int a=10;
for(i=0;i<5;++i)
{
a=a+10;
}
cout<<“Value of a :”<<a<<endl;
getch();
}
इस उदाहरण मे Pre-Increment को use किया गया है | जिससे ये loop केवल 4 बार ही execute होगा |
क्योकि जब loop पहली बार execute होगा तब i की value ‘1’ होगी | और ये loop तब तक चलता रहेगा जब तक i की value ‘5’ से कम होती है | अतः a की value 4 नहीं हो जाती है |
इसका आउटपुट होगा ;
Value of a : 50
Expression as Increment / Decrements
किसी for loop के Increment / Decrements मे , control variable की value को किसी expression के आउटपुट से Increment / Decrements कर सकते है | इस expression का आउटपुट integer value होनी चाहिए |
इसके लिए उदाहरन होगा :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int a=10;
for(i=1;i<10;i=i+(2*i))
{
a=a+10;
}
cout<<“Value of a :”<<a<<endl;
getch();
}
इस उदाहरन मे ,i+(2*i) के आउटपुट से control variable ‘i’ की value को update किया जाता है |
जब loop पहले बार चलाया जाता है उसके बाद
i = 3 होगी जो की 10 से कम है | इसलिए addition operation perform होता है | इसके बाद
i=7 होगी जो की 10 से कम है | इसलिए addition operation perform होता है | इसके बाद
i=15 होगी जो की 10 से ज्यादा होती है अतः loop terminate हो जाता है |
इसका आउटपुट होगा
Value of a : 30
Expression as condition
किसी for loop के condition statement मे , control variable की value को किसी expression के आउटपुट से check किया जाता है | इस expression का आउटपुट integer value होनी चाहिए |
इसके लिए उदाहरन होगा :
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int a=10;
string name ;
cout<<“Enter Name :”<<endl;
cin>>name;
for(i=1;i<strlen(name);i++)
{
cout<<name[i];
}
cout<<“Value of a :”<<a<<endl;
getch();
}
इस उदाहरन मे string को display किया जाता है | इसके लिए string को यूजर से input किया जाता है |
और loop चलाया जाता है |
condition मे control variable को string के length से check किया जाता है | जो की strlen() function से calculate किया जाता है |
इसका उदाहरन होगा :
Enter Name : Parth
Parth
Value of a : 10
इस article मे , for loop के general format मे हो सकने वाले changes को discuss किया है | अब आगे के article मे while और do-while loop को discuss किया जाता है |
Sbistudy

Recent Posts

मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi

malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…

1 month ago

कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए

राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…

1 month ago

हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained

hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…

1 month ago

तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second

Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…

1 month ago

चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi

chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…

2 months ago

भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi

first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…

2 months ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now