SlideShare a Scribd company logo
1 of 28
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

What's hot

CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06CS201- Introduction to Programming- Lecture 06
CS201- Introduction to Programming- Lecture 06Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28CS201- Introduction to Programming- Lecture 28
CS201- Introduction to Programming- Lecture 28Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20CS201- Introduction to Programming- Lecture 20
CS201- Introduction to Programming- Lecture 20Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 03CS201- Introduction to Programming- Lecture 03
CS201- Introduction to Programming- Lecture 03Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30CS201- Introduction to Programming- Lecture 30
CS201- Introduction to Programming- Lecture 30Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34CS201- Introduction to Programming- Lecture 34
CS201- Introduction to Programming- Lecture 34Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 16
CS201- Introduction to Programming- Lecture 16CS201- Introduction to Programming- Lecture 16
CS201- Introduction to Programming- Lecture 16Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 08CS201- Introduction to Programming- Lecture 08
CS201- Introduction to Programming- Lecture 08Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36CS201- Introduction to Programming- Lecture 36
CS201- Introduction to Programming- Lecture 36Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41CS201- Introduction to Programming- Lecture 41
CS201- Introduction to Programming- Lecture 41Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 09
CS201- Introduction to Programming- Lecture 09CS201- Introduction to Programming- Lecture 09
CS201- Introduction to Programming- Lecture 09Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35CS201- Introduction to Programming- Lecture 35
CS201- Introduction to Programming- Lecture 35Bilal Ahmed
 
Grade 12 U0-L4-GraphicalMethods
Grade 12 U0-L4-GraphicalMethodsGrade 12 U0-L4-GraphicalMethods
Grade 12 U0-L4-GraphicalMethodsgruszecki1
 
Edit data base menggunakan web
Edit data base menggunakan webEdit data base menggunakan web
Edit data base menggunakan webhaviedz miftah
 
CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38CS201- Introduction to Programming- Lecture 38
CS201- Introduction to Programming- Lecture 38Bilal Ahmed
 
Grade 12 U0-L3-ErrorEstimation
Grade 12 U0-L3-ErrorEstimationGrade 12 U0-L3-ErrorEstimation
Grade 12 U0-L3-ErrorEstimationgruszecki1
 
CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 18CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 18Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29CS201- Introduction to Programming- Lecture 29
CS201- Introduction to Programming- Lecture 29Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25CS201- Introduction to Programming- Lecture 25
CS201- Introduction to Programming- Lecture 25Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 14
CS201- Introduction to Programming- Lecture 14CS201- Introduction to Programming- Lecture 14
CS201- Introduction to Programming- Lecture 14Bilal Ahmed
 

What's hot (20)

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

Viewers also liked

Top dominating models in TVCs in the year 2015
Top dominating models in TVCs in the year 2015Top dominating models in TVCs in the year 2015
Top dominating models in TVCs in the year 2015Mediatrack Pakistan
 
PTI and PAT Sep 3 rally Coverage analysis
PTI and PAT Sep 3 rally Coverage analysis PTI and PAT Sep 3 rally Coverage analysis
PTI and PAT Sep 3 rally Coverage analysis Mediatrack Pakistan
 
Top most innovative billboards around the world
Top most innovative billboards around the worldTop most innovative billboards around the world
Top most innovative billboards around the worldMediatrack Pakistan
 
Afghan media industry analysis January 2017
Afghan media industry analysis January 2017Afghan media industry analysis January 2017
Afghan media industry analysis January 2017Mediatrack Pakistan
 
Studi Confortiani 02: Realisasi intuisi awal
Studi Confortiani 02: Realisasi intuisi awalStudi Confortiani 02: Realisasi intuisi awal
Studi Confortiani 02: Realisasi intuisi awalMisionaris Xaverian
 
ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23ENG101- English Comprehension- Lecture 23
ENG101- English Comprehension- Lecture 23Bilal Ahmed
 
ENG101- English Comprehension- Lecture 35
ENG101- English Comprehension- Lecture 35ENG101- English Comprehension- Lecture 35
ENG101- English Comprehension- Lecture 35Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31CS201- Introduction to Programming- Lecture 31
CS201- Introduction to Programming- Lecture 31Bilal Ahmed
 
ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30ENG101- English Comprehension- Lecture 30
ENG101- English Comprehension- Lecture 30Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 27
MGT101 - Financial Accounting- Lecture 27MGT101 - Financial Accounting- Lecture 27
MGT101 - Financial Accounting- Lecture 27Bilal Ahmed
 
