Tuesday, April 5, 2011

::::|| VU ||:::: LearnC_lecture07

**************Lesson7********************
Date:5-April-2011
Author:asad ali khan sherwani
Message:Take your soul out of the way from christanity.
*****************************************

****************Today's Ajenda***************
1.Loops
2.The while Loop
3.More operators
4.The fore Loops
5.Nesting of Loops
6.The Odd loop
7.The break statement
8.The continue statement
9.The do-while Loop
10.Exercise
**************************
A concept of Loop
agr hum ko koi khaee k pehly 100 numbers ka sum nikaley to 
app bta sakty hain k hum iss ko kis tarha karey gay aor kitney der lagy gi.
jaha tak hum ney pra hai agr dekha jaey to hum ney simple instructions,aor
mazeed if-else decision baes instructions ko pra hai. agr in techniques ko
use kar k problem ko solve kiya jay to kafi dear lag jaey gi.bal k na sirf
dear lgy gi programer code likhety bi thak jay ga aor program ka size bi 
kafi bra ho jaey ga .program ifself bi kafi sust ho jaey ga.
is ki aik aor example yun samjey k pehly aik dfa main ney aik 
example di thi k hmary pass 10 students hian jin ki average age nikalni hai.
ab agr app farz kary k hmary pass 100 students hain jin ki average nikalni hai to
kiya hum wo hi simple way use kary gay???

To jnab aesy koi bat nahi hai C language hamesha ki tarha aksr mushkalat sey
nikalney k ley humey different types ka feture provide karty hai.
aesy problems jha repetation structure use ho raha hoo. yani
aik hi kam ko bar bar kiya jaey aor sirf parameters change ho rahy hoo to 
uder hmary pass aik loop structure ki sahoolt majood hai.
ham iss tarha k problems ko loops structure ki madad sey asaani sey
solve kar sakty hain.To aye hum in loops k bary mai mazeed kuch party hain

Loops:
C language hum ko 3 different qisam k loops structure provide karty hai.

for 
while
do-while

while loop:
yeh loop structure programmer ko aik khass statement k true honey tak aik hi kam ko 
bar bar karney ki ijaazt dety hai. yani while loop mai mai hum ziyada tar aik statement to 
musalsal chk karty rahty hain. k jab tak true hai to repetation structure chalta rahta hai. 
standard format of while loop

for example
while(iteams on list not came on finished)
purchase next iteam and cross it from the list;

aik waqt aesy bi atta hai k while statement false ho jati hai aor program while k structure sey 
nikal kar nechy waly code mai aajata hai.
Note :agr while loop mai aik sey ziyada satement hon to phir {} k and andr code likha jata hai
for example
while(semester=2)
{
ENG201=included in course//yeh bi bakwas subject hai
ECO401=included in course//aor yeh bi.
}
iss example mai 2 cheezy goor talb hain. aik to hum ney aik sey ziyada statement k ley 
{}ka istmal kiya hai .agr na karty to app zra soch kar reply kary k kiya ho sakta tha??
aor dosra yeh k program mai aik galti hai kiya app bta sakty hai k kon si???
agr kissi ko pta hai to pls zaroor reply kary.

Tips and Traps:
*while statement k agy ; nahi ata 
*while ki body mai aik sey ziyada statement ko {} mai likhty hain. 
*while loop condition ko reglarly test kart hai aor true honey tak chalta hai 
*while loop ki condition false kab ho gi iss bat ka khiyal rakhna bohat hi zaroori hai 
 iss bat ko agy ja kar mazeed explain kar diya jaey ga.
*while ki condition statement single ya compound bi ho sakty hai. 
*while ki condition ko chk kary k kahi aesy na ho k wo statement kabi false hi na ho.
*while condition statement ka  variable float type ka bi ho sakta hai. 
*while condition statement k variable ko increment aor decrment bi kiya ja sakta hai.
example program

number=1;
while(number<=10){cout<<"3*"<<number<<"="<<3*number<<"\n";number=number+1;}
yeh program 3 ka table print karwany k ley hai. 

