SlideShare a Scribd company logo
IInnttrroodduuccttiioonn ttoo PPrrooggrraammmmiinngg 
LLeeccttuurree 77
wwhhiillee lloooopp 
wwhhiillee ((ccoonnddiittiioonn)) 
{{ 
ssttaatteemmeennttss;; 
:: 
}} 
ssttaatteemmeennttss;;
WWhhiillee lloooopp eexxeeccuutteess zzeerroo 
oorr mmoorree ttiimmeess.. WWhhaatt iiff wwee 
wwaanntt tthhee lloooopp ttoo eexxeeccuuttee 
aatt lleeaasstt oonnee ttiimmee??
ddoo--wwhhiillee
Do while lloooopp eexxeeccuuttee oonn 
oorr mmoorree ttiimmeess
Syntax ooff ddoo--wwhhiillee 
lloooopp 
do do{ 
{ 
ssttaatteemmeennttss ;; 
}} 
wwhhiillee (( ccoonnddiittiioonn )) ;;
Example-GGuueessssiinngg ggaammee 
cchhaarr cc ;; 
iinntt ttrryyNNuumm == 11 ;; 
ddoo 
{{ 
ccoouutt <<<< ""PPlleeaassee eenntteerr yyoouurr gguueessss bbyy pprreessssiinngg aa cchhaarraacctteerr kkeeyy ffrroomm aa ttoo zz ““ ;; 
cciinn >>>> cc ;; 
iiff (( cc ==== ''zz‘‘ )) 
{{ 
ccoouutt <<<< ""CCoonnggrraattuullaattiioonnss!! yyoouu gguueesssseedd tthhee rriigghhtt aannsswweerr““ ;; 
ttrryyNNuumm == 6 ;; 
}} 
eellssee 
ttrryyNNuumm == ttrryyNNuumm ++ 11 ;; 
}} wwhhiillee (( ttrryyNNuumm <<== 55 )) ;;
Flow cchhaarrtt ffoorr ddoo--wwhhiillee 
lloooopp 
Do-while 
Process 
condition 
Exit 
true 
false
RReellaattiioonnaall 
OOppeerraattoorrss 
cchhaarr cc ;; 
iinntt ttrryyNNuumm ,, mmaaxxTTrriieess ;; 
ttrryyNNuumm == 11 ;; 
mmaaxxTTrriieess == 55 ;; 
ccoouutt <<<< ""GGuueessss tthhee aallpphhaabbeett bbeettwweeeenn aa ttoo zz ““ ;; 
cciinn >>>> cc ;; 
wwhhiillee (( (( ttrryyNNuumm <<== mmaaxxTTrriieess )) &&&& (( cc!! == ‘‘zz‘‘ )) )) 
{{ 
ccoouutt <<<< ""GGuueessss tthhee aallpphhaabbeett bbeettwweeeenn aa ttoo zz ““ ;; 
cciinn >>>> cc ;; 
ttrryyNNuumm == ttrryyNNuumm ++ 11 ;; 
}}
ffoorr LLoooopp
FFoorr lloooopp 
ffoorr (( iinniittiiaalliizzaattiioonn ccoonnddiittiioonn ;; tteerrmmiinnaattiioonn ccoonnddiittiioonn ;; iinnccrreemmeenntt ccoonnddiittiioonn {{ 
ssttaatteemmeenntt (( ss )) ;; 
}}
EExxaammppllee 
iinntt ccoouunntteerr ;; 
ffoorr(( ccoouunntteerr == 00 ;; ccoouunntteerr << 1100 ;; ccoouunntteerr == ccoouunntteerr ++ 11 )) 
ccoouutt <<<< ccoouunntteerr;; 
OOuuttppuutt 
00112233445566778899
TTaabbllee ffoorr 22 
22 xx 11 == 22 
22 xx 22 == 44 
22 xx 3 == 66 
:: 
:: 
22 xx 1100 == 2200
Example - Calculate TTaabbllee ffoorr 
22 
##iinncclluuddee <<iioossttrreeaamm..hh>> 
mmaaiinn (( )) 
{{ 
iinntt ccoouunntteerr ;; 
ffoorr (( ccoouunntteerr == 11 ;; ccoouunntteerr <<== 1100 ;; ccoouunntteerr == ccoouunntteerr ++ 11 )) 
{{ 
ccoouutt <<<< ""22 xx "" <<<< ccoouunntteerr <<<< "" == "" <<<< 22** ccoouunntteerr <<<< ""nn““ ;; 
}} 
}}
OOuuttppuutt 
22 xx11 == 22 
22 xx 22 == 44 
22 xx 33 == 66 
:: 
:: 
22 xx 1100 == 2200
Flow chart for the ‘‘TTaabbllee’’ eexxaammppllee 
Start 
counter=1 
While 
counter <=10? 
yes 
Print 2*counter 
Stop 
No Exit 
Counter = 
counter + 1
Example: CCaallccuullaattee TTaabbllee-- 
EEnnhhaanncceedd 
#iinncclluuddee <<iioossttrreeaamm..hh>> 
mmaaiinn (( )) 
{{ 
iinntt nnuummbbeerr ;; 
iinntt mmaaxxMMuullttiipplliieerr ;; 
iinntt ccoouunntteerr ;; 
mmaaxxMMuullttiipplliieerr == 1100 ;; 
ccoouutt <<<< "" PPlleeaassee eenntteerr tthhee nnuummbbeerr ffoorr wwhhiicchh yyoouu wwiisshh ttoo ccoonnssttrruucctt tthhee ttaabbllee ““ ;; 
cciinn >>>> nnuummbbeerr ;; 
ffoorr (( ccoouunntteerr == 11 ;; ccoouunntteerr <<== mmaaxxMMuullttiipplliieerr ;; ccoouunntteerr == ccoouunntteerr ++ 11 )) 
{{ 
ccoouutt <<<< nnuummbbeerr <<<<"" xx "" <<<< ccoouunntteerr<<<< "" == "" <<<< nnuummbbeerr ** ccoouunntteerr <<<< ""nn““ ;; 
}} 
}}
 AAllwwaayyss tthhiinnkk rree--uussee 
 DDoonn’’tt uussee eexxpplliicciitt ccoonnssttaannttss
IInnccrreemmeenntt ooppeerraattoorr 
++++ 
 ccoouunntteerr ++++ ;; 
ssaammee aass 
 ccoouunntteerr == ccoouunntteerr ++ 11;;
DDeeccrreemmeenntt ooppeerraattoorr 
---- 
 ccoouunntteerr ---- ;; 
ssaammee aass 
 ccoouunntteerr == ccoouunntteerr -- 11
++== 
 ccoouunntteerr ++== 33 ;; 
ssaammee aass 
 ccoouunntteerr == ccoouunntteerr ++ 33 ;;
--== 
 ccoouunntteerr --== 55 ;; 
ssaammee aass 
 ccoouunntteerr == ccoouunntteerr –– 55 ;;
**== 
xx**==22 
xx == xx ** 22
//== 
xx //== 22 
xx == xx // 22
Compound AAssssiiggnnmmeenntt 
OOppeerraattoorrss 
ooppeerraattoorr==
%%== 
 xx %%== 22 ;; 
ssaammee aass 
 xx == xx %% 22 ;;
CCoommmmeennttss 
 WWrriittee ccoommmmeenntt aatt tthhee ttoopp 
pprrooggrraamm ttoo sshhooww wwhhaatt iitt ddooeess 
 WWrriittee ccoommmmeennttss tthhaatt mmeeaann ssoommee 
tthhiinngg
IInn ttooddaayy’’ss lleeccttuurree 
 DDoo -- wwhhiillee 
– EExxeeccuutteess tthhee ccooddee aatt lleeaasstt oonneess 
 FFoorr lloooopp 
– EExxeeccuutteess aatt lleeaasstt zzeerroo ttiimmeess 
 SShhoorrtt hhaanndd ooppeerraattoorrss 
– IInnccrreemmeennttiinngg 
– DDeeccrreemmeennttiinngg 
 CCoommppoouunndd aassssiiggnnmmeenntt ooppeerraattoorr

More Related Content

PPT
CS201- Introduction to Programming- Lecture 33
PPT
CS201- Introduction to Programming- Lecture 40
PPT
CS201- Introduction to Programming- Lecture 15
PPT
CS201- Introduction to Programming- Lecture 12
PPT
CS201- Introduction to Programming- Lecture 26
PPT
CS201- Introduction to Programming- Lecture 37
PPT
CS201- Introduction to Programming- Lecture 10
PPT
CS201- Introduction to Programming- Lecture 13
CS201- Introduction to Programming- Lecture 33
CS201- Introduction to Programming- Lecture 40
CS201- Introduction to Programming- Lecture 15
CS201- Introduction to Programming- Lecture 12
CS201- Introduction to Programming- Lecture 26
CS201- Introduction to Programming- Lecture 37
CS201- Introduction to Programming- Lecture 10
CS201- Introduction to Programming- Lecture 13

What's hot (20)

PPT
CS201- Introduction to Programming- Lecture 06
PPT
CS201- Introduction to Programming- Lecture 28
PPT
CS201- Introduction to Programming- Lecture 20
PPT
CS201- Introduction to Programming- Lecture 03
PPT
CS201- Introduction to Programming- Lecture 30
PPT
CS201- Introduction to Programming- Lecture 34
PPT
CS201- Introduction to Programming- Lecture 16
PPT
CS201- Introduction to Programming- Lecture 08
PPT
CS201- Introduction to Programming- Lecture 36
PPT
CS201- Introduction to Programming- Lecture 41
PPT
CS201- Introduction to Programming- Lecture 09
PPT
CS201- Introduction to Programming- Lecture 35
PPT
Grade 12 U0-L4-GraphicalMethods
PPT
Edit data base menggunakan web
PPT
CS201- Introduction to Programming- Lecture 38
PPT
Grade 12 U0-L3-ErrorEstimation
PPT
CS201- Introduction to Programming- Lecture 18
PPT
CS201- Introduction to Programming- Lecture 29
PPT
CS201- Introduction to Programming- Lecture 25
PPT
CS201- Introduction to Programming- Lecture 14
CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 16
CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 09
CS201- Introduction to Programming- Lecture 35
Grade 12 U0-L4-GraphicalMethods
Edit data base menggunakan web
CS201- Introduction to Programming- Lecture 38
Grade 12 U0-L3-ErrorEstimation
CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 14
Ad

Viewers also liked (20)

PPSX
Kkm advertiser hydrabad
PPTX
Top dominating models in TVCs in the year 2015
PPTX
PTI and PAT Sep 3 rally Coverage analysis
PPTX
Top most innovative billboards around the world
PPTX
Afghan media analysis July 2016
PPTX
Afghan media industry analysis January 2017
PPTX
Studi Confortiani 02: Realisasi intuisi awal
PPS
ENG101- English Comprehension- Lecture 23
PPS
ENG101- English Comprehension- Lecture 35
PPT
CS201- Introduction to Programming- Lecture 31
PPS
ENG101- English Comprehension- Lecture 30
PPT
MGT101 - Financial Accounting- Lecture 27
PPS
CS101- Introduction to Computing- Lecture 43
PPT
CS201- Introduction to Programming- Lecture 32
PPT
CS201- Introduction to Programming- Lecture 27
PPS
ENG101- English Comprehension- Lecture 42
PPT
MGT101 - Financial Accounting- Lecture 28
PPT
CS201- Introduction to Programming- Lecture 22
DOC
MTH101 - Calculus and Analytical Geometry- Lecture 44
PPT
CS201- Introduction to Programming- Lecture 43
Kkm advertiser hydrabad
Top dominating models in TVCs in the year 2015
PTI and PAT Sep 3 rally Coverage analysis
Top most innovative billboards around the world
Afghan media analysis July 2016
Afghan media industry analysis January 2017
Studi Confortiani 02: Realisasi intuisi awal
ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 35
CS201- Introduction to Programming- Lecture 31
ENG101- English Comprehension- Lecture 30
MGT101 - Financial Accounting- Lecture 27
CS101- Introduction to Computing- Lecture 43
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 27
ENG101- English Comprehension- Lecture 42
MGT101 - Financial Accounting- Lecture 28
CS201- Introduction to Programming- Lecture 22
MTH101 - Calculus and Analytical Geometry- Lecture 44
CS201- Introduction to Programming- Lecture 43
Ad

Similar to CS201- Introduction to Programming- Lecture 07 (12)

PPT
CS201- Introduction to Programming- Lecture 05
PPT
CS201- Introduction to Programming- Lecture 45
PPT
CS201- Introduction to Programming- Lecture 11
PPT
21 High-quality programming code construction part-ii
PPT
CS201- Introduction to Programming- Lecture 42
PPT
CS201- Introduction to Programming- Lecture 44
PPT
CS201- Introduction to Programming- Lecture 24
PPT
Securing PHP Applications
PPT
19 Algorithms and complexity
PPT
Capital budgetin
PPT
CS201- Introduction to Programming- Lecture 04
PPT
Microsoft excel 2
CS201- Introduction to Programming- Lecture 05
CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 11
21 High-quality programming code construction part-ii
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 24
Securing PHP Applications
19 Algorithms and complexity
Capital budgetin
CS201- Introduction to Programming- Lecture 04
Microsoft excel 2

CS201- Introduction to Programming- Lecture 07

  • 2. wwhhiillee lloooopp wwhhiillee ((ccoonnddiittiioonn)) {{ ssttaatteemmeennttss;; :: }} ssttaatteemmeennttss;;
  • 3. WWhhiillee lloooopp eexxeeccuutteess zzeerroo oorr mmoorree ttiimmeess.. WWhhaatt iiff wwee wwaanntt tthhee lloooopp ttoo eexxeeccuuttee aatt lleeaasstt oonnee ttiimmee??
  • 5. Do while lloooopp eexxeeccuuttee oonn oorr mmoorree ttiimmeess
  • 6. Syntax ooff ddoo--wwhhiillee lloooopp do do{ { ssttaatteemmeennttss ;; }} wwhhiillee (( ccoonnddiittiioonn )) ;;
  • 7. Example-GGuueessssiinngg ggaammee cchhaarr cc ;; iinntt ttrryyNNuumm == 11 ;; ddoo {{ ccoouutt <<<< ""PPlleeaassee eenntteerr yyoouurr gguueessss bbyy pprreessssiinngg aa cchhaarraacctteerr kkeeyy ffrroomm aa ttoo zz ““ ;; cciinn >>>> cc ;; iiff (( cc ==== ''zz‘‘ )) {{ ccoouutt <<<< ""CCoonnggrraattuullaattiioonnss!! yyoouu gguueesssseedd tthhee rriigghhtt aannsswweerr““ ;; ttrryyNNuumm == 6 ;; }} eellssee ttrryyNNuumm == ttrryyNNuumm ++ 11 ;; }} wwhhiillee (( ttrryyNNuumm <<== 55 )) ;;
  • 8. Flow cchhaarrtt ffoorr ddoo--wwhhiillee lloooopp Do-while Process condition Exit true false
  • 9. RReellaattiioonnaall OOppeerraattoorrss cchhaarr cc ;; iinntt ttrryyNNuumm ,, mmaaxxTTrriieess ;; ttrryyNNuumm == 11 ;; mmaaxxTTrriieess == 55 ;; ccoouutt <<<< ""GGuueessss tthhee aallpphhaabbeett bbeettwweeeenn aa ttoo zz ““ ;; cciinn >>>> cc ;; wwhhiillee (( (( ttrryyNNuumm <<== mmaaxxTTrriieess )) &&&& (( cc!! == ‘‘zz‘‘ )) )) {{ ccoouutt <<<< ""GGuueessss tthhee aallpphhaabbeett bbeettwweeeenn aa ttoo zz ““ ;; cciinn >>>> cc ;; ttrryyNNuumm == ttrryyNNuumm ++ 11 ;; }}
  • 11. FFoorr lloooopp ffoorr (( iinniittiiaalliizzaattiioonn ccoonnddiittiioonn ;; tteerrmmiinnaattiioonn ccoonnddiittiioonn ;; iinnccrreemmeenntt ccoonnddiittiioonn {{ ssttaatteemmeenntt (( ss )) ;; }}
  • 12. EExxaammppllee iinntt ccoouunntteerr ;; ffoorr(( ccoouunntteerr == 00 ;; ccoouunntteerr << 1100 ;; ccoouunntteerr == ccoouunntteerr ++ 11 )) ccoouutt <<<< ccoouunntteerr;; OOuuttppuutt 00112233445566778899
  • 13. TTaabbllee ffoorr 22 22 xx 11 == 22 22 xx 22 == 44 22 xx 3 == 66 :: :: 22 xx 1100 == 2200
  • 14. Example - Calculate TTaabbllee ffoorr 22 ##iinncclluuddee <<iioossttrreeaamm..hh>> mmaaiinn (( )) {{ iinntt ccoouunntteerr ;; ffoorr (( ccoouunntteerr == 11 ;; ccoouunntteerr <<== 1100 ;; ccoouunntteerr == ccoouunntteerr ++ 11 )) {{ ccoouutt <<<< ""22 xx "" <<<< ccoouunntteerr <<<< "" == "" <<<< 22** ccoouunntteerr <<<< ""nn““ ;; }} }}
  • 15. OOuuttppuutt 22 xx11 == 22 22 xx 22 == 44 22 xx 33 == 66 :: :: 22 xx 1100 == 2200
  • 16. Flow chart for the ‘‘TTaabbllee’’ eexxaammppllee Start counter=1 While counter <=10? yes Print 2*counter Stop No Exit Counter = counter + 1
  • 17. Example: CCaallccuullaattee TTaabbllee-- EEnnhhaanncceedd #iinncclluuddee <<iioossttrreeaamm..hh>> mmaaiinn (( )) {{ iinntt nnuummbbeerr ;; iinntt mmaaxxMMuullttiipplliieerr ;; iinntt ccoouunntteerr ;; mmaaxxMMuullttiipplliieerr == 1100 ;; ccoouutt <<<< "" PPlleeaassee eenntteerr tthhee nnuummbbeerr ffoorr wwhhiicchh yyoouu wwiisshh ttoo ccoonnssttrruucctt tthhee ttaabbllee ““ ;; cciinn >>>> nnuummbbeerr ;; ffoorr (( ccoouunntteerr == 11 ;; ccoouunntteerr <<== mmaaxxMMuullttiipplliieerr ;; ccoouunntteerr == ccoouunntteerr ++ 11 )) {{ ccoouutt <<<< nnuummbbeerr <<<<"" xx "" <<<< ccoouunntteerr<<<< "" == "" <<<< nnuummbbeerr ** ccoouunntteerr <<<< ""nn““ ;; }} }}
  • 18.  AAllwwaayyss tthhiinnkk rree--uussee  DDoonn’’tt uussee eexxpplliicciitt ccoonnssttaannttss
  • 19. IInnccrreemmeenntt ooppeerraattoorr ++++  ccoouunntteerr ++++ ;; ssaammee aass  ccoouunntteerr == ccoouunntteerr ++ 11;;
  • 20. DDeeccrreemmeenntt ooppeerraattoorr ----  ccoouunntteerr ---- ;; ssaammee aass  ccoouunntteerr == ccoouunntteerr -- 11
  • 21. ++==  ccoouunntteerr ++== 33 ;; ssaammee aass  ccoouunntteerr == ccoouunntteerr ++ 33 ;;
  • 22. --==  ccoouunntteerr --== 55 ;; ssaammee aass  ccoouunntteerr == ccoouunntteerr –– 55 ;;
  • 23. **== xx**==22 xx == xx ** 22
  • 24. //== xx //== 22 xx == xx // 22
  • 26. %%==  xx %%== 22 ;; ssaammee aass  xx == xx %% 22 ;;
  • 27. CCoommmmeennttss  WWrriittee ccoommmmeenntt aatt tthhee ttoopp pprrooggrraamm ttoo sshhooww wwhhaatt iitt ddooeess  WWrriittee ccoommmmeennttss tthhaatt mmeeaann ssoommee tthhiinngg
  • 28. IInn ttooddaayy’’ss lleeccttuurree  DDoo -- wwhhiillee – EExxeeccuutteess tthhee ccooddee aatt lleeaasstt oonneess  FFoorr lloooopp – EExxeeccuutteess aatt lleeaasstt zzeerroo ttiimmeess  SShhoorrtt hhaanndd ooppeerraattoorrss – IInnccrreemmeennttiinngg – DDeeccrreemmeennttiinngg  CCoommppoouunndd aassssiiggnnmmeenntt ooppeerraattoorr