CS101- Introduction to Computing- Lecture 43
CS101- Introduction to Computing- Lecture 43CS101- Introduction to Computing- Lecture 43
CS101- Introduction to Computing- Lecture 43Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32CS201- Introduction to Programming- Lecture 32
CS201- Introduction to Programming- Lecture 32Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27CS201- Introduction to Programming- Lecture 27
CS201- Introduction to Programming- Lecture 27Bilal Ahmed
 
ENG101- English Comprehension- Lecture 42
ENG101- English Comprehension- Lecture 42ENG101- English Comprehension- Lecture 42
ENG101- English Comprehension- Lecture 42Bilal Ahmed
 
MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28MGT101 - Financial Accounting- Lecture 28
MGT101 - Financial Accounting- Lecture 28Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22CS201- Introduction to Programming- Lecture 22
CS201- Introduction to Programming- Lecture 22Bilal Ahmed
 
MTH101 - Calculus and Analytical Geometry- Lecture 44
MTH101 - Calculus and Analytical Geometry- Lecture 44MTH101 - Calculus and Analytical Geometry- Lecture 44
MTH101 - Calculus and Analytical Geometry- Lecture 44Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43CS201- Introduction to Programming- Lecture 43
CS201- Introduction to Programming- Lecture 43Bilal Ahmed
 

Viewers also liked (20)

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

Similar to CS201- Introduction to Programming- Lecture 07

21 High-quality programming code construction part-ii
21 High-quality programming code construction part-ii21 High-quality programming code construction part-ii
21 High-quality programming code construction part-iimaznabili
 
CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44Bilal Ahmed
 
01 termoqu%e dmica
01 termoqu%e dmica01 termoqu%e dmica
01 termoqu%e dmicaIQPonce14
 
19 Algorithms and complexity
19 Algorithms and complexity19 Algorithms and complexity
19 Algorithms and complexitymaznabili
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24Bilal Ahmed
 

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

21 High-quality programming code construction part-ii
21 High-quality programming code construction part-ii21 High-quality programming code construction part-ii
21 High-quality programming code construction part-ii
 
CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11CS201- Introduction to Programming- Lecture 11
CS201- Introduction to Programming- Lecture 11
 
CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44CS201- Introduction to Programming- Lecture 44
CS201- Introduction to Programming- Lecture 44
 
Chapter 6 testbench
Chapter 6 testbenchChapter 6 testbench
Chapter 6 testbench
 
01 termoqu%e dmica
01 termoqu%e dmica01 termoqu%e dmica
01 termoqu%e dmica
 
Resumen termoquimica1
Resumen termoquimica1Resumen termoquimica1
Resumen termoquimica1
 
19 Algorithms and complexity
19 Algorithms and complexity19 Algorithms and complexity
19 Algorithms and complexity
 
CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23CS201- Introduction to Programming- Lecture 23
CS201- Introduction to Programming- Lecture 23
 
CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04
 
10 het eerste licht
10 het eerste licht10 het eerste licht
10 het eerste licht
 
CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24CS201- Introduction to Programming- Lecture 24
CS201- Introduction to Programming- Lecture 24
 

More from Bilal Ahmed

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19Bilal Ahmed
 
CS201- Introduction to Programming- Lecture 17
CS201- Introduction to Programming- Lecture 17CS201- Introduction to Programming- Lecture 17
CS201- Introduction to Programming- Lecture 17Bilal Ahmed
 

More from Bilal Ahmed (6)

CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45CS201- Introduction to Programming- Lecture 45
CS201- Introduction to Programming- Lecture 45
 
CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42CS201- Introduction to Programming- Lecture 42
CS201- Introduction to Programming- Lecture 42
 
CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39CS201- Introduction to Programming- Lecture 39
CS201- Introduction to Programming- Lecture 39
 
CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21CS201- Introduction to Programming- Lecture 21
CS201- Introduction to Programming- Lecture 21
 
CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19CS201- Introduction to Programming- Lecture 19
CS201- Introduction to Programming- Lecture 19
 
CS201- Introduction to Programming- Lecture 17
CS201- Introduction to Programming- Lecture 17CS201- Introduction to Programming- Lecture 17
CS201- Introduction to Programming- Lecture 17
 

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