Nested loop
A structure completely inside the body of another loop is called nested loop.Nested loop me 2 tarha k loop hoty hai
outer loop
inner loop
jo pehla loop hota hai wo outer loop hota hai ur jo loop k ander loop hota hai inner loop hota hai
Outer loop ek bar execute ho ga per inner loop me jo condition lagi ho ge wo pora execute ho ga farz kary ap ne ineer loop k lay condition 4 lagi hai ur outer k lay 2 to pehly outer loop 1 bar execute ho k inner 4 bar execute ho ga jab false ho jay ga inne loop to outer loop per execute ho ga 1 bar per inner me aye ga to inner 4 bar per execute ho ga perouter loop check ho ge to false ho jai ge ur body se bahr jai ge ur programm run ho ga
I hope k ap ko samj aye ho ge nested loop kesy kam karty hai pehly do-while nested loop karty hai per InshAllah nested for loop karain gy ur InshAllah ap log seek loo gy
Ab 2 programm banty hai while k nested… okkkkkkk
Void main()
{
int a,b;
a=1;
while (a<=2)
{
b=1;
cout<<a<<endl;
while(b<=3)
{
cout<<”Ali Okz”<<endl;
b++
}
a++;
}
getch();
}
• Yasd raky me ne pehly pe kaha ta k programming statement by statement chalti hai to kesy diky
• Pehly ham ne 2 varible ly int type kjes ko ham ne a ur b nam dia
• Per a ko 1 se initialize kia
• Per while (outer loop) condition lagi k a less ho 2 se ya equal to per terminate ho
• To true hai q k a less hai 2 se
• Body me aye ga
• b ko 1 se initialize kia
• next statement chalie ge output pyt 1 aye ga q k a ke value abi 1 hai
• abi body k andr ek ur loop hai (inner loop)
• inner ke condition check ho ge I les hai 3 se ya equal to less hai q k I 1 hai abi condition true hai
• inner ke body me aye ga
• cout ke statement chali ge ali okz aye ga
• b me increment ho k 2 ho jay ga
• inner ke condition check ho ge 2 lees hai 3 se b ke value 2 ho gai hai q k incremnt ho gai hai
• true hai body me jay ga per ali okz aye ga
• per b me increment ho ge 3 ho jay ga check kary ga less hai nahe les nahe hai par eqaul hai condition true hai body me aye ga statement chali ge
• ali okz aye ga
• per increment ho k 4 ho jay ga ur check ho ge to false ho jay ga body se bahr ajy ga
• next staement hai outer loop ka abi inner se bahar agea par outer ke condition false nahe hai means k abi outer k andar he hai
• next satement a++ ho ge check ho ge true hai q k a 2 hai ur equl hai 2 k
• body me aye ga b ko 1 se initilize ho ga
• next staemeent chali ge 2 ajy ga output py line change ho ge
• inner ke body check ho ge b less hai 3 se ha q k b abi 1 hai
• body me aye ga cout ke staement chali ge ali okz aye ga
• next staement chali ge b me increment ho k 2 ho jay ga
• per condition check ho ge inner ke true hai q k 2 less hai 3 se body me aye ga
• ali okz aye ga next satement chali ge b me increment ho ge 3 ho jay ga
• condition check ho ge equal hai true hai body me aye ga inner ke
• ali okz aye ga b me increment ho ga
• condition check ho ge false ho jay ga q k 4 bara hai 3 se inner se bahar jay ga
• a me increment ho ge 3 ho jy ga
• ab outer ke condtion check ho ge false ho jay ga q k 3 bara hai 2 se to outer se bahar aye ga or result show ho ga
• jab b outer false ho jay to inner khud bakhod false ho jay ga
• jab ham outer ke body se bahr agy to per inner kam nahe kary ga
• output ye ho ge
1
ali okz
ali okz
ali okz
2
ali okz
ali okz
ali okz
Ab samj aye ho ge ap sab ko k nested loop kesy kam karty hai outer k andr ap ne inner k lay jo b condition lagi ho ge wo pora run ho k outer per chli ge jab tak outer false na ho tab tak inner chali ge
1 2 3 4 5
1 3 5 7 9
1 4 7 10 13
Ab es ka diky inshAllah es se bht achi tarha seek loo gy ur per es k bad ek do-while nested loop par lecture dainy gy per ek lecture nested for loop py per InshAllah Arrays start karain gy Agr zendagi rahe to
Void main()
{
int a,b,c;
a=1;
while(a<=3)
{
b=1;
c=1;
while(b<=5)
{
cout<<c<<” ”;
c=c+a;
b=b+1;
}
cout<<endl;
a=a+1;
}
getch();
}
• ok es me ham ne 3 varible liye hai a b c
• a ko 1 se initialize kia
• per outer loop ke condition check ho ge a lees hai 3 se ha less hai
• body me aye ga
• b ko 1 mil jay ga ur c ko b 1 mil jy ga
• inner loop ke condition check ho ge true hai q k b lees hai 5 se
• body me aye ga pehli statement chali ge cout wali 1 aye ga output py k c ke value abi 1 hai
• next py aye ga c=c+a to 2 ho jay ga q k c ke b 1 hai ur a ke b 1 hai
• next staement check ho ge b me 2 ho jay ga
• inner ke condition check ho ge true hai q k 2 less hai 5 se body me aye ga inner k
• pehli satement chalie ge 2 aye ga next staement chali ge c=c+a to c 2 hai ur a 1 hai 3 ho jay ga next staement chalie ge b=b+1 3 ho jay ga
• condition check ho ge 3 less hai 5 se ha true hai body me aye ga
• cout ke staement chali ge 3 aye ga q k c ke value 3 ho gai te
• next chly ga c=c+a means k 3+1 4 ho jay ga es tarha aye ga 5 tak to inner ke condtion false ho jai ge body se bahr aye ga
• next staement chali ge cout<<endle ke line change ho jay ga
• a me increment ho k 2 ho jay ga outer ke condition check ho ge 2 less hai 3 se ha outer ke body me aye ga
• b ko 1 mil jay ga c ko b 1 mil jay ga
• per inner loop py aye ga check ho ga b less hai 5 se ha q k b ke value 1 hai
• pehli satement chali ge 1 aye ga q k c ke value abi 1 hai
• next satement chali ge c=c+a means k 1+2 q k a ke value ab 3 hai
• b me increment ho k 2 ho jai gew
• condition check ho ge inner ke 2 less hai 5 se ha true hai
• body me aye ga cout ho ga 3 aye ga
• bus yar thak gea es k bad khud karooo I hope k samj aye ho ge kesy hoti hai nested loop
• ok aj k lay ye kafi hai bht thak gea ap sab khud programm bana k us ka run approch likooo InshhAllah sab ko samj aye ho ge
• dua me yad rakna
Mt Ali Okz
No comments:
Post a Comment