More operators:
while progrm mai hum ko aksr condition statement ko false karney k ley iss ko increment ya decrement 
karna parta hai app ney aksr C language mai iss tarha ki satement dekhi ho gi
number=number+1;
is ko increment statment kehty hain. is ka matlab simple alfazz mai yun samjley k number ki jo bi value hai 
us mai 21 add kar do bus.
aor 
number=number-1;
iss ka matlba k number ki jo bi value hai us mai sey aik minus kar do. 
iss ko decrement statement bolty hain. 
increment statement ko aik oar tareeqy sey bi likha ja sakta hai. 
number+=1;
yeh statement bi increment statement khalwati hai.+= ko compound operator bi kehty hain.
matlb jo aik sey ziyada task perform kary . 
yeh operator pehly value add karta hai aor phir assign kar wata hai. 
iss tarha -=,*=,/=,aor %= operator bi hai .
increment karney ka aik oar tareeqa yeh bi ho sakta hai
number++;
iss sey bi number ki jo bi vaue hai us mai 1 ka izafa hota hai aor issi tarha agr
hum decrement karna chahy to 
number--; 
yeh line number ki jo bi value hai us mai sey aik minus kar dety hai. 
iss statement ko post increment statement bi kehty hain. 
iss statement sey milti julti aik satement aor bi hai. 
++number;
--number;
yeh statements bi number ko add karney aor minus karney k ley use hoti hain. 
iss statement ko preincrement aor predecrement statement bi kehty hain. 
Note:
number++;
++number;
yeh values mai 1 add karty hain. 
+++ ya --- yeh koi bi operator nahi hai iss ley  1 sey ziyada add aor minus 
k ley iss ko use na kiya jaey C lanuage +++ ya --- ko nahi janti.

for example
while(i++<10)
{
cout<<i;
}

while(++i<10)
{
cout<<i;
}
phely code mai i ka 10 k sath comparisen hota hai aor phir i mai 1 increment hota hai.
pehly code mai i ki value 9 print ho gi to pehly condition test ho gi phir increment ho kar
value 10 print ho jaey gi phir increament ho ga jo k 11 ho jaye ga aor condition false ho jaey
gai.
dosry code mai pehly i mai 1 increment hota hai phir i ki value test hoti hai.
dosry code mai jab i ki value 10 ho gi to condition false ho jaey gi aor 10 screen per print 
nahi hoga. 


The for loop
standard syntax of for loop is

for(count=1;count<=10;count++)
{
statement1;
statement2;
.
.
.
.
end
}

For loop ko aksar wha use kiya jata hai jha per exact number of iteration ka pta hun.
for example agr 100 tak numbers ka sum lena hun ya 9 tak tmam numbers ka table print karna haoo.
iss tarha ka problems ko solve karney k ley for ko use kiya jata hai. 

Tips and Traps:
*condition variable ko for loop ki condition satatement mai hi likha jata hai
*increment statement unlike while loop ko bi for ki condition satateent k andr hi liha jata hai
*for ki condition statement k end per ; nahi ata.
*for loop mai pehly condition test hoti hai .phir body execute hoti hai aor bad mai increment hota hai.
*initialize statement ko agr loop  ka bahir bi kar diya jaye to error nahi hota .
*increment statement ko agr code k end per kar diya jay to koi farq nahi parta.
for example
i=i+1;
for(;i<=10;)
{
cout<<i;
++i;
}



Nesting of Loops:
jiss tarha previous lecture mai hum ney if else  structure ko nested kar k dekha tha.
bilkul same us tarha while aor for loops ko bi nested kiya ja sakta hai

int r,c,sum;
for(r=1;r<=3;r++)
{
for(c=1;c<=2;c++)
{
sum=r+c;
cout<<"r="<<r<<"c="<<c<<"sum="<<sum<<"\n";
}
}

ans will be 
r=1c=1sum=2
r=1c=2sum=3
r=2c=1sum=3
r=2c=2sum=4
r=3c=1sum=4
r=3c=2sum=5
explanation:
pehly inner loop aik dfa sari complete chalti hai us ka bad bahir loop mai aik 
increment hota hai. jab increment hota hai to inner loog aik dfa pir sari chalti hai.
jiss tarha aik for loop k andr for loop aa sakti hai issi tarha aik while loop k andr aik
aor while loop bi aasakti hai.issi tarha while loop k andr for aor for loop k andr while loop bi aasakti hai.

