हिंदी माध्यम नोट्स
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;
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 किया जाता है |
Recent Posts
सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है
सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…
16 hours ago
मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the
marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…
16 hours ago
राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi
sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…
2 days ago
गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi
gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…
2 days ago
Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन
वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…
3 months ago
polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten
get all types and chapters polity notes pdf in hindi for upsc , SSC ,…
3 months ago