The odd loop
real time programing mai baaz aesy bi hota hai k hum ko loops ki exact number of iteration ka 
sahee andaza nah hota hai . k loop ki limit kitny hogi aesy halt mai while loop ko hi use kiya jata hai
aor loop ko odd loop kehty hai. 
for example:
while(inut=='Y')
{
cout<"enter a number\t";cin>>number;
if(number%2==0)cout<<"number is divideable on 2\n";
cout<<"Do you want to run program again y=yes,n=No\t";cin>>input;
}
iss example mai hum ney control user ko diya howa hai k wo jitny der chahy iss progrm ko chlta rahy .

Break statement:
some times hum ko loop ki conditio false honey sey pehly hi program mai loop ko khatam karney sey 
bahir ana part hai. us k ley C ka keyword hai "break" is statement ko use karney sey 
program loop ki halt sey bahir asskta hai.
for example 
while( number<=10)
{
if(number%7==0)break;
}                                                                                                                                                                                                                    

*agr break inner loop mai use kiya jaey to andr wala loop pehly terminate ho ga jab k bahir wala
loop abi nahi ho ga.



The continue statement:
baz owqaat programing mai kuch aesy problems bi samney aajaty hain k hum ko loops mai code
ka kuch hisa wasey hi choor diya jata hai aor kuch hisa chalya jata hai.

for example:
int i,j;
for (i=1;i<=2;i++)
{
for (j=1;j<=2;j++)
{
if(i==j)
   continue;
cout<<i<<" "<<j<<"\n";
   
}
}

iss program mai jab i aor j ki value same hoti hai to program continue sey nechy waly code ko 
skip karta howa dobara uder sey hi wapis chala jata hai. aor loop k shuru sey chalna shuru
hojata hai.

The do while loop:
general formate of do while loop
 do 
{
this;
do this;
and this;
and this;

}while(this condition is true);

while aor do while statement mai aik major difference yeh hai k whiele loop mai 
pehly condition chk hoti hai aor phir code execute hota hai yani agr pehli hi bar mai conditin false 
ho jay to loop ki body execute karti hi nahi hai bal k wo trminate ka jata hai. 
iss k bilkul baraks do while loop mai pehly loop ki body execute hoti hai aor phir condition test hoti hai.
yani agr condition pehly hi dfa false bi hot code kam az kam aid dfa zaroor chal jata hai.

aye iss concept ko aik example sey clr karty hain. 

while(4<1)cout<<"you r lucky";

do 
{
cout<<"you ar lucky";
}while(4<1);

iss example mai while ka code execute nahi ho ga q k pehly hi bar jab condition chk ho gi to condition false ho 
gi lehaza control loop sey exit kar jay ga
aor do while loop aik dfa execute kary ga aor iss ka message bi print ho ja lekin jab yeh condition ko chk kary ga to.
to conditio q k false hai iss ley aik k mazeed dosry dfa nahi chaly ga. 
******************************************************************

Exercise:
1.write a program to print all the ascii values and their equivalent ncharacters using a whie loop.The ascii values vary from 0 to 255.
2.write a program to print all Armstrong numbers between 1 to 500 
  armstrong number is for example 
  153=(1*1*1)+(5*5*5)+(3*3*3)
  i.e the sum of cube of each digits of the number is equal to the number itself is armstrong number 
3.write a program to produce the following program dont use too many couts
  A B C D E F G F E D C B A 
  A B C D E F   F E D C B A 
  A B C D E       E D C B A 
  A B C D           D C B A 
  A B C               C B A 
  A B                   B A 
  A                       A
  A                       A
  A B                   B A 
  A B C               C B A 
  A B C D           D C B A 
  A B C D E       E D C B A 
  A B C D E F   F E D C B A 
  A B C D E F G F E D C B A 
4.write a program to add frist seven terms of the following series using for loop:
    1 2   3
   ___ +___+___+........
    1! 2!   3!




 




--

Take your soul out of the way from cheristanity 
Asad Ali

BSc (Semester-02) 

City.Lahore


--
For study materials, past papers and assignments,
Join VU School at www.VusCool.com
and www.VUGuys.com
CoooL Virtual University Students Google Group.
To post to this group, send email to coool_vu_students@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/coool_vu_students?hl=en

No comments:

Post a Comment