Chapter 02 - Primitive Data and Definite Loops

Chapter 02 - Primitive Data and Definite Loops -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU 3ULPLWLYH 'DWD DQG 'HILQLWH /RRSV &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 7KH IRU /RRS 7UDFLQJ IRU /RRSV SULQW 9HUVXV SULQWOQ 1HVWHG IRU /RRSV 0DQDJLQJ &RPSOH[LW\ 6FRSH 3VHXGRFRGH $ 'HFUHPHQWLQJ IRU /RRS &ODVV &RQVWDQWV &DVH 6WXG\ $ &RPSOH[ )LJXUH 3UREOHP 'HFRPSRVLWLRQ DQG 3VHXGRFRGH /LQH 3DWWHUQ 7DEOH ,QLWLDO 6WUXFWXUHG 9HUVLRQ $GGLQJ D &ODVV &RQVWDQW )XUWKHU 9DULDWLRQV %DVLF 'DWD &RQFHSWV 3ULPLWLYH 7\SHV ([SUHVVLRQV /LWHUDOV $ULWKPHWLF 2SHUDWRUV 3UHFHGHQFH 0L[LQJ 7\SHV DQG &DVWLQJ 9DULDEOHV $VVLJQPHQW 'HFODUDWLRQ 9DULDWLRQV 6WULQJ &RQFDWHQDWLRQ ,QFUHPHQW 'HFUHPHQW 2SHUDWRUV 9DULDEOHV DQG 0L[LQJ 7\SHV ,QWURGXFWLRQ 1RZ WKDW \RX NQRZ VRPHWKLQJ DERXW WKH EDVLF VWUXFWXUH RI -DYD SURJUDPV \RX DUH UHDG\ WR OHDUQ KRZ WR VROYH SUREOHPV WKDW DUH IDLUO\ FRPSOH[ <RX ZLOO VWLOO EH UHVWULFWHG WR SURJUDPV WKDW SURGXFH RXWSXW EXW ZH ZLOO EHJLQ WR H[SORUH VRPH RI WKH DVSHFWV RI SURJUDPPLQJ WKDW UHTXLUH SUREOHP VROYLQJ VNLOOV 7KH ILUVW KDOI RI WKH FKDSWHU ILOOV LQ WZR LPSRUWDQW DUHDV )LUVW LW H[DPLQHV H[SUHVVLRQV KRZ WR H[SUHVV VLPSOH FRPSXWDWLRQV LQ -DYD SDUWLFXODUO\ WKRVH LQYROYLQJ QXPHULF GDWD 6HFRQG LW GLVFXVVHV SURJUDP HOHPHQWV FDOOHG YDULDEOHV WKDW FDQ FKDQJH LQ YDOXH DV WKH SURJUDP H[HFXWHV 7KH VHFRQG KDOI RI WKH FKDSWHU LQWURGXFHV \RXU ILUVW FRQWURO VWUXFWXUH WKH IRU ORRS <RX XVH WKLV VWUXFWXUH WR UHSHDW DFWLRQV LQ D SURJUDP 7KLV LV XVHIXO ZKHQHYHU \RX ILQG D SDWWHUQ LQ D FRPSOH[ WDVN OLNH WKH FUHDWLRQ RI D FRPSOH[ ILJXUH EHFDXVH \RX FDQ XVH D IRU ORRS WR UHSHDW DQ DFWLRQ WKDW FUHDWHV D SDUWLFXODU SDWWHUQ 7KH FKDOOHQJH LV ILQGLQJ HDFK SDWWHUQ DQG ILJXULQJ RXW ZKDW UHSHDWHG DFWLRQV ZLOO UHSURGXFH LW ,Q H[SORULQJ WKLV ZH ZLOO H[DPLQH D YDULDWLRQ RI SULQWOQ NQRZQ DV SULQW WKDW DOORZV XV WR EUHDN XS D FRPSOH[ OLQH RI RXWSXW LQWR VHYHUDO SLHFHV RI RXWSXW 7KH IRU ORRS LV D IOH[LEOH FRQWURO VWUXFWXUH WKDW FDQ EH XVHG IRU PDQ\ WDVNV EXW LQ WKLV FKDSWHU ZH XVH LW IRU GHILQLWH ORRSV ZKHUH \RX NQRZ H[DFWO\ KRZ PDQ\ WLPHV \RX ZDQW WR SHUIRUP D SDUWLFXODU WDVN ,Q &KDSWHU ZH ZLOO GLVFXVV KRZ WR ZULWH LQGHILQLWH ORRSV ZKHUH \RX GRQ W NQRZ LQ DGYDQFH KRZ PDQ\ WLPHV WR SHUIRUP D WDVN 63 %DVLF 'DWD &RQFHSWV 3URJUDPV PDQLSXODWH LQIRUPDWLRQ DQG LQIRUPDWLRQ FRPHV LQ PDQ\ IRUPV -DYD LV D VWURQJO\ W\SHG ODQJXDJH ZKLFK PHDQV WKDW -DYD UHTXLUHV XV WR EH H[SOLFLW DERXW ZKDW NLQG RI LQIRUPDWLRQ ZH LQWHQG WR PDQLSXODWH (YHU\WKLQJ WKDW ZH PDQLSXODWH LQ D -DYD SURJUDP ZLOO EH RI D FHUWDLQ W\SH DQG ZH ZLOO FRQVWDQWO\ ILQG RXUVHOYHV WHOOLQJ -DYD ZKDW W\SHV RI GDWD ZH LQWHQG WR XVH $ GHFLVLRQ ZDV PDGH HDUO\ LQ WKH GHVLJQ RI -DYD WR KDYH WZR GLIIHUHQW NLQGV RI GDWD SULPLWLYH GDWD DQG REMHFWV 7KH GHVLJQHUV DGPLW WKDW WKLV GHFLVLRQ ZDV PDGH SXUHO\ RQ WKH EDVLV RI SHUIRUPDQFH WR PDNH -DYD SURJUDPV UXQ IDVWHU 7KHLU GHFLVLRQ LV XQIRUWXQDWH EHFDXVH LW PHDQV WKDW ZH KDYH WR OHDUQ WZR VHWV RI UXOHV DERXW KRZ GDWD ZRUNV %XW WKLV LV RQH RI WKRVH WLPHV ZKHQ \RX VLPSO\ KDYH WR SD\ WKH SULFH LI \RX ZDQW WR XVH DQ LQGXVWULDO VWUHQJWK SURJUDPPLQJ ODQJXDJH 7R PDNH RXU OLYHV D OLWWOH HDVLHU ZH ZLOO VWXG\ WKH SULPLWLYH GDWD W\SHV ILUVW LQ WKLV FKDSWHU DQG WKHQ WXUQ RXU DWWHQWLRQ WR REMHFWV LQ WKH QH[W FKDSWHU 3ULPLWLYH 7\SHV 7KHUH DUH HLJKW SULPLWLYH GDWD W\SHV LQ -DYD RI ZKLFK IRXU DUH FRQVLGHUHG IXQGDPHQWDO 7KH RWKHU IRXU W\SHV DUH YDULDWLRQV WKDW H[LVW IRU SURJUDPV WKDW KDYH VSHFLDO UHTXLUHPHQWV 7KH IRXU W\SHV ZH ZLOO H[SORUH DUH OLVWHG LQ WKH IROORZLQJ WDEOH &RPPRQO\ 8VHG 3ULPLWLYH 7\SHV LQ -DYD 7\SH int double 'HVFULSWLRQ ([DPSOHV LQWHJHUV ZKROH QXPEHUV 42 -3 18 20493 0 7.35 14.9 -19.83423 UHDO QXPEHUV char 'a' 'X' '!' VLQJOH FKDUDFWHUV boolean true false ORJLFDO YDOXHV 7KH W\SH QDPHV LQW GRXEOH FKDU DQG ERROHDQ DUH -DYD NH\ZRUGV WKDW ZH ZLOO XVH LQ RXU SURJUDPV WR OHW WKH FRPSLOHU NQRZ WKDW ZH LQWHQG WR XVH WKDW W\SH RI GDWD ,W PD\ VHHP RGG WR KDYH RQH W\SH IRU LQWHJHUV DQG DQRWKHU W\SH IRU UHDO QXPEHUV ,VQ W HYHU\ LQWHJHU D UHDO QXPEHU" 7KH DQVZHU LV \HV EXW WKHVH DUH IXQGDPHQWDOO\ GLIIHUHQW W\SHV RI QXPEHUV 7KH GLIIHUHQFH LV VR JUHDW WKDW ZH PDNH WKLV GLVWLQFWLRQ HYHQ LQ (QJOLVK :H GRQ W DVN +RZ PXFK VLVWHUV GR \RX KDYH" RU +RZ PDQ\ GR \RX ZHLJK" :H UHDOL]H WKDW VLVWHUV FRPH LQ GLVFUHWH LQWHJHU TXDQWLWLHV VLVWHUV VLVWHU VLVWHUV VLVWHUV DQG VR RQ DQG ZH XVH WKH ZRUG PDQ\ IRU LQWHJHU TXDQWLWLHV +RZ PDQ\ VLVWHUV GR \RX KDYH" 6LPLODUO\ ZH UHDOL]H WKDW ZHLJKW FDQ YDU\ E\ WLQ\ DPRXQWV SRXQGV YHUVXV SRXQGV YHUVXV SRXQGV DQG VR RQ DQG ZH XVH WKH ZRUG PXFK IRU WKHVH UHDO YDOXHG TXDQWLWLHV +RZ PXFK GR \RX ZHLJK" ,Q SURJUDPPLQJ WKLV GLVWLQFWLRQ LV HYHQ PRUH LPSRUWDQW EHFDXVH LQWHJHUV DQG UHDOV DUH UHSUHVHQWHG LQ D GLIIHUHQW ZD\ LQ WKH FRPSXWHU V PHPRU\ ,QWHJHUV DUH VWRUHG H[DFWO\ ZKLOH UHDOV DUH VWRUHG DV DSSUR[LPDWLRQV ZLWK D OLPLWHG QXPEHU RI GLJLWV RI DFFXUDF\ :H ZLOO VHH WKDW VWRULQJ YDOXHV DV DSSUR[LPDWLRQV FDQ OHDG WR URXQG RII HUURUV ZKHQ \RX XVH UHDO YDOXHV 64 7KH QDPH GRXEOH IRU UHDO YDOXHV LVQ W YHU\ FOHDU ,W V DQ DFFLGHQW RI KLVWRU\ WKDW ZH KDYH WKLV QDPH LQ PXFK WKH VDPH ZD\ WKDW ZH VWLOO WDON DERXW GLDOLQJ D QXPEHU RQ RXU WHOHSKRQHV HYHQ WKRXJK PRGHUQ WHOHSKRQHV GRQ W KDYH D GLDO 7KH & SURJUDPPLQJ ODQJXDJH LQWURGXFHG D W\SH FDOOHG IORDW IRU VWRULQJ UHDO QXPEHUV VKRUW IRU IORDWLQJ SRLQW QXPEHU %XW IORDWV KDG OLPLWHG DFFXUDF\ DQG DQRWKHU W\SH ZDV LQWURGXFHG FDOOHG GRXEOH VKRUW IRU GRXEOH SUHFLVLRQ GRXEOH WKH SUHFLVLRQ RI D VLPSOH IORDW $V PHPRU\ KDV EHFRPH FKHDSHU SHRSOH KDYH PRYHG PRUH WRZDUGV XVLQJ GRXEOH DV WKH GHIDXOW IRU IORDWLQJ SRLQW YDOXHV ,Q KLQGVLJKW LW PLJKW KDYH EHHQ EHWWHU WR XVH WKH ZRUG IORDW IRU ZKDW LV QRZ FDOOHG GRXEOH DQG WKHQ XVHG D ZRUG OLNH KDOI IRU WKH YDOXHV ZLWK OHVV DFFXUDF\ EXW LW V WRXJK WR FKDQJH KDELWV WKDW DUH VR LQJUDLQHG 6R SURJUDPPLQJ ODQJXDJHV ZLOO FRQWLQXH WR XVH WKH ZRUG GRXEOH IRU IORDWLQJ SRLQW QXPEHUV DQG SHRSOH ZLOO VWLOO WDON DERXW GLDOLQJ SHRSOH RQ WKH SKRQH HYHQ LI WKH\ YH QHYHU WRXFKHG D WHOHSKRQH GLDO ([SUHVVLRQV :KHQ ZULWLQJ SURJUDPV ZH ZLOO RIWHQ QHHG WR LQFOXGH YDOXHV DQG FDOFXODWLRQV 7KH WHFKQLFDO WHUP IRU WKHVH LV H[SUHVVLRQV ([SUHVVLRQ $ VLPSOH YDOXH RU D VHW RI RSHUDWLRQV WKDW SURGXFHV D YDOXH 7KH VLPSOHVW H[SUHVVLRQ LV D VSHFLILF YDOXH OLNH RU :H FDOO WKHVH OLWHUDO YDOXHV RU OLWHUDOV 0RUH FRPSOH[ H[SUHVVLRQV LQYROYH FRPELQLQJ VLPSOH YDOXHV 6XSSRVH IRU H[DPSOH WKDW \RX ZDQW WR NQRZ KRZ PDQ\ ERWWOHV RI ZDWHU \RX KDYH ,I \RX KDYH WZR SDFNV IRXU SDFNV DQG LQGLYLGXDO ERWWOHV WKHQ \RX FRXOG FRPSXWH WKH WRWDO QXPEHU RI ERWWOHV ZLWK WKH IROORZLQJ H[SUHVVLRQ (2 * 6) + (4 * 4) + 2 1RWLFH WKDW ZH XVH WKH DVWHULVN WR UHSUHVHQW PXOWLSOLFDWLRQ DQG WKDW ZH XVH SDUHQWKHVHV WR JURXS SDUWV RI WKH H[SUHVVLRQ 7KH FRPSXWHU GHWHUPLQHV WKH YDOXH RI DQ H[SUHVVLRQ E\ HYDOXDWLQJ LW (YDOXDWLRQ 7KH SURFHVV RI REWDLQLQJ WKH YDOXH RI DQ H[SUHVVLRQ 7KH YDOXH REWDLQHG ZKHQ DQ H[SUHVVLRQ LV HYDOXDWHG LV FDOOHG WKH UHVXOW &RPSOH[ H[SUHVVLRQV DUH IRUPHG XVLQJ RSHUDWRUV 2SHUDWRU $ VSHFLDO V\PERO OLNH + RU * XVHG WR LQGLFDWH DQ RSHUDWLRQ WR EH SHUIRUPHG RQ RQH RU PRUH YDOXHV 7KH YDOXHV XVHG LQ WKH H[SUHVVLRQ DUH FDOOHG RSHUDQGV )RU H[DPSOH FRQVLGHU WKH IROORZLQJ VLPSOH H[SUHVVLRQV 65 3 + 29 4 * 5 7KH RSHUDWRUV KHUH DUH WKH + DQG * DQG WKH RSHUDQGV DUH VLPSOH QXPEHUV 3 | operand 4 | operand + | operator * | operator 29 | operand 5 | operand :KHQ \RX IRUP FRPSOH[ H[SUHVVLRQV WKHVH VLPSOHU H[SUHVVLRQV FDQ LQ WXUQ EHFRPH RSHUDQGV IRU RWKHU RSHUDWRUV )RU H[DPSOH FRQVLGHU WKH IROORZLQJ H[SUHVVLRQ (3 + 29) - (4 * 5) :KLFK KDV WZR OHYHOV RI RSHUDWRUV (3 + 29) (4 * 5) | | | | | | | operand operator operand | operand operator operand | | | | | +------------------+ | +------------------+ operand operator operand 7KH SOXV RSHUDWRU KDV VLPSOH RSHUDQGV RI DQG DQG WKH WLPHV RSHUDWRU KDV VLPSOH RSHUDQGV RI DQG EXW WKH PLQXV RSHUDWRU KDV RSHUDQGV WKDW DUH HDFK SDUHQWKHVL]HG H[SUHVVLRQV ZLWK RSHUDWRUV RI WKHLU RZQ 7KXV FRPSOH[ H[SUHVVLRQV FDQ EH EXLOW IURP VPDOOHU H[SUHVVLRQV $W WKH ORZHVW OHYHO \RX KDYH VLPSOH QXPEHUV 7KHVH DUH XVHG DV RSHUDQGV WR PDNH PRUH FRPSOH[ H[SUHVVLRQV ZKLFK LQ WXUQ FDQ EH XVHG DV RSHUDQGV LQ HYHQ PRUH FRPSOH[ H[SUHVVLRQV 7KHUH DUH PDQ\ WKLQJV \RX FDQ GR ZLWK H[SUHVVLRQV 2QH RI WKH VLPSOHVW WKLQJV \RX FDQ GR LV WR SULQW WKH YDOXH RI DQ H[SUHVVLRQ XVLQJ D SULQWOQ VWDWHPHQW )RU H[DPSOH LI \RX VD\ System.out.println(42); System.out.println(2 + 2); \RX ZLOO JHW WKH IROORZLQJ WZR OLQHV RI RXWSXW 42 4 1RWLFH WKDW IRU WKH VHFRQG SULQWOQ WKH FRPSXWHU HYDOXDWHV WKH H[SUHVVLRQ DGGLQJ SULQWV WKH UHVXOW LQ WKLV FDVH DQG DQG <RX ZLOO VHH PDQ\ GLIIHUHQW RSHUDWRUV DV \RX SURJUHVV WKURXJK WKH ERRN DOO RI ZKLFK FDQ EH XVHG WR IRUP H[SUHVVLRQV ([SUHVVLRQV FDQ EH DUELWUDULO\ FRPSOH[ ZLWK DV PDQ\ RSHUDWRUV DV \RX OLNH )RU WKDW UHDVRQ ZKHQ ZH WHOO \RX $Q H[SUHVVLRQ FDQ EH XVHG KHUH ZH ZLOO RIWHQ SRLQW RXW WKDW ZH PHDQ DUELWUDU\ H[SUHVVLRQV WR HPSKDVL]H WKDW \RX FDQ XVH FRPSOH[ H[SUHVVLRQV DV ZHOO DV VLPSOH YDOXHV 66 /LWHUDOV 7KH VLPSOHVW H[SUHVVLRQV UHIHU WR YDOXHV GLUHFWO\ XVLQJ ZKDW DUH NQRZQ DV OLWHUDOV $Q LQWHJHU OLWHUDO FRQVLGHUHG WR EH RI W\SH LQW LV D VHTXHQFH RI GLJLWV ZLWK RU ZLWKRXW D OHDGLQJ VLJQ 3 482 -29434 0 92348 +9812 $ IORDWLQJ SRLQW OLWHUDO FRQVLGHUHG WR EH RI W\SH GRXEOH ZLOO LQFOXGH D GHFLPDO SRLQW DV LQ 298.4 0.284 207. .2843 -17.452 -.98 1RWLFH WKDW 207. LV FRQVLGHUHG D GRXEOH HYHQ WKRXJK LW FRLQFLGHV ZLWK DQ LQWHJHU EHFDXVH RI WKH GHFLPDO SRLQW /LWHUDOV RI W\SH GRXEOH FDQ DOVR EH H[SUHVVHG LQ VFLHQWLILF QRWDWLRQ D QXPEHU IROORZHG E\ e IROORZHG E\ DQ LQWHJHU DV LQ 2.3e4 1e-5 3.84e92 2.458e12 7KH ILUVW QXPEHU DERYH UHSUHVHQWV WLPHV WR WKH WK SRZHU ZKLFK HTXDOV WKRXVDQG (YHQ WKRXJK WKLV KDSSHQV WR FRLQFLGH ZLWK DQ LQWHJHU LW LV FRQVLGHUHG WR EH D YDOXH RI W\SH GRXEOH EHFDXVH LW LV H[SUHVVHG LQ VFLHQWLILF QRWDWLRQ 7KH VHFRQG QXPEHU UHSUHVHQWV WLPHV WR WKH SRZHU ZKLFK LV HTXDO WR 7KH WKLUG YDOXH UHSUHVHQWV WLPHV WR WKH QG SRZHU 7KH IRXUWK UHSUHVHQWV WLPHV WR WKH WK SRZHU &KDUDFWHU OLWHUDOV RI W\SH FKDU DUH HQFORVHG LQ VLQJOH TXRWDWLRQ PDUNV DQG FDQ LQFOXGH MXVW RQH FKDUDFWHU 'a' 'm' 'X' '!' '3' '\\' $OO RI WKHVH DUH RI W\SH FKDU 1RWLFH WKDW WKH ODVW H[DPSOH XVHV DQ HVFDSH VHTXHQFH WR UHSUHVHQW WKH EDFNVODVK FKDUDFWHU <RX FDQ HYHQ UHIHU WR WKH VLQJOH TXRWDWLRQ FKDUDFWHU XVLQJ DQ HVFDSH VHTXHQFH '\'' 5HPHPEHU WKDW WKH SULPLWLYH W\SH ERROHDQ VWRUHV ORJLFDO LQIRUPDWLRQ /RJLF GHDOV ZLWK MXVW WZR SRVVLELOLWLHV WUXH DQG IDOVH 7KHVH WZR ZRUGV DUH NH\ZRUGV LQ -DYD WKDW DUH WKH WZR OLWHUDO YDOXHV RI W\SH ERROHDQ true false $ULWKPHWLF 2SHUDWRUV 7KH EDVLF DULWKPHWLF RSHUDWRUV DUH DV IROORZV 67 $ULWKPHWLF 2SHUDWRUV LQ -DYD 2SHUDWRU 0HDQLQJ DGGLWLRQ VXEWUDFWLRQ PXOWLSOLFDWLRQ GLYLVLRQ UHPDLQGHU RU PRG 7KH DGGLWLRQ DQG VXEWUDFWLRQ RSHUDWRUV VKRXOG EH IDPLOLDU WR \RX 7KH DVWHULVN DV D PXOWLSOLFDWLRQ RSHUDWRU PLJKW EH D VXUSULVH IRU QRQSURJUDPPHUV EXW GRHVQ W WDNH ORQJ WR JHW XVHG WR 'LYLVLRQ LV DOVR IDLUO\ IDPLOLDU DOWKRXJK DV ZH OO VHH -DYD KDV WZR GLIIHUHQW GLYLVLRQ RSHUDWLRQV 7KH UHPDLQGHU RU PRG RSHUDWLRQ LV WKH RQH WKDW LV PRVW OLNHO\ WR EH XQIDPLOLDU WR \RX 'LYLVLRQ SUHVHQWV D SUREOHP ZKHQ WKH RSHUDQGV DUH LQWHJHUV :KHQ \RX GLYLGH E\ IRU H[DPSOH \RX GR QRW JHW DQ LQWHJHU UHVXOW 7KHUHIRUH LQWHJHU GLYLVLRQ LV H[SUHVVHG DV WZR GLIIHUHQW LQWHJHUV D TXRWLHQW DQG D UHPDLQGHU 119 --5 = 23 (quotient) with 4 (remainder) ([DPSOH 5HVXOW ,Q WHUPV RI WKH DULWKPHWLF RSHUDWRUV 119 / 5 evaluates to 23 119 % 5 evaluates to 4 /RQJ GLYLVLRQ FDOFXODWLRQV DUH SHUIRUPHG OLNH WKLV 31 _______ 34 ) 1079 102 ---59 34 -25 +HUH GLYLGLQJ E\ \LHOGV ZRXOG EH GHVFULEHG OLNH WKLV 1079 / 34 evaluates to 31 1079 % 34 evaluates to 25 ZLWK D UHPDLQGHU RI 8VLQJ DULWKPHWLF RSHUDWRUV WKH SUREOHP ,W WDNHV D ZKLOH WR JHW XVHG WR LQWHJHU GLYLVLRQ )RU WKH GLYLVLRQ RSHUDWRU / WKH NH\ WKLQJ WR NHHS LQ PLQG LV WKDW LW WUXQFDWHV DQ\WKLQJ DIWHU WKH GHFLPDO SRLQW 6R LI \RX LPDJLQH FRPSXWLQJ DQ DQVZHU RQ D FDOFXODWRU MXVW WKLQN RI LJQRULQJ DQ\WKLQJ DIWHU WKH GHFLPDO SRLQW 19 / 5 is 3.8 on a calculator, so 19 / 5 evaluates to 3 207 / 10 is 20.7 on a calculator, so 203 / 10 evaluates to 20 3 / 8 is 0.375 on a calculator, so 3 / 8 evaluates to 0 68 7KH UHPDLQGHU RSHUDWRU LV XVXDOO\ UHIHUUHG WR DV WKH PRG RSHUDWRU RU VLPSO\ PRG 7KH PRG RSHUDWRU OHWV \RX NQRZ KRZ PXFK ZDV OHIW XQDFFRXQWHG IRU E\ WKH WUXQFDWHG GLYLVLRQ RSHUDWRU <RX KDYH WR WKLQN LQ WHUPV RI ZKDW WKH WUXQFDWHG GLYLVLRQ RSHUDWRU JLYHV DV D UHVXOW DQG WKHQ \RX FDQ GHWHUPLQH ZKDW LV OHIW RYHU )RU H[DPSOH JLYHQ WKH H[DPSOHV DERYH ZH G FRPSXWH WKH PRG UHVXOWV DV IROORZV Mod First What does division How much is Problem divide account for? left over? Answer? -----------------------------------------------------------------------19 % 5 19 / 5 is 3 3 * 5 is 15 19 - 15 is 4 4 207 % 10 207 / 10 is 20 20 * 10 is 200 207 - 200 is 7 7 3 % 8 3 / 8 is 0 0 * 8 is 0 3 - 0 is 3 3 ,Q HDFK FDVH ZH ILJXUH RXW KRZ PXFK RI WKH QXPEHU LV DFFRXQWHG IRU E\ WKH WUXQFDWHG GLYLVLRQ RSHUDWRU 7KH PRG RSHUDWRU JLYHV \RX DQ\ H[FHVV WKH UHPDLQGHU 3XWWLQJ WKH WDEOH DERYH LQWR D IRUPXOD \RX FDQ WKLQN RI WKH PRG RSHUDWRU DV EHKDYLQJ DV IROORZV x % y = x - (x / y) * y <RX FDQ JHW D UHVXOW RI IRU WKH PRG RSHUDWRU 7KLV KDSSHQV ZKHQ RQH QXPEHU JRHV HYHQO\ LQWR DQRWKHU )RU H[DPSOH HDFK RI WKH IROORZLQJ H[SUHVVLRQV HYDOXDWHV WR EHFDXVH WKH VHFRQG QXPEHU JRHV HYHQO\ LQWR WKH ILUVW QXPEHU 28 % 7 95 % 5 44 % 2 7KH PRG RSHUDWRU KDV PDQ\ XVHIXO DSSOLFDWLRQV LQ FRPSXWHU SURJUDPV +HUH DUH MXVW D IHZ LGHDV WHVWLQJ ZKHWKHU D QXPEHU LV HYHQ RU RGG QXPEHU LV IRU HYHQV QXPEHU ILQGLQJ LQGLYLGXDO GLJLWV RI D QXPEHU H J QXPEHU LV WKH ILQDO GLJLW ILQGLQJ WKH ODVW IRXU GLJLWV RI D VRFLDO VHFXULW\ QXPEHU QXPEHU LV IRU RGGV )RU IORDWLQJ SRLQW YDOXHV YDOXHV RI W\SH GRXEOH WKH GLYLVLRQ RSHUDWRU GRHV ZKDW ZH FRQVLGHU QRUPDO GLYLVLRQ 6R HYHQ WKRXJK WKH H[SUHVVLRQ HYDOXDWHV WR WKH H[SUHVVLRQ HYDOXDWHV WR 7KH UHPDLQGHU RSHUDWRU FDQ EH XVHG ZLWK GRXEOHV DV ZHOO DV ZLWK LQWV DQG LW KDV D VLPLODU PHDQLQJ <RX FRQVLGHU KRZ PXFK LV OHIW RYHU ZKHQ \RX WDNH DZD\ DV PDQ\ ZKROH YDOXHV DV \RX FDQ )RU H[DPSOH WKH H[SUHVVLRQ HYDOXDWHV WR EHFDXVH \RX FDQ WDNH DZD\ IRXU V IURP OHDYLQJ \RX ZLWK OHIW RYHU 3UHFHGHQFH -DYD H[SUHVVLRQV DUH OLNH FRPSOH[ QRXQ SKUDVHV LQ (QJOLVK 6XFK SKUDVHV DUH VXEMHFW WR DPELJXLW\ DV LQ WKH PDQ RQ WKH KLOO E\ WKH ULYHU ZLWK WKH WHOHVFRSH ,V WKH ULYHU E\ WKH KLOO RU E\ WKH PDQ" ,V WKH PDQ KROGLQJ WKH WHOHVFRSH RU LV WKH WHOHVFRSH RQ WKH KLOO RU LV WKH WHOHVFRSH LQ WKH ULYHU" <RX GRQ W NQRZ KRZ WR JURXS WKH YDULRXV SDUWV WRJHWKHU <RX FDQ JHW WKH VDPH NLQG RI DPELJXLW\ LI SDUHQWKHVHV DUHQ W XVHG WR JURXS WKH SDUWV RI D -DYD H[SUHVVLRQ )RU H[DPSOH WKH H[SUHVVLRQ KDV WZR RSHUDWRUV :KLFK LV SHUIRUPHG ILUVW" <RX FRXOG LQWHUSUHW WKLV WZR ZD\V 69 2 + 3 * 4 \---/ 5 * 4 \-----/ 20 2 + 3 * 4 \---/ 2 + 12 \-----/ 14 7KH ILUVW RI WKHVH HYDOXDWHV WR ZKLOH WKH VHFRQG HYDOXDWHV WR 7R GHDO ZLWK WKH DPELJXLW\ -DYD KDV UXOHV RI SUHFHGHQFH WKDW GHWHUPLQH KRZ WR JURXS WKH YDULRXV SDUWV WRJHWKHU 3UHFHGHQFH 7KH ELQGLQJ SRZHU RI DQ RSHUDWRU ZKLFK GHWHUPLQHV KRZ WR JURXS SDUWV RI DQ H[SUHVVLRQ 5XOHV RI SUHFHGHQFH DUH DSSOLHG ZKHQ WKH JURXSLQJ RI RSHUDWRUV LQ DQ H[SUHVVLRQ LV DPELJXRXV $Q RSHUDWRU ZLWK ORZ SUHFHGHQFH LV HYDOXDWHG DIWHU RSHUDWRUV RI KLJKHU SUHFHGHQFH :LWKLQ D JLYHQ OHYHO RI SUHFHGHQFH WKH RSHUDWRUV DUH HYDOXDWHG LQ RQH GLUHFWLRQ XVXDOO\ OHIW WR ULJKW )RU DULWKPHWLF H[SUHVVLRQV WKHUH DUH WZR OHYHOV RI SUHFHGHQFH 7KH PXOWLSOLFDWLYH RSHUDWRUV KDYH D KLJKHU OHYHO RI SUHFHGHQFH WKDQ WKH DGGLWLYH RSHUDWRUV 6R WKH H[SUHVVLRQ LQWHUSUHWHG DV 2 + 3 * 4 \---/ 2 + 12 \-----/ 14 LV :LWKLQ WKH VDPH OHYHO RI SUHFHGHQFH DULWKPHWLF RSHUDWRUV DUH HYDOXDWHG IURP OHIW WR ULJKW 7KLV RIWHQ GRHVQ W PDNH D GLIIHUHQFH LQ WKH ILQDO UHVXOW EXW RFFDVLRQDOO\ LW GRHV &RQVLGHU IRU H[DPSOH WKH H[SUHVVLRQ 40 - 25 - 9 ZKLFK HYDOXDWHV DV IROORZV 40 - 25 - 9 \-----/ 15 - 9 \-------/ 6 <RX ZRXOG JHW D GLIIHUHQW UHVXOW LI WKH VHFRQG PLQXV ZHUH HYDOXDWHG ILUVW <RX FDQ DOZD\V RYHUULGH SUHFHGHQFH ZLWK SDUHQWKHVHV )RU H[DPSOH LI \RX UHDOO\ ZDQW WKH VHFRQG PLQXV WR EH HYDOXDWHG ILUVW \RX FDQ IRUFH WKDW WR KDSSHQ E\ LQWURGXFLQJ SDUHQWKHVHV 40 - (25 - 9) ZKLFK HYDOXDWHV DV IROORZV 70 40 - (25 - 9) \------/ 40 16 \--------/ 24 :LWK DULWKPHWLF ZH DOVR KDYH ZKDW DUH NQRZQ DV XQDU\ SOXV DQG PLQXV ZLWK D VLQJOH RSHUDQG )RU H[DPSOH ZH FDQ ILQG WKH QHJDWLRQ RI E\ DVNLQJ IRU -8 7KHVH XQDU\ RSHUDWRUV KDYH D KLJKHU OHYHO RI SUHFHGHQFH ZKLFK DOORZV \RX WR IRUP H[SUHVVLRQV OLNH WKH IROORZLQJ 12 * -8 ZKLFK HYDOXDWHV WR :H ZLOO VHH PDQ\ RSHUDWRUV LQ WKH QH[W IHZ FKDSWHUV 7KH IROORZLQJ LV D SUHFHGHQFH WDEOH WKDW KDV WKH DULWKPHWLF RSHUDWRUV $V ZH OHDUQ DERXW PRUH RSHUDWRUV ZH OO XSGDWH WKLV WDEOH WR LQFOXGH WKHP DV ZHOO 7KH WDEOH LV RUGHUHG IURP KLJKHVW SUHFHGHQFH WR ORZHVW SUHFHGHQFH )RU H[DPSOH WKH WDEOH LQGLFDWHV WKDW -DYD ZLOO ILUVW JURXS SDUWV RI DQ H[SUHVVLRQ XVLQJ WKH XQDU\ RSHUDWRUV WKHQ LW ZLOO JURXS XVLQJ WKH PXOWLSOLFDWLYH RSHUDWRUV DQG RQO\ WKHQ ZLOO LW JURXS XVLQJ WKH DGGLWLYH RSHUDWRUV -DYD 2SHUDWRU 3UHFHGHQFH 'HVFULSWLRQ 2SHUDWRUV +, XQDU\ RSHUDWRUV PXOWLSOLFDWLYH RSHUDWRUV *, /, % +, DGGLWLYH RSHUDWRUV %HIRUH ZH OHDYH WKLV WRSLF OHW V ORRN DW D FRPSOH[ H[SUHVVLRQ DQG VHH KRZ LW LV HYDOXDWHG VWHS E\ VWHS &RQVLGHU WKH IROORZLQJ H[SUHVVLRQ 13 * 2 - 209 / 10 % 5 + 2 * 2 ,W KDV D WRWDO RI VL[ RSHUDWRUV WZR PXOWLSOLFDWLRQV RQH GLYLVLRQ RQH PRG RQH VXEWUDFWLRQ DQG RQH DGGLWLRQ 7KH PXOWLSOLFDWLRQV GLYLVLRQ DQG PRG ZLOO EH SHUIRUPHG ILUVW EHFDXVH WKH\ KDYH KLJKHU SUHFHGHQFH DQG WKH\ ZLOO EH SHUIRUPHG IURP OHIW WR ULJKW RUGHU EHFDXVH WKH\ DUH DOO DW WKH VDPH OHYHO RI SUHFHGHQFH 13 * 2 + 239 / 10 % 5 \----/ 26 + 239 / 10 % 5 \------/ 26 + 23 % 5 \-------/ 26 + 3 26 + 3 - 2 * 2 - 2 * 2 - 2 * 2 - 2 * 2 \---/ 4 1RZ ZH HYDOXDWH WKH DGGLWLYH RSHUDWRUV IURP OHIW WR ULJKW 71 26 + 3 4 \-------------/ 29 4 \-----------------/ 25 0L[LQJ 7\SHV DQG &DVWLQJ :H RIWHQ ILQG RXUVHOYHV PL[LQJ YDOXHV RI GLIIHUHQW W\SHV DQG ZDQWLQJ WR FRQYHUW IURP RQH W\SH WR DQRWKHU -DYD KDV VLPSOH UXOHV WKDW DYRLG FRQIXVLRQ DQG SURYLGHV D PHFKDQLVP IRU UHTXHVWLQJ WKDW D YDOXH EH FRQYHUWHG IURP RQH W\SH WR DQRWKHU :H RIWHQ ILQG RXUVHOYHV PL[LQJ LQWV DQG GRXEOHV :H PLJKW IRU H[DPSOH DVN -DYD WR FRPSXWH 7KLV H[SUHVVLRQ LQFOXGHV WKH LQW OLWHUDO DQG WKH GRXEOH OLWHUDO ,Q WKLV FDVH -DYD FRQYHUWV WKH LQW LQWR D GRXEOH DQG SHUIRUPV WKLV FRPSXWDWLRQ HQWLUHO\ ZLWK GRXEOH YDOXHV ,I -DYD HQFRXQWHUV DQ LQW ZKHUH LW ZDV H[SHFWLQJ D GRXEOH LW DOZD\V FRQYHUWV WKH LQW WR D GRXEOH 7KLV EHFRPHV SDUWLFXODUO\ LPSRUWDQW ZKHQ \RX IRUP H[SUHVVLRQV WKDW LQYROYH GLYLVLRQ ,I WKH WZR RSHUDQGV DUH ERWK RI W\SH LQW WKHQ -DYD ZLOO XVH LQWHJHU WUXQFDWHG GLYLVLRQ ,I HLWKHU RI WKH WZR RSHUDQGV LV RI W\SH GRXEOH KRZHYHU WKHQ LW ZLOO GR UHDO YDOXHG QRUPDO GLYLVLRQ )RU H[DPSOH HYDOXDWHV WR EXW DOO RI WKH IROORZLQJ HYDOXDWH WR 23.0 / 4 23. / 4 23 / 4.0 23 / 4. 23. / 4. 23.0 / 4.0 6RPHWLPHV \RX ZDQW -DYD WR JR WKH RWKHU ZD\ FRQYHUWLQJ D GRXEOH LQWR DQ LQW <RX FDQ DVN -DYD IRU WKLV FRQYHUVLRQ ZLWK D FDVW 7KLQN RI LW DV FDVWLQJ D YDOXH LQ D GLIIHUHQW OLJKW <RX UHTXHVW D FDVW E\ SXWWLQJ WKH QDPH RI WKH W\SH \RX ZDQW WR FDVW WR LQ SDUHQWKHVHV LQ IURQW RI WKH WKLQJ \RX ZDQW WR FDVW )RU H[DPSOH LI \RX VD\ (int) 4.75 \RX ZLOO JHW WKH LQW YDOXH :KHQ \RX FDVW D GRXEOH YDOXH WR DQ LQW LW VLPSO\ WUXQFDWHV DQ\WKLQJ DIWHU WKH GHFLPDO SRLQW ,I \RX ZDQW WR FDVW WKH UHVXOW RI DQ H[SUHVVLRQ \RX KDYH WR EH FDUHIXO WR XVH SDUHQWKHVHV )RU H[DPSOH VXSSRVH WKDW \RX KDYH VRPH ERRNV WKDW DUH IHHW ZLGH DQG \RX ZDQW WR NQRZ KRZ PDQ\ RI WKHP ZRXOG ILW LQ D ERRNVKHOI WKDW LV IHHW ZLGH <RX FRXOG GR D VWUDLJKW GLYLVLRQ RI EXW WKDW HYDOXDWHV WR D GRXEOH UHVXOW WKDW LV EHWZHHQ DQG $PHULFDQV XVH WKH SKUDVH DQG FKDQJH DV D ZD\ WR H[SUHVV WKH LGHD WKDW LW LV ODUJHU WKDQ EXW QRW DV ELJ DV ,Q WKLV FDVH ZH GRQ W FDUH DERXW WKH FKDQJH :H ZDQW WR FRPSXWH WKH SDUW <RX PLJKW IRUP WKH IROORZLQJ H[SUHVVLRQ (int) 2.5 / 0.15 8QIRUWXQDWHO\ WKLV H[SUHVVLRQ HYDOXDWHV WR WKH ZURQJ DQVZHU EHFDXVH WKH FDVW LV DSSOLHG WR ZKDWHYHU FRPHV ULJKW DIWHU LW ZKLFK LV WKH YDOXH 6R WKLV FDVWV LQWR WKH LQWHJHU GLYLGHV E\ DQG HYDOXDWHV WR DQG FKDQJH ZKLFK LVQ W DQ LQWHJHU DQG LVQ W WKH ULJKW DQVZHU <RX ZDQW WR IRUP WKLV H[SUHVVLRQ 72 (int) (2.5 / 0.15) 7KLV H[SUHVVLRQ SHUIRUPV WKH GLYLVLRQ ILUVW WR JHW DQG FKDQJH DQG WKHQ LW FDVWV WKDW YDOXH WR DQ LQW E\ WUXQFDWLQJ DQG HYDOXDWHV WR WKH LQW YDOXH ZKLFK LV WKH DQVZHU \RX UH ORRNLQJ IRU ,Q ODWHU FKDSWHUV ZH OO VHH RWKHU H[SUHVVLRQV LQYROYLQJ PL[HG W\SHV )RU H[DPSOH LQ &KDSWHU ZH ZLOO VHH WKDW HYHU\ YDOXH RI W\SH FKDU KDV D FRUUHVSRQGLQJ LQWHJHU YDOXH ZKLFK DOORZV IRU VWUDQJH H[SUHVVLRQV OLNH WKH IROORZLQJ 2 * 'a' (char) ('f' + 2) // produces 194 // produces 'h' 9DULDEOHV 3ULPLWLYH GDWD FDQ EH VWRUHG LQ WKH FRPSXWHU V PHPRU\ LQ D YDULDEOH 9DULDEOH $ PHPRU\ ORFDWLRQ ZLWK D QDPH DQG D W\SH WKDW VWRUHV D YDOXH 7KLQN RI WKH FRPSXWHU V PHPRU\ DV EHLQJ OLNH D JLDQW VSUHDGVKHHW WKDW KDV PDQ\ FHOOV ZKHUH GDWD FDQ EH VWRUHG :KHQ \RX WHOO -DYD WKDW \RX ZDQW WR KDYH D YDULDEOH \RX DUH DVNLQJ LW WR VHW DVLGH RQH RI WKRVH FHOOV IRU WKLV QHZ YDULDEOH ,QLWLDOO\ WKH FHOO ZLOO EH HPSW\ EXW \RX ZLOO KDYH WKH RSWLRQ WR VWRUH D YDOXH LQ WKH FHOO $QG DV ZLWK VSUHDGVKHHWV \RX ZLOO KDYH WKH RSWLRQ WR FKDQJH WKH YDOXH LQ WKDW FHOO ODWHU LI \RX ZDQW WR -DYD LV D OLWWOH PRUH SLFN\ WKDQ D VSUHDGVKHHW LQ WKDW LW UHTXLUHV \RX WR WHOO LW H[DFWO\ ZKDW NLQG RI GDWD \RX DUH JRLQJ WR VWRUH LQ WKDW FHOO )RU H[DPSOH LI \RX ZDQW WR VWRUH DQ LQWHJHU \RX QHHG WR WHOO -DYD WKDW \RX LQWHQG WR XVH W\SH LQW ,I \RX ZDQW WR VWRUH D UHDO YDOXH \RX QHHG WR WHOO -DYD WKDW \RX LQWHQG WR XVH D GRXEOH <RX DOVR KDYH WR GHFLGH RQ D QDPH WR XVH ZKHQ \RX ZDQW WR UHIHU WR WKLV PHPRU\ ORFDWLRQ 7KH QRUPDO UXOHV RI -DYD LGHQWLILHUV DSSO\ PXVW VWDUW ZLWK D OHWWHU DQG FDQ WKHQ KDYH DQ\ FRPELQDWLRQ RI OHWWHUV DQG GLJLWV 7KH VWDQGDUG FRQYHQWLRQ LQ -DYD LV WR VWDUW YDULDEOH QDPHV ZLWK D ORZHU FDVH OHWWHU DV LQ number RU digits DQG WR FDSLWDOL]H DQ\ VXEVHTXHQW ZRUGV DV LQ numberOfDigits 7R H[SORUH WKH EDVLF XVH RI YDULDEOHV OHW V H[DPLQH D SURJUDP WKDW FRPSXWHV DQ LQGLYLGXDO V ERG\ PDVV LQGH[ RU %0, +HDOWK SURIHVVLRQDOV XVH WKLV QXPEHU WR DGYLVH SHRSOH DERXW ZKHWKHU RU QRW WKH\ DUH RYHUZHLJKW *LYHQ DQ LQGLYLGXDO V KHLJKW DQG ZHLJKW ZH FDQ FRPSXWH D %0, $ VLPSOH %0, SURJUDP WKHQ ZRXOG QDWXUDOO\ KDYH WKUHH YDULDEOHV IRU WKHVH WKUHH SLHFHV RI LQIRUPDWLRQ 7KHUH DUH VHYHUDO GHWDLOV WKDW ZH QHHG WR GLVFXVV DERXW YDULDEOHV EXW LW FDQ EH KHOSIXO WR ORRN DW D FRPSOHWH SURJUDP ILUVW WR VHH WKH RYHUDOO SLFWXUH 7KH IROORZLQJ LV D FRPSOHWH SURJUDP WKDW FRPSXWHV DQG SULQWV WKH %0, IRU DQ LQGLYLGXDO ZKR LV IRRW LQFKHV LQ KHLJKW DQG ZHLJKV SRXQGV 73 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class BMICalculator { public static void main(String args) { // declare variables double height; double weight; double bmi; // compute BMI height = 70; weight = 195; bmi = weight / (height * height) * 703; // print results System.out.println("Current BMI:"); System.out.println(bmi); } } 1RWLFH WKDW WKH SURJUDP LQFOXGHV EODQN OLQHV DQG FRPPHQWV WR LQGLFDWH ZKDW WKH GLIIHUHQW SDUWV RI WKH SURJUDP GR ,W SURGXFHV WKH IROORZLQJ RXWSXW Current BMI: 27.976530612244897 /HW V QRZ H[DPLQH WKH GHWDLOV RI WKLV SURJUDP WR XQGHUVWDQG KRZ YDULDEOHV ZRUN %HIRUH YDULDEOHV FDQ EH XVHG LQ D -DYD SURJUDP WKH\ PXVW EH GHFODUHG 7KH OLQH RI FRGH WKDW GHFODUHV WKH YDULDEOH LV NQRZQ DV D YDULDEOH GHFODUDWLRQ 'HFODUDWLRQ $ UHTXHVW WR VHW DVLGH D QHZ YDULDEOH ZLWK D JLYHQ QDPH DQG W\SH (DFK YDULDEOH LV GHFODUHG MXVW RQFH ,I \RX GHFODUH WKH YDULDEOH PRUH WKDQ RQFH \RX ZLOO JHW DQ HUURU PHVVDJH IURP WKH -DYD FRPSLOHU 6LPSOH YDULDEOH GHFODUDWLRQV DUH RI WKH IROORZLQJ IRUP <type> <name>; DV LQ WKH WKUHH GHFODUDWLRQV DW WKH EHJLQQLQJ RI RXU VDPSOH SURJUDP double height; double weight; double bmi; 1RWLFH WKDW YDULDEOH GHFODUDWLRQV HQG ZLWK D VHPLFRORQ MXVW OLNH VWDWHPHQWV GR 7KHVH GHFODUDWLRQV FDQ DSSHDU DQ\ZKHUH D VWDWHPHQW FDQ RFFXU :H DUH XVLQJ WKH NH\ZRUG GRXEOH WR GHILQH WKH W\SH RI WKHVH WKUHH YDULDEOHV 5HPHPEHU WKDW WKH QDPH RI HDFK SULPLWLYH W\SH LV D NH\ZRUG LQ -DYD LQW GRXEOH FKDU ERROHDQ 1RWLFH WKDW WKH GHFODUDWLRQ LQGLFDWHV WKH W\SH DQG WKH QDPH RI WKH YDULDEOH 2QFH D YDULDEOH LV GHFODUHG -DYD VHWV DVLGH D PHPRU\ ORFDWLRQ WR VWRUH LWV YDOXH 8VLQJ WKLV VLPSOH IRUP RI YDULDEOH GHFODUDWLRQ -DYD GRHV QRW VWRUH DQ LQLWLDO YDOXH LQ WKH PHPRU\ ORFDWLRQ 6R JLYHQ WKH WKUHH GHFODUDWLRQV DERYH -DYD ZRXOG DOORFDWH WKUHH PHPRU\ ORFDWLRQV IRU WKH YDULDEOHV DQG ZRXOG KDYH WKHP LQLWLDOO\ QRW VWRUH D YDOXH 74 +---+ height| ? | +---+ +---+ weight | ? | +---+ +---+ bmi | ? | +---+ :H UHIHU WR WKHVH DV XQLQLWLDOL]HG YDULDEOHV DQG WKH\ DUH VLPLODU WR EODQN FHOOV LQ D VSUHDGVKHHW 6R KRZ GR ZH JHW YDOXHV LQWR WKRVH FHOOV" 7KH HDVLHVW ZD\ WR GR VR LV XVLQJ DQ DVVLJQPHQW VWDWHPHQW 7KH JHQHUDO V\QWD[ RI WKH DVVLJQPHQW VWDWHPHQW LV <variable> = <expression>; DV LQ height = 70; 7KLV VWDWHPHQW VWRUHV WKH YDOXH LQ WKH PHPRU\ ORFDWLRQ IRU WKH YDULDEOH KHLJKW LQGLFDWLQJ WKDW WKLV SHUVRQ LV LQFKHV WDOO IRRW LQFKHV :H RIWHQ XVH WKH SKUDVH JHWV RU LV DVVLJQHG ZKHQ UHDGLQJ D VWDWHPHQW OLNH WKLV DV LQ KHLJKW JHWV RU KHLJKW LV DVVLJQHG :KHQ WKH VWDWHPHQW H[HFXWHV WKH FRPSXWHU ILUVW HYDOXDWHV WKH H[SUHVVLRQ RQ WKH ULJKW VLGH WKHQ LW VWRUHV WKH UHVXOW LQ WKH PHPRU\ ORFDWLRQ IRU WKH JLYHQ YDULDEOH ,Q WKLV FDVH WKH H[SUHVVLRQ LV MXVW D VLPSOH OLWHUDO YDOXH VR DIWHU H[HFXWLQJ WKLV VWDWHPHQW PHPRU\ ZRXOG ORRN OLNH WKLV +------+ height | 70.0 | +------+ +---+ weight | ? | +---+ +---+ bmi | ? | +---+ 1RWLFH WKDW WKH YDOXH LV VWRUHG DV EHFDXVH WKH YDULDEOH LV RI W\SH GRXEOH 7KH YDULDEOH KHLJKW KDV QRZ EHHQ LQLWLDOL]HG EXW WKH YDULDEOHV ZHLJKW DQG EPL DUH VWLOO XQLQLWLDOL]HG 7KH VHFRQG DVVLJQPHQW VWDWHPHQW JLYHV D YDOXH WR ZHLJKW weight = 195; $IWHU H[HFXWLQJ WKLV VWDWHPHQW PHPRU\ ORRNV OLNH WKLV +------+ height | 70.0 | +------+ +-------+ weight | 195.0 | +-------+ +---+ bmi | ? | +---+ 7KH WKLUG DVVLJQPHQW VWDWHPHQW LQFOXGHV D IRUPXOD DQ H[SUHVVLRQ WR EH HYDOXDWHG bmi = weight / (height * height) * 703; 7R FDOFXODWH WKH YDOXH RI WKLV H[SUHVVLRQ WKH FRPSXWHU WDNHV WKH ZHLJKW DQG GLYLGHV E\ WKH VTXDUH RI WKH KHLJKW DQG WKHQ PXOWLSOLHV E\ WKH OLWHUDO YDOXH 7KH UHVXOW LV VWRUHG LQ WKH YDULDEOH EPL 6R PHPRU\ ORRNV OLNH WKLV DIWHU WKH WKLUG DVVLJQPHQW VWDWHPHQW QRW DOO RI WKH GLJLWV RI EPL DUH LQFOXGHG KHUH +------+ height | 70.0 | +------+ +-------+ weight | 195.0 | +-------+ +--------+ bmi | 27.976 | +--------+ 7KH ODVW WZR OLQHV RI WKH SURJUDP UHSRUW WKH %0, UHVXOW XVLQJ SULQWOQ VWDWHPHQWV 75 System.out.println("Current BMI:"); System.out.println(bmi); 1RWLFH WKDW ZH FDQ LQFOXGH D YDULDEOH LQ D SULQWOQ VWDWHPHQW WKH VDPH ZD\ WKDW ZH LQFOXGHG OLWHUDO YDOXHV DQG RWKHU H[SUHVVLRQV WR EH SULQWHG $V LWV QDPH LPSOLHV D YDULDEOH FDQ WDNH RQ GLIIHUHQW YDOXHV DW GLIIHUHQW WLPHV )RU H[DPSOH FRQVLGHU WKH IROORZLQJ YDULDWLRQ RI WKH %0, SURJUDP WKDW FRPSXWHV D QHZ %0, DVVXPLQJ WKH SHUVRQ ORVW SRXQGV JRLQJ IURP SRXQGV WR SRXQGV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class BMICalculator2 { public static void main(String args) { // declare variables double height; double weight; double bmi; // compute BMI height = 70; weight = 195; bmi = weight / (height * height) * 703; // print results System.out.println("Current BMI:"); System.out.println(bmi); // recompute BMI weight = 180; bmi = weight / (height * height) * 703; // report new results System.out.println("Target BMI:"); System.out.println(bmi); } } 7KH SURJUDP EHJLQV WKH VDPH ZD\ VHWWLQJ WKH WKUHH YDULDEOHV WR WKH IROORZLQJ YDOXHV DQG UHSRUWLQJ WKLV LQLWLDO YDOXH IRU %0, +------+ height | 70.0 | +------+ +-------+ weight | 195.0 | +-------+ +--------+ bmi | 27.976 | +--------+ %XW WKH QHZ SURJUDP WKHQ LQFOXGHV WKH IROORZLQJ DVVLJQPHQW VWDWHPHQW weight = 180; 7KLV FKDQJHV WKH YDOXH RI WKH YDULDEOH FDOOHG ZHLJKW +------+ height | 70.0 | +------+ +-------+ weight | 180.0 | +-------+ +--------+ bmi | 27.976 | +--------+ <RX PLJKW WKLQN WKDW WKLV ZRXOG DOVR FKDQJH WKH YDOXH RI WKH YDULDEOH EPL $IWHU DOO HDUOLHU LQ WKH SURJUDP ZH VDLG WKDW WKH IROORZLQJ VKRXOG EH WUXH 76 bmi = weight / (height * height) * 703; %XW HYHQ WKRXJK -DYD XVHV WKH HTXDOV VLJQ IRU DVVLJQPHQW GRQ W FRQIXVH WKLV ZLWK D VWDWHPHQW RI HTXDOLW\ 7KH DVVLJQPHQW VWDWHPHQW GRHV QRW UHSUHVHQW DQ DOJHEUDLF UHODWLRQVKLS ,Q DOJHEUD \RX PLJKW VD\ x = y + 2 ,Q PDWKHPDWLFV \RX VWDWH GHILQLWLYHO\ WKDW [ LV HTXDO WR \ SOXV D IDFW WKDW LV WUXH QRZ DQG IRUHYHU ,I [ FKDQJHV \ ZLOO FKDQJH DFFRUGLQJO\ -DYD V DVVLJQPHQW VWDWHPHQW LV YHU\ GLIIHUHQW 7KH DVVLJQPHQW VWDWHPHQW LV D FRPPDQG WR SHUIRUP DQ DFWLRQ DW D SDUWLFXODU SRLQW LQ WLPH ,W GRHV QRW UHSUHVHQW D ODVWLQJ UHODWLRQVKLS EHWZHHQ YDULDEOHV 7KDW V ZK\ ZH XVXDOO\ XVH VD\ JHWV RU LV DVVLJQHG UDWKHU WKDQ VD\LQJ HTXDOV ZKHQ ZH UHDG DVVLJQPHQW VWDWHPHQWV *HWWLQJ EDFN WR RXU SURJUDP UHVHWWLQJ WKH YDULDEOH FDOOHG ZHLJKW GRHV QRW UHVHW WKH YDULDEOH FDOOHG EPL 7KDW V ZK\ WKH QHZ SURJUDP UHVHWV WKH ZHLJKW DQG UHFRPSXWHV WKH EPL VR WKDW WKH\ HDFK JHW D QHZ YDOXH weight = 180; bmi = weight / (height * height) * 703; :LWKRXW WKH VHFRQG DVVLJQPHQW VWDWHPHQW DERYH WKH YDULDEOH EPL ZRXOG VWRUH WKH VDPH YDOXH DV EHIRUH 7KDW ZRXOG EH D UDWKHU GHSUHVVLQJ RXWFRPH WR UHSRUW WR WKH SHUVRQ WKDW ORVLQJ SRXQGV ZLOO KDYH QR HIIHFW RQ WKHLU %0, :LWK WKH FRPPDQG WR UHFRPSXWH ZH UHVHW ERWK WKH ZHLJKW DQG EPL YDULDEOHV VR WKDW PHPRU\ ORRNV OLNH WKLV DJDLQ ZH DUH VKRZLQJ RQO\ WKH OHDGLQJ GLJLWV RI EPL +------+ height | 70.0 | +------+ +-------+ weight | 180.0 | +-------+ +--------+ bmi | 25.824 | +--------+ 7KH RXWSXW RI WKH QHZ YHUVLRQ RI WKH SURJUDP LV Current BMI: 27.976530612244897 Target BMI: 25.82448979591837 2QH YHU\ FRPPRQ DVVLJQPHQW VWDWHPHQW WKDW SRLQWV RXW WKH GLIIHUHQFH EHWZHHQ DOJHEUDLF UHODWLRQVKLSV DQG SURJUDP VWDWHPHQWV LV x = x + 1; 5HPHPEHU QRW WR WKLQN RI WKLV DV [ HTXDOV [ 7KHUH DUH QR QXPEHUV WKDW VDWLVI\ WKDW HTXDWLRQ :H XVH D ZRUG OLNH JHWV WR UHDG WKLV DV [ JHWV WKH YDOXH RI [ SOXV RQH 7KLV PD\ VHHP D UDWKHU RGG VWDWHPHQW EXW \RX VKRXOG EH DEOH WR GHFLSKHU LW JLYHQ WKH UXOHV RXWOLQHG DERYH 6XSSRVH WKDW WKH FXUUHQW YDOXH RI [ LV 7R H[HFXWH WKH VWDWHPHQW \RX ILUVW HYDOXDWH WKH H[SUHVVLRQ WR REWDLQ WKH UHVXOW 7KH FRPSXWHU VWRUHV WKLV YDOXH LQ WKH YDULDEOH QDPHG RQ WKH OHIW LQ YDULDEOH [ 7KXV WKLV VWDWHPHQW DGGV RQH WR WKH YDOXH RI WKH YDULDEOH :H UHIHU WR WKLV DV LQFUHPHQWLQJ WKH YDOXH RI [ ,W LV D IXQGDPHQWDO SURJUDPPLQJ RSHUDWLRQ EHFDXVH LW LV WKH SURJUDPPLQJ HTXLYDOHQW RI FRXQWLQJ DQG VR RQ 7KH IROORZLQJ VWDWHPHQW LV D YDULDWLRQ WKDW FRXQWV GRZQ ZKLFK ZH FDOO GHFUHPHQWLQJ D YDULDEOH 77 x = x - 1; $VVLJQPHQW 'HFODUDWLRQ 9DULDWLRQV ,Q WKH ODVW VHFWLRQ ZH VDZ WKH VLPSOHVW IRUP RI YDULDEOH GHFODUDWLRQ DQG DVVLJQPHQW EXW -DYD LV D FRPSOH[ ODQJXDJH WKDW SURYLGHV D ORW RI IOH[LELOLW\ WR SURJUDPPHUV ,W ZRXOGQ W EH D EDG LGHD WR VWLFN ZLWK WKH VLPSOHVW IRUP ZKLOH \RX DUH OHDUQLQJ EXW \RX OO FRPH DFURVV WKHVH RWKHU IRUPV DV \RX UHDG RWKHU SHRSOH V SURJUDPV VR \RX OO ZDQW WR XQGHUVWDQG ZKDW WKH\ PHDQ 7KH ILUVW YDULDWLRQ LV WKDW -DYD DOORZV \RX WR SURYLGH DQ LQLWLDO YDOXH WR D YDULDEOH DW WKH WLPH WKDW \RX GHFODUH LW 7KH V\QWD[ LV DV IROORZV <type> <name> = <expression>; DV LQ double height = 70; double weight = 195; 7KH VWDWHPHQWV DERYH KDYH WKH VDPH HIIHFW DV KDYLQJ WZR GHFODUDWLRQV IROORZHG E\ WZR DVVLJQPHQW VWDWHPHQWV double double height weight height; weight; = 70; = 195; 6R WKLV YDULDWLRQ FRPELQHV GHFODUDWLRQ DQG DVVLJQPHQW LQ RQH OLQH RI FRGH $QRWKHU YDULDWLRQ LV WR GHFODUH VHYHUDO YDULDEOHV DOO RI WKH VDPH W\SH 7KH V\QWD[ LV DV IROORZV <type> <name>, <name>, <name>, ..., <name>; DV LQ double height, weight; 1RWLFH WKDW WKH W\SH DSSHDUV MXVW RQFH DW WKH EHJLQQLQJ RI WKH GHFODUDWLRQ 6R WKH H[DPSOH DERYH GHFODUHV WZR GLIIHUHQW YDULDEOHV ERWK RI W\SH GRXEOH 7KH ILQDO YDULDWLRQ LV D PL[WXUH RI WKH SUHYLRXV WZR <RX FDQ GHFODUH PXOWLSOH YDULDEOHV DOO RI WKH VDPH W\SH DQG \RX FDQ LQLWLDOL]H WKHP DW WKH VDPH WLPH )RU H[DPSOH \RX FRXOG VD\ double height = 70, weight = 195; 7KLV QRW RQO\ GHFODUHV WKH WZR GRXEOH YDULDEOHV KHLJKW DQG ZHLJKW LW DOVR JLYHV WKHP LQLWLDO YDOXHV DQG UHVSHFWLYHO\ -DYD HYHQ DOORZV \RX WR PL[ LQLWLDOL]LQJ DQG QRW LQLWLDOL]LQJ DV LQ double height = 70, weight = 195, bmi; 7KLV GHFODUHV WKUHH GRXEOH YDULDEOHV FDOOHG KHLJKW ZHLJKW DQG EPL DQG SURYLGHV DQ LQLWLDO YDOXH WR WZR RI WKHP KHLJKW DQG ZHLJKW 7KH YDULDEOH EPL ZRXOG EH XQLQLWLDOL]HG E\ WKLV GHFODUDWLRQ 78 &RPPRQ 3URJUDPPLQJ (UURU $FFLGHQWDOO\ 'HFODULQJ D 9DULDEOH 7ZLFH 2QH RI WKH WKLQJV WR NHHS LQ PLQG DV \RX OHDUQ LV WKDW \RX GHFODUH DQ\ JLYHQ YDULDEOH MXVW RQFH <RX FDQ DVVLJQ LW DV PDQ\ WLPHV DV \RX OLNH RQFH \RX YH GHFODUHG LW EXW WKH GHFODUDWLRQ DSSHDUV MXVW RQFH 7KLQN RI YDULDEOH GHFODUDWLRQ DV EHLQJ OLNH FKHFNLQJ LQWR D KRWHO DQG DVVLJQPHQW DV EHLQJ OLNH JRLQJ LQ DQG RXW RI \RXU URRP <RX KDYH WR FKHFN LQ ILUVW WR JHW \RXU URRP NH\ EXW WKHQ \RX FDQ FRPH DQG JR DV RIWHQ DV \RX OLNH ,I \RX WULHG WR FKHFN LQ D VHFRQG WLPH WKH KRWHO ZRXOG EH OLNHO\ WR DVN \RX LI \RX UHDOO\ ZDQW WR SD\ IRU D VHFRQG URRP ,I -DYD VHHV \RX GHFODULQJ D YDULDEOH PRUH WKDQ RQFH LW JHQHUDWHV D FRPSLOHU HUURU )RU H[DPSOH LI \RX VD\ int x = 13; System.out.println(x); int x = 2; System.out.println(x); // This line does not compile. 7KH ILUVW OLQH LV RND\ ,W GHFODUHV DQ LQWHJHU YDULDEOH FDOOHG [ DQG LQLWLDOL]HV LW WR 7KH VHFRQG OLQH LV RND\ EHFDXVH LW VLPSO\ SULQWV WKH YDOXH RI [ %XW WKH WKLUG OLQH ZLOO JHQHUDWH DQ HUURU PHVVDJH WKDW [ LV DOUHDG\ GHILQHG ,I \RX ZDQW WR FKDQJH WKH YDOXH RI [ WKHQ \RX QHHG WR XVH D VLPSOH DVVLJQPHQW VWDWHPHQW LQVWHDG RI D YDULDEOH GHFODUDWLRQ int x = 13; System.out.println(x); x = 2; System.out.println(x); :H KDYH EHHQ UHIHUULQJ WR WKH DVVLJQPHQW VWDWHPHQW EXW LQ IDFW DVVLJQPHQW LV DQ RSHUDWRU QRW D VWDWHPHQW :KHQ \RX DVVLJQ D YDOXH WR D YDULDEOH WKH RYHUDOO H[SUHVVLRQ HYDOXDWHV WR WKH YDOXH MXVW DVVLJQHG 7KDW PHDQV WKDW \RX FDQ IRUP H[SUHVVLRQV WKDW KDYH DVVLJQPHQW RSHUDWRUV HPEHGGHG ZLWKLQ WKHP 8QOLNH PRVW RWKHU RSHUDWRUV WKH DVVLJQPHQW RSHUDWRU HYDOXDWHV IURP ULJKW WR OHIW ZKLFK DOORZV SURJUDPPHUV WR ZULWH VWDWHPHQWV OLNH WKH IROORZLQJ int x, y, z; x = y = z = 2 * 5 + 4; %HFDXVH WKH DVVLJQPHQW RSHUDWRU HYDOXDWHV ULJKW WR OHIW WKLV VWDWHPHQW LV HTXLYDOHQW WR x = (y = (z = 2 * 5 + 4)); 7KH H[SUHVVLRQ 2 * 5 + 4 HYDOXDWHV WR 7KLV YDOXH LV DVVLJQHG WR ] %XW WKDW LQ WXUQ HYDOXDWHV WR ZKLFK LV WKHQ XVHG WR DVVLJQ D YDOXH WR \ $QG WKDW DVVLJQPHQW HYDOXDWHV WR DV ZHOO ZKLFK LV XVHG WR DVVLJQ D YDOXH WR [ 6R DOO WKUHH YDULDEOHV ZLOO EH DVVLJQHG WKH YDOXH DV D UHVXOW RI WKLV VWDWHPHQW :KLOH \RX FDQ GR DVVLJQPHQWV OLNH WKHVH LW V QRW FOHDU WKDW LW LV ZLVH WR GR VR 7KH FKDLQHG DVVLJQPHQW VWDWHPHQW DERYH LV QRW WKDW GLIILFXOW WR UHDG EXW WU\ WR ILJXUH RXW ZKDW WKH IROORZLQJ VWDWHPHQW GRHV x = 3 * (y = 2 + 2) / (z = 2); ,W V HDVLHU WR VHH ZKDW LV JRLQJ RQ ZKHQ \RX ZULWH WKH FRGH DERYH DV WKUHH VHSDUDWH VWDWHPHQWV 79 y = 2 + 2; z = 2; x = 3 * y / z; 6WULQJ &RQFDWHQDWLRQ <RX VDZ LQ FKDSWHU WKDW \RX FDQ RXWSXW VWULQJ OLWHUDOV XVLQJ 6\VWHP RXW SULQWOQ <RX FDQ DOVR RXWSXW QXPHULF H[SUHVVLRQV XVLQJ 6\VWHP RXW SULQWOQ System.out.println(12 + 3 - 1); 7KLV VWDWHPHQW FDXVHV WKH FRPSXWHU WR ILUVW HYDOXDWH WKH H[SUHVVLRQ ZKLFK \LHOGV WKH YDOXH DQG WKHQ WR ZULWH WR WKH FRQVROH ZLQGRZ 2IWHQ \RX ZDQW WR RXWSXW PRUH WKDQ RQH YDOXH RQ D OLQH 8QIRUWXQDWHO\ \RX FDQ RQO\ SDVV RQH YDOXH WR SULQWOQ 7R JHW DURXQG WKLV OLPLWDWLRQ -DYD SURYLGHV D VLPSOH PHFKDQLVP FDOOHG FRQFDWHQDWLRQ IRU SXWWLQJ VHYHUDO SLHFHV WRJHWKHU LQWR RQH ORQJ VWULQJ OLWHUDO 6WULQJ &RQFDWHQDWLRQ &RPELQLQJ VHYHUDO VWULQJV LQWR D VLQJOH VWULQJ RU FRPELQLQJ D VWULQJ ZLWK RWKHU GDWD LQWR D QHZ ORQJHU VWULQJ 7KH SOXV RSHUDWRU FRQFDWHQDWHV WKH SLHFHV WRJHWKHU 'RLQJ VR IRUPV DQ H[SUHVVLRQ WKDW FDQ EH HYDOXDWHG (YHQ WKRXJK VXFK H[SUHVVLRQV LQFOXGH ERWK QXPEHUV DQG WH[W WKH\ FDQ EH HYDOXDWHG MXVW OLNH WKH QXPHULF H[SUHVVLRQV ZH DUH H[SORULQJ &RQVLGHU IRU H[DPSOH WKH IROORZLQJ "I have " + 3 + " things to concatenate" <RX KDYH WR SD\ FORVH DWWHQWLRQ WR WKH TXRWDWLRQ PDUNV LQ DQ H[SUHVVLRQ OLNH WKLV WR NHHS WUDFN RI ZKLFK SDUWV DUH LQVLGH D VWULQJ OLWHUDO DQG ZKLFK DUH RXWVLGH 7KLV H[SUHVVLRQ EHJLQV ZLWK WKH WH[W , KDYH LQFOXGLQJ D VSDFH DW WKH HQG 7KHQ ZH VHH D SOXV VLJQ DQG WKH LQWHJHU OLWHUDO -DYD FRQYHUWV WKH LQWHJHU LQWR D WH[WXDO IRUP DQG FRQFDWHQDWHV WKH WZR SLHFHV WRJHWKHU WR IRUP , KDYH )ROORZLQJ WKH LV DQRWKHU SOXV DQG DQRWKHU VWULQJ OLWHUDO WKLQJV WR FRQFDWHQDWH ZKLFK VWDUWV ZLWK D VSDFH 7KLV SLHFH LV JOXHG RQWR WKH HQG RI WKH RWKHU VWULQJ WR IRUP WKH VWULQJ , KDYH WKLQJV WR FRQFDWHQDWH %HFDXVH WKLV H[SUHVVLRQ SURGXFHV D VLQJOH FRQFDWHQDWHG VWULQJ ZH FDQ LQFOXGH LW LQ D SULQWOQ VWDWHPHQW System.out.println("I have " + 3 + " things to concatenate"); ZKLFK ZRXOG SURGXFH D VLQJOH OLQH RI RXWSXW I have 3 things to concatenate :H RIWHQ XVH VWULQJ FRQFDWHQDWLRQ WR UHSRUW WKH YDOXH RI D YDULDEOH &RQVLGHU IRU H[DPSOH WKH IROORZLQJ SURJUDP WKDW FRPSXWHV WKH QXPEHU RI KRXUV PLQXWHV DQG VHFRQGV LQ D VWDQGDUG \HDU 80 1 2 3 4 5 6 7 8 9 10 public class Time { public static void main(String args) { int hours = 365 * 24; int minutes = hours * 60; int seconds = minutes * 60; System.out.println("Total hours in a year = " + hours); System.out.println("Total minutes in a year = " + minutes); System.out.println("Total seconds in a year = " + seconds); } } 1RWLFH WKDW WKH WKUHH SULQWOQ FRPPDQGV DW WKH HQG HDFK KDYH D VWULQJ OLWHUDO FRQFDWHQDWHG ZLWK D YDULDEOH 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW Total hours in a year = 8760 Total minutes in a year = 525600 Total seconds in a year = 31536000 <RX FDQ XVH FRQFDWHQDWLRQ WR IRUP DUELWUDULO\ FRPSOH[ H[SUHVVLRQV )RU H[DPSOH LI \RX KDG YDULDEOHV [ \ DQG ] DQG \RX ZDQWHG WR ZULWH WKHLU YDOXHV RXW LQ FRRUGLQDWH IRUPDW ZLWK SDUHQWKHVHV DQG FRPPDV \RX FRXOG VD\ System.out.println("(" + x + ", " + y + ", " + z + ")"); ,I [ \ DQG ] KDYH WKH YDOXHV DQG UHVSHFWLYHO\ WKHQ WKLV VWDWHPHQW ZRXOG RXWSXW WKH VWULQJ 7KH SOXV XVHG IRU FRQFDWHQDWLRQ KDV WKH VDPH OHYHO RI SUHFHGHQFH DV WKH QRUPDO DULWKPHWLF SOXV RSHUDWRU ZKLFK FDQ OHDG WR VRPH FRQIXVLRQ &RQVLGHU IRU H[DPSOH WKH IROORZLQJ H[SUHVVLRQ 2 + 3 + " hello " + 7 + 2 * 3 7KLV H[SUHVVLRQ KDV IRXU SOXV RSHUDWRUV DQG RQH PXOWLSOLFDWLRQ RSHUDWRU %HFDXVH RI SUHFHGHQFH ZH HYDOXDWH WKH PXOWLSOLFDWLRQ ILUVW 2 + 3 + " hello " + 7 + 2 * 3 \---/ 2 + 3 + " hello " + 7 + 6 7KLV JURXSLQJ PLJKW VHHP RGG EXW WKDW V ZKDW WKH SUHFHGHQFH UXOH VD\V WR GR :H GRQ W HYDOXDWH DQ\ DGGLWLRQ RSHUDWRUV XQWLO ZH YH ILUVW HYDOXDWHG DOO RI WKH PXOWLSOLFDWLYH RSHUDWRUV 6R RQFH ZH YH WDNHQ FDUH RI WKH PXOWLSOLFDWLRQ ZH UH OHIW ZLWK WKH IRXU DGGLWLRQ RSHUDWRUV 7KHVH ZLOO EH HYDOXDWHG IURP OHIW WR ULJKW ,Q HYDOXDWLQJ WKH ILUVW DGGLWLRQ ZH ILQG RXUVHOYHV DVNHG WR DGG WRJHWKHU WZR LQWHJHU YDOXHV 7KH RYHUDOO H[SUHVVLRQ LQYROYHV D VWULQJ EXW WKLV OLWWOH VXEH[SUHVVLRQ KDV MXVW WZR LQWHJHUV $V D UHVXOW ZH SHUIRUP LQWHJHU DGGLWLRQ 2 + 3 + " hello " + 7 + \---/ 5 + " hello " + 7 + 6 6 81 7KH QH[W DGGLWLRQ LQYROYHV DGGLQJ WKH LQWHJHU WR WKH VWULQJ OLWHUDO KHOOR ,I HLWKHU RI WKH WZR RSHUDQGV LV D VWULQJ WKHQ ZH SHUIRUP FRQFDWHQDWLRQ 6R LQ WKLV FDVH ZH FRQYHUW WKH LQWHJHU LQWR D WH[W HTXLYDOHQW DQG JOXH WKH SLHFHV WRJHWKHU WR IRUP D QHZ VWULQJ YDOXH 5 + " hello " + 7 + \-------------/ "5 hello " + 7 + 6 6 <RX PLJKW WKLQN WKDW -DYD ZRXOG DGG WRJHWKHU WKH DQG WKH ZD\ ZH GLG ZLWK WKH DQG WKDW ZHUH DGGHG WRJHWKHU WR PDNH %XW LW GRHVQ W ZRUN WKDW ZD\ 7KH UXOHV RI SUHFHGHQFH DUH VLPSOH DQG -DYD IROORZV WKHP ZLWK VLPSOH PLQGHG FRQVLVWHQF\ 3UHFHGHQFH WHOOV XV WKDW DGGLWLRQ RSHUDWRUV DUH HYDOXDWHG OHIW WR ULJKW 6R ILUVW ZH DGG WKH VWULQJ KHOOR WR 7KDW LV DQRWKHU FRPELQDWLRQ RI D VWULQJ DQG DQ LQWHJHU VR -DYD FRQYHUWV WKH LQWHJHU WR LWV WH[WXDO HTXLYDOHQW DQG FRQFDWHQDWHV WKH WZR SDUWV WRJHWKHU WR IRUP D QHZ VWULQJ "5 hello " + 7 + \---------------/ "5 hello 7" + 6 6 1RZ WKHUH LV MXVW D VLQJOH DGGLWLRQ WR SHUIRUP ZKLFK DJDLQ LQYROYHV D VWULQJ LQW FRPELQDWLRQ :H FRQYHUW WKH LQWHJHU WR LWV WH[WXDO HTXLYDOHQW DQG FRQFDWHQDWH WKH WZR SDUWV WRJHWKHU WR IRUP D QHZ VWULQJ "5 hello 7" + 6 \------------------/ "5 hello 76" &OHDUO\ VXFK H[SUHVVLRQV FDQ EH FRQIXVLQJ EXW \RX ZRXOGQ W ZDQW WKH -DYD FRPSLOHU WR JXHVV ZKDW \RX PHDQ 2XU MRE DV SURJUDPPHUV LV HDVLHU LI ZH NQRZ WKDW WKH FRPSLOHU LV JRLQJ WR IROORZ VLPSOH UXOHV FRQVLVWHQWO\ <RX FDQ DYRLG PXFK RI WKLV FRQIXVLRQ E\ DGGLQJ SDUHQWKHVHV WR WKH RULJLQDO H[SUHVVLRQ )RU H[DPSOH LI ZH UHDOO\ GLG ZDQW -DYD WR DGG WRJHWKHU WKH DQG LQVWHDG RI FRQFDWHQDWLQJ WKHP VHSDUDWHO\ ZH FRXOG KDYH ZULWWHQ WKH RULJLQDO H[SUHVVLRQ LQ D PXFK FOHDUHU ZD\ DV (2 + 3) + " hello " + (7 + 2 * 3) %HFDXVH RI WKH SDUHQWKHVHV -DYD ZLOO HYDOXDWH WKH WZR QXPHULF SDUWV RI WKLV H[SUHVVLRQ ILUVW DQG WKHQ FRQFDWHQDWH WKH UHVXOWV ZLWK WKH VWULQJ LQ WKH PLGGOH 7KLV H[SUHVVLRQ HYDOXDWHV WR KHOOR ,QFUHPHQW 'HFUHPHQW 2SHUDWRUV ,Q DGGLWLRQ WR WKH VWDQGDUG DVVLJQPHQW RSHUDWRU -DYD KDV VHYHUDO VSHFLDO RSHUDWRUV WKDW DUH XVHIXO IRU D SDUWLFXODU IDPLO\ RI RSHUDWLRQV WKDW DUH FRPPRQ LQ SURJUDPPLQJ <RX RIWHQ ILQG \RXUVHOI LQFUHDVLQJ D YDULDEOH E\ D SDUWLFXODU DPRXQW ZKLFK ZH FDOO LQFUHPHQWLQJ <RX DOVR RIWHQ ILQG \RXUVHOI GHFUHDVLQJ D YDULDEOH E\ D SDUWLFXODU DPRXQW ZKLFK ZH FDOO GHFUHPHQWLQJ 7R DFFRPSOLVK WKLV \RX ZULWH VWDWHPHQWV OLNH WKH IROORZLQJ x = x + 1; y = y - 1; z = z + 2; 82 <RX DOVR RIWHQ ILQG \RXUVHOI ZDQWLQJ WR GRXEOH RU WULSOH D YDULDEOH RU WR UHGXFH LWV YDOXH E\ D IDFWRU RI LQ ZKLFK FDVH \RX PLJKW ZULWH FRGH OLNH WKH IROORZLQJ x = x * 2; y = y * 3; z = z / 2; -DYD KDV D VKRUWKDQG IRU WKHVH VLWXDWLRQV <RX JOXH WRJHWKHU WKH RSHUDWRU FKDUDFWHU HWF ZLWK WKH HTXDOV WR JHW D VSHFLDO DVVLJQPHQW RSHUDWRU HWF 7KLV YDULDWLRQ DOORZV \RX WR UHZULWH DVVLJQPHQWV VWDWHPHQWV OLNH WKH RQHV DERYH DV IROORZV x += 1; y -= 1; z += 2; x *= 2; y *= 3; z /= 2; 7KLV FRQYHQWLRQ LV \HW DQRWKHU GHWDLO WR OHDUQ DERXW -DYD EXW WKH FRGH FDQ EH FOHDUHU WR UHDG 7KLQN RI D VWDWHPHQW OLNH x += 2 DV VD\LQJ DGG WR [ 7KDW V PRUH FRQFLVH WKDQ VD\LQJ x = x + 2 -DYD KDV DQ HYHQ PRUH FRQFLVH ZD\ RI H[SUHVVLQJ WKLV IRU WKH SDUWLFXODU FDVH ZKHUH \RX ZDQW WR LQFUHPHQW E\ RU GHFUHPHQW E\ LQ ZKLFK FDVH \RX FDQ XVH WKH LQFUHPHQW DQG GHFUHPHQW RSHUDWRUV ++ DQG -- )RU H[DPSOH \RX FDQ VD\ x++; y--; 7KHUH DUH DFWXDOO\ WZR GLIIHUHQW IRUPV RI HDFK RI WKHVH EHFDXVH \RX DUH DOVR DOORZHG WR SXW WKH RSHUDWRU LQ IURQW RI WKH YDULDEOH ++x; --y; 7KH WZR YHUVLRQV RI ++ DUH NQRZQ DV WKH SUH LQFUHPHQW [ DQG SRVW LQFUHPHQW [ RSHUDWRUV 7KH WZR YHUVLRQV RI -- DUH VLPLODUO\ NQRZQ DV WKH SUH GHFUHPHQW [ DQG SRVW GHFUHPHQW [ RSHUDWRUV 7KH SUH YHUVXV SRVW GLVWLQFWLRQ GRHVQ W PDWWHU ZKHQ \RX LQFOXGH WKHP DV VWDWHPHQWV E\ WKHPVHOYHV DV LQ WKHVH WZR H[DPSOHV 7KH GLIIHUHQFH FRPHV XS RQO\ ZKHQ \RX HPEHG WKHVH LQVLGH RI PRUH FRPSOH[ H[SUHVVLRQV ZKLFK ZH GRQ W UHFRPPHQG 1RZ WKDW ZH YH VHHQ D QXPEHU RI QHZ RSHUDWRUV LW LV ZRUWK UHYLVLWLQJ WKH LVVXH RI SUHFHGHQFH +HUH LV DQ XSGDWHG SUHFHGHQFH WDEOH LQFOXGLQJ WKH DVVLJQPHQW RSHUDWRUV DQG WKH LQFUHPHQW DQG GHFUHPHQW RSHUDWRUV -DYD 2SHUDWRU 3UHFHGHQFH 'HVFULSWLRQ 2SHUDWRUV ++, --, +, XQDU\ RSHUDWRUV *, /, % PXOWLSOLFDWLYH RSHUDWRUV +, DGGLWLYH RSHUDWRUV DVVLJQPHQW RSHUDWRUV =, +=, -=, *=, /=, %= 83 'LG \RX .QRZ DQG 7KH ++ DQG -- RSHUDWRUV ZHUH ILUVW LQWURGXFHG LQ WKH & SURJUDPPLQJ ODQJXDJH -DYD KDV WKHP EHFDXVH WKH GHVLJQHUV RI WKH ODQJXDJH GHFLGHG WR XVH WKH V\QWD[ RI & DV WKH EDVLV IRU -DYD V\QWD[ 0DQ\ ODQJXDJHV KDYH PDGH WKH VDPH FKRLFH LQFOXGLQJ & DQG & 7KHUH LV DOPRVW D VHQVH RI SULGH DPRQJ & SURJUDPPHUV WKDW WKHVH RSHUDWRUV DOORZ \RX WR ZULWH H[WUHPHO\ FRQFLVH FRGH 0DQ\ RWKHU SHRSOH IHHO WKDW WKH\ FDQ PDNH FRGH XQQHFHVVDULO\ FRPSOH[ ,Q WKLV ERRN ZH DOZD\V XVH WKHVH RSHUDWRUV DV D VHSDUDWH VWDWHPHQW VR WKDW LW LV REYLRXV ZKDW LV JRLQJ RQ EXW VRPH SHRSOH OLNH WR NQRZ DOO RI WKH GHWDLOV VR KHUH WKH\ DUH 7KH SUH DQG SRVW YDULDWLRQV ERWK KDYH WKH VDPH RYHUDOO HIIHFW 7KH WZR LQFUHPHQW RSHUDWRUV LQFUHPHQW D YDULDEOH 7KH WZR GHFUHPHQW RSHUDWRUV GHFUHPHQW D YDULDEOH %XW WKH\ GLIIHU LQ WHUPV RI ZKDW WKH\ HYDOXDWH WR :KHQ \RX LQFUHPHQW RU GHFUHPHQW WKHUH DUH UHDOO\ WZR YDOXHV LQYROYHG 7KHUH LV WKH RULJLQDO YDOXH WKDW WKH YDULDEOH KDG EHIRUH WKH LQFUHPHQW RU GHFUHPHQW DQG WKHUH LV WKH ILQDO YDOXH WKDW WKH YDULDEOH KDV DIWHU WKH LQFUHPHQW RU GHFUHPHQW 7KH SRVW YHUVLRQV HYDOXDWH WR WKH RULJLQDO ROGHU YDOXH DQG WKH SUH YHUVLRQV HYDOXDWH WR WKH ILQDO ODWHU YDOXH &RQVLGHU IRU H[DPSOH WKH IROORZLQJ FRGH IUDJPHQW int x = 10; int y = 20; int z = ++x * y--; :KDW YDOXH LV ] DVVLJQHG" 7KH DQVZHU LV 7KH WKLUG DVVLJQPHQW LQFUHPHQWV [ WR DQG GHFUHPHQWV \ WR EXW LQ FRPSXWLQJ WKH YDOXH RI ] LW XVHV WKH QHZ YDOXH RI [ [ WLPHV WKH ROG YDOXH RI \ \ ZKLFK LV WLPHV RU 7KHUH LV D VLPSOH PQHPRQLF WR UHPHPEHU WKLV :KHQ \RX VHH x++ UHDG LW DV JLYH PH [ WKHQ LQFUHPHQW DQG UHDG ++x DV LQFUHPHQW WKHQ JLYH PH [ +HUH V DQRWKHU PHPRU\ GHYLFH WKDW PLJKW KHOS -XVW UHPHPEHU WKDW & LV D EDG QDPH IRU D SURJUDPPLQJ ODQJXDJH 7KH H[SUHVVLRQ & ZRXOG EH LQWHUSUHWHG DV (YDOXDWH WR WKH ROG YDOXH RI & DQG WKHQ LQFUHPHQW & ,Q RWKHU ZRUGV HYHQ WKRXJK \RX UH WU\LQJ WR FRPH XS ZLWK VRPHWKLQJ QHZ DQG GLIIHUHQW \RX UH UHDOO\ VWXFN ZLWK WKH ROG DZIXO ODQJXDJH 7KH ODQJXDJH \RX ZDQW LV & EHFDXVH WKHQ \RX G LPSURYH WKH ODQJXDJH DQG \RX G JHW WR ZRUN ZLWK WKH QHZ DQG LPSURYHG ODQJXDJH UDWKHU WKDQ WKH ROG RQH 6RPH SHRSOH KDYH VXJJHVWHG WKDW SHUKDSV -DYD LV & 9DULDEOHV DQG 0L[LQJ 7\SHV :H KDYH VHHQ WKDW ZKHQ \RX GHFODUH D YDULDEOH \RX PXVW WHOO -DYD ZKDW W\SH RI YDOXH LW ZLOO EH VWRULQJ )RU H[DPSOH \RX PLJKW GHFODUH D YDULDEOH RI W\SH LQW IRU LQWHJHU YDOXHV RU RI W\SH GRXEOH IRU UHDO YDOXHV 7KH VLWXDWLRQ LV IDLUO\ FOHDU ZKHQ \RX KDYH MXVW LQWHJHUV RU MXVW UHDOV EXW ZKDW KDSSHQV ZKHQ \RX VWDUW PL[LQJ WKH W\SHV" )RU H[DPSOH WKH IROORZLQJ FRGH LV FOHDUO\ RND\ int x; double y; x = 2 + 3; y = 3.4 * 2.9; :H KDYH DQ LQWHJHU YDULDEOH WKDW ZH DVVLJQ DQ LQWHJHU YDOXH DQG D GRXEOH YDULDEOH WKDW ZH DVVLJQ D GRXEOH YDOXH %XW ZKDW LI ZH WU\ WR GR LW WKH RWKHU ZD\ DURXQG" 84 int x; double y; x = 3.4 * 2.9; y = 2 + 3; // illegal // okay $V WKH FRPPHQWV LQGLFDWH \RX FDQ W DVVLJQ DQ LQWHJHU YDULDEOH D GRXEOH YDOXH EXW \RX FDQ DVVLJQ D GRXEOH YDULDEOH DQ LQWHJHU YDOXH /HW V FRQVLGHU WKH VHFRQG FDVH ILUVW 7KH H[SUHVVLRQ 2 + 3 HYDOXDWHV WR WKH LQWHJHU 7KLV YDOXH LVQ W D GRXEOH EXW HYHU\ LQWHJHU LV D UHDO YDOXH VR LW LV HDV\ HQRXJK IRU -DYD WR FRQYHUW WKH LQWHJHU LQWR D GRXEOH 7KH WHFKQLFDO WHUP LV WKDW -DYD SURPRWHV WKH LQWHJHU LQWR D GRXEOH 7KH RWKHU GLUHFWLRQ LV PRUH SUREOHPDWLF 7KH H[SUHVVLRQ 3.4 * 2.9 HYDOXDWHV WR WKH GRXEOH YDOXH 7KLV YDOXH FDQ W EH VWRUHG LQ DQ LQWHJHU EHFDXVH LW LVQ W DQ LQWHJHU ,I \RX ZDQWHG WR SHUIRUP WKLV NLQG RI RSHUDWLRQ \RX G KDYH WR WHOO -DYD KRZ WR FRQYHUW WKLV LQWR DQ LQWHJHU $V GHVFULEHG HDUOLHU \RX FDQ FDVW WR DQ LQW ZKLFK ZLOO WUXQFDWH DQ\WKLQJ DIWHU WKH GHFLPDO SRLQW x = (int) (3.4 * 2.9); // now legal 7KLV VWDWHPHQW ILUVW HYDOXDWHV WR JHW DQG WKHQ WUXQFDWHV WR JHW WKH LQWHJHU &RPPRQ 3URJUDPPLQJ (UURU )RUJHWWLQJ WR &DVW :H RIWHQ ZULWH SURJUDPV WKDW LQYROYH D PL[WXUH RI LQWV DQG GRXEOHV VR LW LV HDV\ WR PDNH PLVWDNHV ZKHQ LW FRPHV WR FRPELQDWLRQV RI WKH WZR )RU H[DPSOH VXSSRVH WKDW ZH ZDQW WR FRPSXWH D VWXGHQW V SHUFHQW ULJKW RQ D WHVW JLYHQ WKH WRWDO QXPEHU RI TXHVWLRQV RQ WKH WHVW DQG WKH QXPEHU RI TXHVWLRQV WKH VWXGHQW JRW ULJKW :H PLJKW GHFODUH WKH IROORZLQJ YDULDEOHV int totalQuestions; int numRight; double percent; 6XSSRVH WKH ILUVW WZR DUH LQLWLDOL]HG DV IROORZV totalQuestions = 73; numRight = 59; +RZ GR ZH FRPSXWH WKH SHUFHQW WKDW WKH VWXGHQW JRW ULJKW" :H GLYLGH WKH QXPEHU ULJKW E\ WKH WRWDO QXPEHU RI TXHVWLRQV DQG PXOWLSO\ E\ WR WXUQ LW LQWR D SHUFHQWDJH percent = numRight / totalQuestions * 100; 8QIRUWXQDWHO\ LI ZH SULQW RXW WKH YDOXH RI WKH YDULDEOH SHUFHQW DIWHU H[HFXWLQJ WKLV OLQH RI FRGH ZH ZLOO ILQG WKDW LW KDV WKH YDOXH 2EYLRXVO\ WKH VWXGHQW JRW PRUH WKDQ FRUUHFW 7KH SUREOHP FRPHV IURP LQWHJHU GLYLVLRQ 7KH H[SUHVVLRQ ZH DUH XVLQJ EHJLQV ZLWK WZR LQW YDOXHV numRight / totalQuestions :KLFK PHDQV ZH DUH FRPSXWLQJ 59 / 73 85 7KLV HYDOXDWHV WR ZLWK LQWHJHU GLYLVLRQ 6RPH VWXGHQWV IL[ WKLV E\ FKDQJLQJ WKH W\SHV RI WKH YDULDEOHV WR EH DOO GRXEOHV 7KDW ZLOO VROYH WKH LPPHGLDWH SUREOHP EXW LW V QRW D JRRG FKRLFH WR PDNH IURP D VW\OLVWLF SRLQW RI YLHZ ,W LV EHVW WR XVH WKH PRVW DSSURSULDWH W\SH IRU GDWD DQG ZH FHUWDLQO\ H[SHFW WKDW WKH QXPEHU RI TXHVWLRQV RQ WKH WHVW ZLOO EH DQ LQWHJHU :H FRXOG WU\ WR IL[ WKLV E\ FKDQJLQJ WKH YDOXH percent = numRight / totalQuestions * 100.0; WR 7KLV GRHVQ W KHOS EHFDXVH ZH GR WKH GLYLVLRQ ILUVW 2QH YHUVLRQ WKDW GRHV ZRUN LV WR SXW WKH ILUVW percent = 100.0 * numRight / totalQuestions; 7KLV ZRUNV EHFDXVH QRZ WKH PXOWLSOLFDWLRQ LV FRPSXWHG EHIRUH WKH GLYLVLRQ ZKLFK PHDQV WKDW HYHU\WKLQJ LV FRQYHUWHG WR GRXEOH 6RPHWLPHV \RX FDQ IL[ D SUREOHP OLNH WKLV WKURXJK D FOHYHU UHDUUDQJHPHQW RI WKH IRUPXOD EXW \RX GRQ W ZDQW WR FRXQW RQ FOHYHUQHVV 7KLV LV D JRRG SODFH WR XVH D FDVW )RU H[DPSOH UHWXUQLQJ WR RXU RULJLQDO IRUPXOD ZH FDQ FDVW HDFK RI WKH LQW YDULDEOHV LQWR GRXEOH percent = (double) numRight / (double) totalQuestions * 100.0; :H FDQ DOVR WDNH DGYDQWDJH RI WKH IDFW WKDW RQFH ZH KDYH FDVW RQH RI WKHVH WZR YDULDEOHV WR GRXEOH WKH GLYLVLRQ ZLOO EH GRQH ZLWK GRXEOHV 6R ZH FRXOG IRU H[DPSOH FDVW MXVW WKH ILUVW YDOXH WR GRXEOH percent = (double) numRight / totalQuestions * 100.0; 7KH IRU /RRS 3URJUDPPLQJ RIWHQ LQYROYHV VSHFLI\LQJ UHGXQGDQW WDVNV 7KH IRU ORRS KHOSV WR DYRLG VXFK UHGXQGDQF\ 6XSSRVH \RX ZDQW WR ZULWH RXW WKH VTXDUHV RI WKH ILUVW LQWHJHUV <RX FDQ VD\ 1 2 3 4 5 6 7 8 9 public class WriteSquares { public static void main(String args) System.out.println(1 + " squared = System.out.println(2 + " squared = System.out.println(3 + " squared = System.out.println(4 + " squared = System.out.println(5 + " squared = } } { " " " " " + + + + + (1 (2 (3 (4 (5 * * * * * 1)); 2)); 3)); 4)); 5)); 7KLV SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW 1 2 3 4 5 squared squared squared squared squared = = = = = 1 4 9 16 25 7KLV DSSURDFK LV WHGLRXV 7KH SURJUDP KDV ILYH VWDWHPHQWV WKDW DUH YHU\ VLPLODU 7KH\ DUH DOO RI WKH IRUP System.out.println(number + " squared = " + (number * number)); 86 ZKHUH QXPEHU LV HLWKHU SURJUDP XVLQJ D IRU ORRS 1 2 3 4 5 6 7 RU 7KH IRU ORRS DYRLGV VXFK UHGXQGDQF\ +HUH LV DQ HTXLYDOHQW public class WriteSquares2 { public static void main(String args) { for (int i = 1; i <= 5; i++) { System.out.println(i + " squared = " + (i * i)); } } } 7KLV SURJUDP LQLWLDOL]HV D YDULDEOH FDOOHG L WR WKH YDOXH 7KHQ LW UHSHDWHGO\ H[HFXWHV WKH SULQWOQ VWDWHPHQW DV ORQJ DV WKH YDULDEOH L LV OHVV WKDQ RU HTXDO WR $IWHU HDFK SULQWOQ LW HYDOXDWHV WKH H[SUHVVLRQ L WR LQFUHPHQW L 7KH JHQHUDO V\QWD[ RI WKH IRU ORRS LV DV IROORZV for (<initialization>; <continuation test>; <update>) { <statement>; <statement>; ... <statement>; } <RX DOZD\V LQFOXGH WKH NH\ZRUG IRU DQG WKH SDUHQWKHVHV ,QVLGH WKH SDUHQWKHVHV \RX KDYH WKUHH GLIIHUHQW SDUWV VHSDUDWHG E\ VHPLFRORQV WKH LQLWLDOL]DWLRQ WKH FRQWLQXDWLRQ WHVW DQG WKH XSGDWH 7KHQ \RX KDYH D VHW RI FXUO\ EUDFHV WKDW HQFORVH D VHW RI VWDWHPHQWV 7KH IRU ORRS FRQWUROV WKHVH VWDWHPHQWV LQVLGH WKH FXUO\ EUDFHV :H UHIHU WR WKH FRQWUROOHG VWDWHPHQWV DV WKH ERG\ RI WKH ORRS 7KH LGHD LV WKDW ZH H[HFXWH WKH ERG\ PXOWLSOH WLPHV DV GHWHUPLQHG E\ WKH FRPELQDWLRQ RI WKH RWKHU WKUHH SDUWV 7KH IROORZLQJ GLDJUDP LQGLFDWHV WKH VWHSV WKDW -DYD IROORZV WR H[HFXWH D IRU ORRS ,W SHUIRUPV ZKDWHYHU LQLWLDOL]DWLRQ \RX KDYH UHTXHVWHG RQFH EHIRUH WKH ORRS EHJLQV H[HFXWLQJ 7KHQ LW UHSHDWHGO\ SHUIRUPV WKH FRQWLQXDWLRQ WHVW \RX KDYH SURYLGHG ,I WKH FRQWLQXDWLRQ WHVW HYDOXDWHV WR WUXH WKHQ LW H[HFXWHV WKH FRQWUROOHG VWDWHPHQWV RQFH DQG H[HFXWHV WKH XSGDWH SDUW 7KHQ LW SHUIRUPV WKH WHVW DJDLQ DQG LI LW DJDLQ HYDOXDWHV WR WUXH LW H[HFXWHV WKH VWDWHPHQWV DJDLQ DQG HYDOXDWHV WKH XSGDWH DJDLQ 1RWLFH WKDW WKH XSGDWH LV SHUIRUPHG DIWHU WKH FRQWUROOHG VWDWHPHQWV DUH H[HFXWHG :KHQ WKH WHVW HYDOXDWHV WR IDOVH -DYD LV GRQH H[HFXWLQJ WKH ORRS DQG PRYHV RQ WR ZKDWHYHU VWDWHPHQW FRPHV DIWHU WKH ORRS 87 7KH IRU ORRS LV WKH ILUVW H[DPSOH RI D FRQWURO VWUXFWXUH &RQWURO 6WUXFWXUH $ V\QWDFWLF VWUXFWXUH WKDW FRQWUROV RWKHU VWDWHPHQWV <RX VKRXOG EH FDUHIXO WR XVH LQGHQWDWLRQ WR LQGLFDWH FRQWUROOHG VWDWHPHQWV ,Q WKH FDVH RI WKH IRU ORRS ZH LQGHQW DOO RI WKH VWDWHPHQWV LQ WKH ERG\ RI WKH ORRS DV D ZD\ WR LQGLFDWH WKDW WKH\ DUH LQVLGH WKH ORRS 7UDFLQJ IRU /RRSV /HW V H[DPLQH WKH IRU ORRS RI WKH :ULWH6TXDUHV SURJUDP LQ GHWDLO for (int i = 1; i <= 5; i++) { System.out.println(i + " squared = " + (i * i)); } ,Q WKLV ORRS WKH LQLWLDOL]DWLRQ LQW L GHFODUHV DQ LQWHJHU YDULDEOH L WKDW LV LQLWLDOL]HG WR 7KH FRQWLQXDWLRQ WHVW L LQGLFDWHV WKDW ZH VKRXOG NHHS H[HFXWLQJ DV ORQJ DV L LV OHVV WKDQ RU HTXDO WR 7KDW PHDQV WKDW RQFH L LV JUHDWHU WKDQ ZH ZLOO VWRS H[HFXWLQJ WKH ERG\ RI WKH ORRS 7KH XSGDWH L ZLOO LQFUHDVH L E\ RQH HDFK WLPH JHWWLQJ L FORVHU WR EHLQJ ODUJHU WKDQ $IWHU ILYH H[HFXWLRQV RI WKH ERG\ DQG WKH DFFRPSDQ\LQJ ILYH XSGDWHV L ZLOO EH ODUJHU WKDQ DQG WKH ORRS ZLOO ILQLVK H[HFXWLQJ 7KH IROORZLQJ LV D GHWDLOHG WUDFH RI WKLV SURFHVV 88 Step Code Description ---------------------------------------------------------------------------initialization int i = 1; variable i is allocated and initialized to 1 test body update test body update test body update test body update test body update test i <= 5 {...} i++ i <= 5 {...} i++ i <= 5 {...} i++ i <= 5 {...} i++ i <= 5 {...} i++ i <= 5 true because 1 <= 5, so we enter the loop execute the println with i equal to 1 increment i, which becomes 2 true because 2 <= 5, so we enter the loop execute the println with i equal to 2 increment i, which becomes 3 true because 3 <= 5, so we enter the loop execute the println with i equal to 3 increment i, which becomes 4 true because 4 <= 5, so we enter the loop execute the println with i equal to 4 increment i, which becomes 5 true because 5 <= 5, so we enter the loop execute the println with i equal to 5 increment i, which becomes 6 false because 6 > 5, so we are finished 1RWLFH IURP WKLV WUDFH WKDW WKH ERG\ RI WKH ORRS WKH SULQWOQ LV H[HFXWHG D WRWDO RI WLPHV -DYD DOORZV JUHDW IOH[LELOLW\ LQ GHFLGLQJ ZKDW WR LQFOXGH LQ WKH LQLWLDOL]DWLRQ SDUW DQG WKH XSGDWH ZKLFK DOORZV XV WR XVH WKH IRU ORRS WR VROYH DOO VRUWV RI SURJUDPPLQJ WDVNV )RU ULJKW QRZ ZH ZLOO UHVWULFW RXUVHOYHV WR D SDUWLFXODU NLQG RI ORRS WKDW GHFODUHV DQG LQLWLDOL]HV D VLQJOH YDULDEOH WKDW LV XVHG WR FRQWURO WKH ORRS 7KLV YDULDEOH LV RIWHQ UHIHUUHG WR DV WKH FRQWURO YDULDEOH RI WKH ORRS ,Q WKH WHVW ZH FRPSDUH WKH FRQWURO YDULDEOH DJDLQVW VRPH ILQDO GHVLUHG YDOXH DQG LQ WKH XSGDWH ZH FKDQJH WKH YDOXH RI WKH FRQWURO YDULDEOH PRVW RIWHQ LQFUHPHQWLQJ LW E\ 6XFK ORRSV DUH YHU\ FRPPRQ LQ SURJUDPPLQJ %\ FRQYHQWLRQ ZH RIWHQ XVH YDULDEOH QDPHV OLNH L M DQG N IRU VXFK ORRSV (DFK H[HFXWLRQ RI WKH FRQWUROOHG VWDWHPHQW RI D ORRS LV FDOOHG DQ LWHUDWLRQ RI WKH ORRS DV LQ 7KH ORRS ILQLVKHG H[HFXWLQJ DIWHU IRXU LWHUDWLRQV ,WHUDWLRQ DOVR UHIHUV WR ORRSLQJ LQ JHQHUDO DV LQ , VROYHG WKH SUREOHP XVLQJ LWHUDWLRQ &RQVLGHU DQRWKHU IRU ORRS for (int i = -100; i <= 100; i++) { System.out.println(i + " squared = " + (i * i)); } 7KLV ORRS H[HFXWHV D WRWDO RI WLPHV SURGXFLQJ WKH VTXDUHV RI DOO WKH LQWHJHUV EHWZHHQ DQG LQFOXVLYH 7KH YDOXHV XVHG LQ WKH LQLWLDOL]DWLRQ DQG WKH WHVW WKHQ FDQ EH DQ\ LQWHJHUV 7KH\ FDQ LQ IDFW EH DUELWUDU\ LQWHJHU H[SUHVVLRQV for (int i = (2 + 2); i <= (17 * 3); i++) { System.out.println(i + " squared = " + (i * i)); } 89 7KLV ORRS ZLOO JHQHUDWH WKH VTXDUHV EHWZHHQ DQG LQFOXVLYH 7KH SDUHQWKHVHV DURXQG WKH H[SUHVVLRQV DUH QRW QHFHVVDU\ EXW LPSURYH UHDGDELOLW\ &RQVLGHU WKH IROORZLQJ ORRS for (int i = 1; i <= 30; i++) { System.out.println("+--------+"); } 7KLV ORRS JHQHUDWHV OLQHV RI RXWSXW DOO H[DFWO\ WKH VDPH 7KLV ORRS LV VOLJKWO\ GLIIHUHQW IURP WKH SUHYLRXV RQH EHFDXVH WKH VWDWHPHQW FRQWUROOHG E\ WKH IRU ORRS PDNHV QR UHIHUHQFH WR WKH FRQWURO YDULDEOH 7KXV for (int i = -30; i <= -1; i++) { System.out.println("+--------+"); } JHQHUDWHV H[DFWO\ WKH VDPH RXWSXW 7KH EHKDYLRU RI VXFK D ORRS LV GHWHUPLQHG VROHO\ E\ WKH QXPEHU RI LWHUDWLRQV LW SHUIRUPV 7KH QXPEHU RI LWHUDWLRQV LV JLYHQ E\ (ending value) - (starting value) + 1 ,W LV PXFK VLPSOHU WR VHH WKDW WKH ILUVW RI WKHVH ORRSV LWHUDWHV WLPHV VR LW LV EHWWHU WR XVH ,Q JHQHUDO LI ZH ZDQW D ORRS WR LWHUDWH H[DFWO\ Q WLPHV ZH ZLOO XVH RQH RI WZR VWDQGDUG ORRSV 7KH ILUVW VWDQGDUG IRUP ORRNV OLNH WKH RQHV ZH KDYH VHHQ DERYH for (int <variable> = 1; <variable> <= n; i++) { <statement>; <statement>; ... <statement>; } ,W V SUHWW\ FOHDU WKDW WKLV ORRS H[HFXWHV Q WLPHV EHFDXVH LW VWDUWV DW DQG FRQWLQXHV DV ORQJ DV LW LV OHVV WKDQ RU HTXDO WR Q 2IWHQ KRZHYHU LW LV PRUH FRQYHQLHQW WR VWDUW RXU FRXQWLQJ DW LQVWHDG RI 7KDW UHTXLUHV D FKDQJH LQ WKH ORRS WHVW WR DOORZ XV WR VWRS ZKHQ Q LV OHVV for (int <variable> = 0; <variable> < n; i++) { <statement>; <statement>; ... <statement>; } 1RWLFH WKDW LQ WKLV IRUP ZKHQ ZH LQLWLDOL]H WKH YDULDEOH WR ZH WHVW ZKHWKHU LW LV VWULFWO\ OHVV WKDQ Q (LWKHU IRUP ZLOO H[HFXWH H[DFWO\ Q WLPHV DOWKRXJK ZH ZLOO VHH VRPH VLWXDWLRQV ZKHUH WKH EDVHG ORRS ZRUNV EHWWHU /HW V FRQVLGHU VRPH ERUGHUOLQH FDVHV :KDW KDSSHQV LI \RX VD\ for (int i = 1; i <= 1; i++) { System.out.println("+--------+"); } 90 $FFRUGLQJ WR RXU UXOH LW VKRXOG LWHUDWH RQFH DQG LW GRHV ,W LQLWLDOL]HV WKH YDULDEOH L WR DQG WHVWV WR VHH LI WKLV LV OHVV WKDQ RU HTXDO WR ZKLFK LW LV 6R LW H[HFXWHV WKH SULQWOQ LQFUHPHQWV L DQG WHVWV DJDLQ 7KH VHFRQG WLPH LW WHVWV LW ILQGV WKDW L LV QR ORQJHU OHVV WKDQ RU HTXDO WR VR LW VWRSV H[HFXWLQJ :KDW DERXW WKLV ORRS for (int i = 1; i <= 0; i++) { System.out.println("+--------+"); } // never executes 7KLV ORRS SHUIRUPV QR LWHUDWLRQV DW DOO ,W ZLOO QRW FDXVH DQ H[HFXWLRQ HUURU LW MXVW ZRQ W H[HFXWH WKH ERG\ 7KH YDULDEOH LV LQLWLDOL]HG WR DQG ZH WHVW WR VHH LI LW LV OHVV WKDQ RU HTXDO WR ,W LVQ W VR ZH GRQ W H[HFXWH WKH VWDWHPHQWV LQ WKH ERG\ DW DOO :KHQ \RX FRQVWUXFW D IRU ORRS \RX FDQ LQFOXGH PRUH WKDQ RQH VWDWHPHQW LQVLGH WKH FXUO\ EUDFHV &RQVLGHU IRU H[DPSOH WKH IROORZLQJ FRGH for (int i = 1; i <= 20; i++) { System.out.println("Hi!"); System.out.println("Ho!"); } 7KLV ZLOO SURGXFH SDLUV RI OLQHV WKH ILUVW RI ZKLFK KDV WKH ZRUG +L RQ LW DQG WKH VHFRQG RI ZKLFK KDV WKH ZRUG +R :KHQ D IRU ORRS FRQWUROV D VLQJOH VWDWHPHQW \RX GRQ W KDYH WR LQFOXGH WKH FXUO\ EUDFHV 7KH FXUO\ EUDFHV DUH UHTXLUHG RQO\ IRU VLWXDWLRQV OLNH WKH RQH DERYH ZKHUH \RX KDYH PRUH WKDQ RQH VWDWHPHQW WKDW \RX ZDQW WKH ORRS WR FRQWURO 7KH 6XQ FRGLQJ FRQYHQWLRQ LQFOXGHV WKH FXUO\ EUDFHV HYHQ IRU D VLQJOH VWDWHPHQW DQG ZH IROORZ WKLV FRQYHQWLRQ LQ WKLV ERRN 7KHUH DUH VHYHUDO DGYDQWDJHV WR WKLV FRQYHQWLRQ ,QFOXGLQJ WKH FXUO\ EUDFHV SUHYHQWV IXWXUH HUURUV (YHQ LI \RX QHHG RQO\ RQH VWDWHPHQW LQ WKH ERG\ RI \RXU ORRS QRZ \RXU FRGH LV OLNHO\ WR FKDQJH RYHU WLPH +DYLQJ WKH FXUO\ EUDFHV WKHUH PDNHV LW OHVV OLNHO\ WKDW \RX ZLOO DFFLGHQWDOO\ DGG DQ H[WUD VWDWHPHQW WR WKH ERG\ ODWHU DQG IRUJHW WR LQFOXGH FXUO\ EUDFHV ,Q JHQHUDO LQFOXGLQJ FXUO\ EUDFHV LQ DGYDQFH LV FKHDSHU WKDQ ORFDWLQJ REVFXUH EXJV ODWHU $OZD\V LQFOXGLQJ WKH FXUO\ EUDFHV UHGXFHV WKH OHYHO RI GHWDLO \RX KDYH WR FRQVLGHU DV \RX OHDUQ QHZ FRQWURO VWUXFWXUHV ,W WDNHV WLPH WR PDVWHU WKH GHWDLOV RI DQ\ QHZ FRQWURO VWUXFWXUH ,W ZLOO EH HDVLHU WR PDVWHU WKRVH GHWDLOV LI \RX GRQ W KDYH WR DOVR EH WKLQNLQJ DERXW ZKHQ WR LQFOXGH DQG ZKHQ QRW WR LQFOXGH WKH EUDFHV &RPPRQ 3URJUDPPLQJ (UURU )RUJHWWLQJ &XUO\ %UDFHV <RX VKRXOG XVH LQGHQWDWLRQ WR LQGLFDWH WKH ERG\ RI D IRU ORRS EXW LQGHQWDWLRQ DORQH LV QRW HQRXJK -DYD LJQRUHV LQGHQWDWLRQ ZKHQ GHFLGLQJ KRZ GLIIHUHQW VWDWHPHQWV DUH JURXSHG 6XSSRVH IRU H[DPSOH WKDW \RX ZHUH WR ZULWH WKH IROORZLQJ FRGH for (int i = 1; i <= 20; i++) System.out.println("Hi!"); System.out.println("Ho!"); 91 7KH LQGHQWDWLRQ LQGLFDWHV WKDW ERWK RI WKH SULQWOQ VWDWHPHQWV DUH LQ WKH ERG\ RI WKH IRU ORRS %XW WKHUH DUHQ W DQ\ FXUO\ EUDFHV WR LQGLFDWH WKDW WR -DYD $V D UHVXOW WKLV FRGH LV LQWHUSUHWHG DV IROORZV for (int i = 1; i <= 20; i++) { System.out.println("Hi!"); } System.out.println("Ho!"); 2QO\ WKH ILUVW SULQWOQ LV FRQVLGHUHG WR EH LQ WKH ERG\ RI WKH IRU ORRS 7KH VHFRQG SULQWOQ LV FRQVLGHUHG WR EH RXWVLGH WKH ORRS 6R WKLV FRGH ZRXOG SURGXFH OLQHV RI RXWSXW WKDW DOO VD\ +L IROORZHG E\ RQH OLQH RI RXWSXW WKDW VD\V +R 7R LQFOXGH ERWK SULQWOQV LQ WKH ERG\ ZH QHHG FXUO\ EUDFHV DURXQG WKHP for (int i = 1; i <= 20; i++) { System.out.println("Hi!"); System.out.println("Ho!"); } SULQW 9HUVXV SULQWOQ 6R IDU ZH KDYH EHHQ SURGXFLQJ HQWLUH OLQHV RI RXWSXW XVLQJ SULQWOQ FRPPDQGV 1RZ WKDW ZH QRZ KRZ WR ZULWH IRU ORRSV ZH ZLOO ZDQW WR EH DEOH WR SURGXFH FRPSOH[ OLQHV RI RXWSXW SLHFH E\ SLHFH )RU H[DPSOH LI ZH ZDQW WR SURGXFH D OLQH RI RXWSXW WKDW KDV VWDUV RQ LW LW ZRXOG EH HDVLHU WR XVH D ORRS WKDW SULQWV RQH VWDU DW D WLPH DQG KDYH LW H[HFXWH WLPHV UDWKHU WKDQ WU\LQJ WR XVH D VLQJOH SULQWOQ %HIRUH ZH FDQ GR WKDW ZH KDYH WR OHDUQ DERXW D YDULDWLRQ RI WKH SULQWOQ FRPPDQG -DYD KDV D YDULDWLRQ RI WKH SULQWOQ FRPPDQG FDOOHG SULQW WKDW DOORZV \RX WR SURGXFH RXWSXW RQ WKH FXUUHQW OLQH ZLWKRXW JRLQJ WR D QHZ OLQH RI RXWSXW 7KH SULQWOQ FRPPDQG UHDOO\ GRHV WZR GLIIHUHQW WKLQJV LW VHQGV RXWSXW WR WKH FXUUHQW OLQH DQG WKHQ LW PRYHV WR WKH EHJLQQLQJ RI D QHZ OLQH 7KH SULQW FRPPDQG GRHV RQO\ WKH ILUVW RI WKHVH 7KXV D VHULHV RI SULQW FRPPDQGV ZLOO JHQHUDWH RXWSXW DOO RQ WKH VDPH OLQH 2QO\ D SULQWOQ FRPPDQG ZLOO FDXVH WKH FXUUHQW OLQH WR EH FRPSOHWHG DQG D QHZ OLQH WR EH VWDUWHG )RU H[DPSOH FRQVLGHU WKHVH VL[ VWDWHPHQWV System.out.print("Hi Ho, "); System.out.print("my man."); System.out.print("Where are you bound? "); System.out.println("My way, I hope."); System.out.print("This is"); System.out.println(" for the whole family!"); 7KHVH VWDWHPHQWV SURGXFH WZR OLQHV RI RXWSXW 5HPHPEHU HYHU\ SULQWOQ VWDWHPHQW SURGXFHV H[DFWO\ RQH OLQH RI RXWSXW %HFDXVH WKHUH DUH WZR SULQWOQ VWDWHPHQWV KHUH WKHUH DUH WZR OLQHV RI RXWSXW $IWHU WKH ILUVW VWDWHPHQW H[HFXWHV WKH FXUUHQW OLQH ORRNV OLNH WKLV Hi ho, ^ 7KH DUURZ EHORZ WKH RXWSXW OLQH LQGLFDWHV WKH SRVLWLRQ ZKHUH RXWSXW ZLOO EH VHQW QH[W :H FDQ VLPSOLI\ RXU GLVFXVLRQ LI ZH UHIHU WR WKH DUURZ DV WKH RXWSXW FXUVRU 1RWLFH WKDW WKH RXWSXW FXUVRU LV DW WKH HQG RI WKLV OLQH DQG WKDW LW LV SUHFHGHG E\ D VSDFH 7KDW LV VR EHFDXVH WKH FRPPDQG ZDV D SULQW GRQ W JR WR D QHZ OLQH DQG WKH VWULQJ OLWHUDO LQ WKH SULQW HQGV ZLWK D VSDFH -DYD ZLOO QRW LQVHUW D VSDFH IRU \RX XQOHVV \RX VSHFLILFDOO\ UHTXHVW LW $IWHU WKH QH[W SULQW WKH OLQH ORRNV OLNH WKLV 92 Hi ho, my man. ^ 7KH RXWSXW FXUVRU GRHVQ W KDYH D VSDFH EHIRUH LW QRZ EHFDXVH WKH VWULQJ OLWHUDO LQ WKH SULQW FRPPDQG HQGV LQ D SHULRG QRW D VSDFH $IWHU WKH QH[W SULQW WKH OLQH ORRNV OLNH WKLV Hi ho, my man.Where are you bound? ^ 7KHUH LV QR VSDFH EHWZHHQ WKH SHULRG DQG WKH ZRUG :KHUH EHFDXVH WKHUH ZDV QR VSDFH LQ WKH SULQW FRPPDQGV %XW WKH VWULQJ OLWHUDO LQ WKH WKLUG VWDWHPHQW KDV VSDFHV DW WKH HQG DQG DV D UHVXOW WKH RXWSXW FXUVRU LV SRVLWLRQHG WZR VSDFHV DIWHU WKH TXHVWLRQ PDUN $IWHU WKH QH[W VWDWHPHQW H[HFXWHV WKH RXWSXW ORRNV OLNH WKLV Hi ho, my man.Where are you bound? ^ My way, I hope. %HFDXVH WKLV IRXUWK VWDWHPHQW LV D SULQWOQ FRPPDQG LW ILQLVKHV WKH RXWSXW OLQH DQG SRVLWLRQV WKH FXUVRU DW WKH EHJLQQLQJ RI WKH VHFRQG OLQH 7KH QH[W VWDWHPHQW LV DQRWKHU SULQW WKDW SURGXFHV WKLV Hi ho, my man.Where are you bound? This is ^ My way, I hope. 7KH ILQDO SULQWOQ FRPSOHWHV WKH VHFRQG OLQH DQG SRVLWLRQV WKH RXWSXW FXUVRU DW WKH EHJLQQLQJ RI D QHZ OLQH Hi ho, my man.Where are you bound? This is for the whole family! ^ My way, I hope. 7KHVH VL[ VWDWHPHQWV DUH HTXLYDOHQW WR WKHVH WZR VLQJOH VWDWHPHQWV System.out.println("Hi ho, my man.Where are you bound? System.out.println("This is for the whole family!"); My way, I hope."); ,W VHHPV D ELW VLOO\ WR KDYH ERWK WKH SULQW DQG WKH SULQWOQ FRPPDQGV IRU SURGXFLQJ OLQHV OLNH WKHVH EXW \RX ZLOO VHH WKDW WKHUH DUH PRUH LQWHUHVWLQJ DSSOLFDWLRQV RI SULQW LQ FRPELQDWLRQ ZLWK IRU ORRSV 5HPHPEHU WKDW LW LV SRVVLEOH WR KDYH DQ HPSW\ SULQWOQ FRPPDQG System.out.println(); %HFDXVH WKHUH LV QRWKLQJ LQVLGH RI SDUHQWKHVHV WR EH ZULWWHQ WR WKH RXWSXW OLQH WKLV SRVLWLRQV WKH RXWSXW FXUVRU WR WKH EHJLQQLQJ RI WKH QH[W OLQH ,I WKHUH DUH SULQW FRPPDQGV EHIRUH WKLV HPSW\ SULQWOQ LW ILQLVKHV RXW WKH OLQH PDGH E\ WKRVH SULQW FRPPDQGV ,I WKHUH DUH QR SUHYLRXV SULQW FRPPDQGV LW SURGXFHV D EODQN OLQH $Q HPSW\ SULQW FRPPDQG LV PHDQLQJOHVV DQG LV LOOHJDO 93 1HVWHG IRU /RRSV 7KH IRU ORRS FRQWUROV D VWDWHPHQW DQG WKH IRU ORRS LV LWVHOI D VWDWHPHQW ZKLFK PHDQV WKDW RQH IRU ORRS FDQ FRQWURO DQRWKHU IRU ORRS )RU H[DPSOH ZH FDQ ZULWH FRGH OLNH WKH IROORZLQJ for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; j++) { System.out.println("Hi there."); } } 7KLV FRGH LV SUREDEO\ HDVLHU WR UHDG IURP WKH LQVLGH RXW 7KH SULQWOQ VWDWHPHQW SURGXFHV D VLQJOH OLQH RI RXWSXW 7KH LQQHU M ORRS H[HFXWHV WKLV VWDWHPHQW WLPHV SURGXFLQJ OLQHV RI RXWSXW 7KH RXWHU L ORRS H[HFXWHV WKH LQQHU ORRS WLPHV ZKLFK SURGXFHV VHWV RI OLQHV RU OLQHV RI RXWSXW 7KH FRGH DERYH WKHQ LV HTXLYDOHQW WR for (int i = 1; i <= 50; i++) { System.out.println("Hi there."); } 7KLV H[DPSOH VKRZV WKDW D IRU ORRS FDQ EH FRQWUROOHG E\ DQRWKHU IRU ORRS 6XFK D ORRS LV FDOOHG D QHVWHG ORRS 7KLV H[DPSOH ZDVQ W YHU\ LQWHUHVWLQJ EHFDXVH WKH QHVWHG ORRS FDQ EH HOLPLQDWHG /HW V ORRN DW D PRUH LQWHUHVWLQJ QHVWHG ORRS WKDW GRHV VRPHWKLQJ XVHIXO for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); } :KHQ \RX ZULWH FRGH WKDW LQYROYHV QHVWHG ORRSV \RX KDYH WR EH FDUHIXO WR LQGHQW WKH FRGH MXVW ULJKW WR PDNH WKH VWUXFWXUH FOHDU $W WKH RXWHUPRVW OHYHO WKH FRGH DERYH LV D VLPSOH IRU ORRS WKDW H[HFXWHV WLPHV for (int i = 1; i <= 6; i++) { ... } :H XVH LQGHQWDWLRQ IRU WKH VWDWHPHQWV LQVLGH WKLV IRU ORRS WR PDNH LW FOHDU WKDW WKH\ DUH WKH ERG\ RI WKLV ORRS ,QVLGH ZH ILQG WZR VWDWHPHQWV DQRWKHU IRU ORRS DQG D SULQWOQ /HW V ORRN DW WKH LQQHU IRU ORRS for (int j = 1; j <= 10; j++) { System.out.print("*"); } 7KLV ORRS LV FRQWUROOHG E\ WKH RXWHU IRU ORRS ZKLFK LV ZK\ LW LV LQGHQWHG EXW LW LWVHOI FRQWUROV D VWDWHPHQW WKH SULQW VWDWHPHQW VR ZH HQG XS ZLWK DQRWKHU OHYHO RI LQGHQWDWLRQ WR LQGLFDWH WKDW WKH SULQW LV FRQWUROOHG E\ WKH LQQHU IRU ORRS ZKLFK LQ WXUQ LV FRQWUROOHG E\ WKH RXWHU IRU ORRS 6R ZKDW GRHV WKLV LQQHU ORRS GR" ,W SULQWV WHQ VWDUV RQ WKH FXUUHQW OLQH RI RXWSXW 7KH\ DOO DSSHDU RQ WKH VDPH OLQH RI RXWSXW EHFDXVH ZH DUH XVLQJ D SULQW LQVWHDG RI D SULQWOQ 1RWLFH WKDW DIWHU WKLV ORRS ZH SHUIRUP D SULQWOQ 94 System.out.println(); 7KH QHW HIIHFW RI WKH IRU ORRS IROORZHG E\ WKH SULQWOQ LV WKDW ZH JHW D OLQH RI RXWSXW ZLWK VWDUV RQ LW %XW UHPHPEHU WKDW WKHVH VWDWHPHQWV DUH FRQWDLQHG LQ DQ RXWHU ORRS WKDW H[HFXWHV WLPHV 6R ZH HQG XS JHWWLQJ OLQHV RI RXWSXW HDFK ZLWK VWDUV ********** ********** ********** ********** ********** ********** /HW V H[DPLQH RQH PRUH YDULDWLRQ ,Q WKH FRGH DERYH WKH LQQHU IRU ORRS DOZD\V GRHV H[DFWO\ WKH VDPH WKLQJ ,W SULQWV H[DFWO\ VWDUV RQ D OLQH RI RXWSXW %XW ZKDW KDSSHQV LI ZH FKDQJH WKH WHVW IRU WKH LQQHU IRU ORRS WR PDNH XVH RI WKH RXWHU IRU ORRS V FRQWURO YDULDEOH L " for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } ,Q WKH ROG YHUVLRQ WKH LQQHU ORRS DOZD\V H[HFXWHV WLPHV SURGXFLQJ VWDUV RQ HDFK OLQH RI RXWSXW :LWK WKH QHZ WHVW M L WKH LQQHU ORRS LV JRLQJ WR H[HFXWH L WLPHV %XW L LV FKDQJLQJ ,W WDNHV RQ WKH YDOXHV DQG 6R RQ WKH ILUVW LWHUDWLRQ RI WKH RXWHU ORRS ZKHQ L LV WKH WHVW M L LV HIIHFWHO\ WHVWLQJ M DQG ZH SURGXFH D OLQH ZLWK VWDU RQ LW 2Q WKH VHFRQG LWHUDWLRQ RI WKH RXWHU ORRS ZKHQ L LV WKH WHVW LV HIIHFWLYHO\ WHVWLQJ M DQG ZH SURGXFH D OLQH ZLWK VWDUV RQ LW 2Q WKH WKLUG LWHUDWLRQ RI WKH RXWHU ORRS ZKHQ L LV WKH WHVW LV HIIHFWLYHO\ WHVWLQJ M DQG ZH SURGXFH D OLQH ZLWK VWDUV RQ LW $QG VR RQ ,Q RWKHU ZRUGV WKLV FRGH SURGXFHV D WULDQJOH DV RXWSXW * ** *** **** ***** ****** 0DQDJLQJ &RPSOH[LW\ 1RZ WKDW ZH KDYH VHHQ VHYHUDO QHZ SURJUDPPLQJ FRQVWUXFWV ZH DUH UHDG\ WR SXW WRJHWKHU WR VROYH VRPH FRPSOH[ WDVNV $V ZH SRLQWHG RXW LQ FKDSWHU %ULDQ .HUQLJKDQ FUHDWRUV RI WKH & SURJUDPPLQJ ODQJXDJH KDV VDLG WKDW &RQWUROOLQJ FRPSOH[LW\ LV WKH FRPSXWHU SURJUDPPLQJ ,Q WKLV VHFWLRQ ZH ZLOO H[DPLQH VHYHUDO WHFKQLTXHV WKDW VFLHQWLVWV XVH WR VROYH FRPSOH[ SUREOHPV ZLWKRXW EHLQJ RYHUZKHOPHG E\ FRPSOH[LW\ WKH SLHFHV RQH RI WKH HVVHQFH RI FRPSXWHU 95 6FRSH $V SURJUDPV JHW ORQJHU WKHUH LV DQ LQFUHDVLQJ SRVVLELOLW\ RI GLIIHUHQW SDUW RI WKH SURJUDP LQWHUIHULQJ ZLWK HDFK RWKHU -DYD KHOSV XV WR PDQDJH WKLV SRWHQWLDO SUREOHP E\ HQIRUFLQJ UXOHV RI VFRSH 6FRSH RI D GHFODUDWLRQ 7KH SDUW RI D SURJUDP LQ ZKLFK D SDUWLFXODU GHFODUDWLRQ LV YDOLG :H KDYH VHHQ WKDW ZKHQ LW FRPHV WR GHFODULQJ VWDWLF PHWKRGV ZH FDQ SXW WKHP LQ DQ\ RUGHU ZKDWVRHYHU 7KH VFRSH RI D VWDWLF PHWKRG LV WKH HQWLUH FODVV LQ ZKLFK LW DSSHDUV 9DULDEOHV ZRUN GLIIHUHQWO\ 7KH VLPSOH UXOH LV WKDW WKH VFRSH RI D YDULDEOH GHFODUDWLRQ H[WHQGV IURP WKH SRLQW ZKHUH LW LV GHFODUHG WR WKH ULJKW FXUO\ EUDFH WKDW HQFORVHV LW ,Q RWKHU ZRUGV ILQG WKH SDLU RI FXUO\ EUDFHV WKDW GLUHFWO\ HQFORVH WKH YDULDEOH GHFODUDWLRQ 7KH VFRSH RI WKH YDULDEOH LV IURP WKH SRLQW ZKHUH LW LV GHFODUHG WR WKH FORVLQJ FXUO\ EUDFH 7KHUH DUH VHYHUDO LPSOLFDWLRQV RI WKLV VFRSH UXOH &RQVLGHU ILUVW ZKDW LW PHDQV IRU GLIIHUHQW PHWKRGV (DFK PHWKRG KDV LWV RZQ VHW RI FXUO\ EUDFHV WR LQGLFDWH WKH VWDWHPHQWV WR EH H[HFXWHG ZKHQ WKH PHWKRG LV FDOOHG ,I YDULDEOHV DUH GHFODUHG LQVLGH RI D PHWKRG V FXUO\ EUDFHV WKHQ WKRVH YDULDEOHV ZRQ W EH DYDLODEOH RXWVLGH WKH PHWKRG :H UHIHU WR VXFK YDULDEOHV DV ORFDO YDULDEOHV DQG ZH UHIHU WR WKLV SURFHVV DV ORFDOL]LQJ YDULDEOHV ,Q JHQHUDO ZH ZDQW WR GHFODUH YDULDEOHV LQ WKH PRVW ORFDO VFRSH SRVVLEOH /RFDO 9DULDEOH $ YDULDEOH GHFODUHG LQVLGH D PHWKRG WKDW LV DFFHVVLEOH RQO\ LQ WKDW PHWKRG /RFDOL]LQJ 9DULDEOHV 'HFODULQJ YDULDEOHV LQ WKH LQQHUPRVW PRVW ORFDO VFRSH SRVVLEOH <RX PLJKW ZRQGHU ZK\ ZH ZRXOG ZDQW WR ORFDOL]H YDULDEOHV WR MXVW RQH PHWKRG :K\ QRW MXVW GHFODUH HYHU\WKLQJ LQ RQH RXWHU VFRSH" 7KDW FHUWDLQO\ VHHPV VLPSOHU 7KH LGHD LV VLPLODU WR WKH XVH RI UHIULJHUDWRUV LQ GRUPLWRULHV (YHU\ GRUP URRP FDQ KDYH LWV RZQ UHIULJHUDWRU IRU XVH LQ WKDW URRP ,I \RX DUH RXWVLGH RI D URRP \RX GRQ W HYHQ NQRZ WKDW LW KDV D UHIULJHUDWRU LQ LW 7KH FRQWHQWV RI WKH URRP DUH KLGGHQ IURP \RX /RFDOL]LQJ YDULDEOHV OHDGV WR VRPH GXSOLFDWLRQ DQG FRQIXVLRQ EXW SURYLGHV PRUH VHFXULW\ 2XU SURJUDPV XVH YDULDEOHV WR VWRUH YDOXHV MXVW DV VWXGHQWV XVH UHIULJHUDWRUV WR VWRUH EHHU LFH FUHDP DQG RWKHU YDOXDEOHV 7KH ODVW WLPH , ZDV LQ D GRUP , QRWLFHG WKDW PRVW RI WKH LQGLYLGXDO URRPV KDG UHIULJHUDWRUV LQ WKHP 7KLV VHHPV WHUULEO\ UHGXQGDQW EXW WKH UHDVRQ LV REYLRXV ,I \RX ZDQW WR JXDUDQWHH WKH VHFXULW\ RI VRPHWKLQJ \RX SXW LW ZKHUH QRERG\ HOVH FDQ JHW LW <RX ZLOO XVH ORFDO YDULDEOHV LQ PXFK WKH VDPH ZD\ (DFK LQGLYLGXDO PHWKRG ZLOO KDYH LWV RZQ ORFDO YDULDEOHV WR XVH ZKLFK PHDQV \RX GRQ W KDYH WR FRQVLGHU SRVVLEOH LQWHUIHUHQFH IURP RWKHU SDUWV RI WKH SURJUDP /HW V ORRN DW D VLPSOH H[DPSOH LQYROYLQJ WZR PHWKRGV 96 1 2 3 4 5 6 7 8 9 10 11 12 13 // This program does not compile. public class ScopeExample { public static void main(String args) { int x = 3; int y = 7; computeSum(); } public static void computeSum() { int sum = x + y; // illegal because x and y are not in this scope System.out.println("sum = " + sum); } } ,Q WKLV H[DPSOH WKH PDLQ PHWKRG GHFODUHV ORFDO YDULDEOHV [ DQG \ DQG JLYHV WKHP LQLWLDO YDOXHV 7KHQ LW FDOOV PHWKRG FRPSXWH6XP ,QVLGH RI PHWKRG FRPSXWH6XP ZH WU\ WR PDNH XVH RI WKH YDOXHV RI [ DQG \ WR FRPSXWH D VXP %XW WKLV GRHVQ W ZRUN 7KH YDULDEOHV [ DQG \ DUH ORFDO WR WKH PDLQ PHWKRG DQG DUHQ W YLVLEOH LQVLGH RI WKH FRPSXWH6XP PHWKRG ,Q WKH QH[W FKDSWHU ZH ZLOO VHH D WHFKQLTXH IRU DOORZLQJ RQH PHWKRG WR SDVV D YDOXH WR DQRWKHU 7KH SURJUDP SURGXFHV HUURU PHVVDJHV OLNH WKH IROORZLQJ ScopeExample.java:10: cannot symbol : variable x location: class ScopeExample int sum = x + y; // ^ ScopeExample.java:10: cannot symbol : variable y location: class ScopeExample int sum = x + y; // ^ find symbol illegal because x and y are not in this scope find symbol illegal because x and y are not in this scope 6FRSH UXOHV DUH LPSRUWDQW WR XQGHUVWDQG ZKHQ ZH WDON DERXW WKH ORFDO YDULDEOHV RI RQH PHWKRG YHUVXV DQRWKHU PHWKRG EXW WKH\ DOVR KDYH LPSOLFDWLRQV IRU ZKDW KDSSHQV LQVLGH D VLQJOH PHWKRG :H KDYH VHHQ WKDW FXUO\ EUDFHV DUH XVHG WR JURXS WRJHWKHU D VHULHV RI VWDWHPHQWV %XW ZH FDQ KDYH FXUO\ EUDFHV LQVLGH RI FXUO\ EUDFHV DQG WKLV OHDGV WR VRPH VFRSH LVVXHV )RU H[DPSOH FRQVLGHU WKH IROORZLQJ FRGH for (int i = 1; i <= 5; i++) { int squared = i * i; System.out.println(i + " squared = " + squared); } 7KLV LV D YDULDWLRQ RI WKH FRGH ZH ORRNHG DW HDUOLHU LQ WKH FKDSWHU WR SULQW RXW WKH VTXDUHV RI WKH ILUVW LQWHJHUV ,Q WKLV YHUVLRQ ZH XVH D YDULDEOH FDOOHG squared WR NHHS WUDFN RI WKH VTXDUH RI WKH IRU ORRS FRQWURO YDULDEOH 7KLV FRGH ZRUNV ILQH EXW FRQVLGHU WKLV YDULDWLRQ for (int i = 1; i <= 5; i++) { int squared = i * i; System.out.println(i + " squared = " + squared); } System.out.println("Last square = " + squared); // illegal 97 7KLV FRGH JHQHUDWHV D FRPSLOHU HUURU 7KH YDULDEOH FDOOHG VTXDUHG LV GHFODUHG LQVLGH WKH IRU ORRS ,Q RWKHU ZRUGV WKH FXUO\ EUDFHV WKDW FRQWDLQ LW DUH WKH FXUO\ EUDFHV IRU WKH ORRS ,W FDQ W EH XVHG RXWVLGH WKLV VFRSH 6R ZKHQ ZH DWWHPSW WR UHIHU WR LW RXWVLGH WKH ORRS ZH JHW D FRPSLOHU HUURU ,I IRU VRPH UHDVRQ \RX QHHG WR ZULWH FRGH OLNH WKLV WKDW DFFHVVHV WKH YDULDEOH DIWHU WKH ORRS \RX G KDYH WR GHFODUH WKH YDULDEOH LQ WKH RXWHU VFRSH EHIRUH WKH ORRS int squared; // declaration is now in outer scope for (int i = 1; i <= 5; i++) { squared = i * i; // change this to an assignment statement System.out.println(i + " squared = " + squared); } System.out.println("Last square = " + squared); // now legal 7KHUH DUH D IHZ VSHFLDO FDVHV IRU VFRSH DQG WKH IRU ORRS LV RQH RI WKHP :KHQ D YDULDEOH LV GHFODUHG LQ WKH LQLWLDOL]DWLRQ SDUW RI D IRU ORRS WKHQ LWV VFRSH LV MXVW WKH IRU ORRS LWVHOI WKH SDUWV LQ WKH IRU ORRS KHDGHU DQG WKH VWDWHPHQWV FRQWUROOHG E\ WKH IRU ORRS 7KDW PHDQV WKDW \RX FDQ XVH WKH VDPH YDULDEOH QDPH LQ PXOWLSOH IRU ORRSV for (int i = 1; i <= 10; System.out.println(i } for (int i = 1; i <= 10; System.out.println(i } i++) { + " squared = " + (i * i)); i++) { + " cubed = " + (i * i * i)); 7KH YDULDEOH L LV GHFODUHG WZLFH EXW EHFDXVH WKH VFRSH RI HDFK LV MXVW WKH IRU ORRS LQ ZKLFK LW LV GHFODUHG WKLV LVQ W D SUREOHP 7KLV LV OLNH KDYLQJ WZR GRUP URRPV HDFK ZLWK LWV RZQ UHIULJHUDWRU 2I FRXUVH ZH FDQ W GR WKLV ZLWK QHVWHG IRU ORRSV for (int i = 1; i <= 5; i++) { for (int i = 1; i <= 10; i++) { System.out.println("hi there."); } } 7KLV FRGH ZRQ W FRPSLOH :KHQ -DYD HQFRXQWHUV WKH LQQHU IRU ORRS LW ZLOO FRPSODLQ WKDW WKH YDULDEOH L KDV DOUHDG\ EHHQ GHFODUHG ZLWKLQ WKLV VFRSH <RX DUHQ W DOORZHG WR GHFODUH WKH VDPH YDULDEOH WZLFH ZLWKLQ WKH VDPH VFRSH <RX G KDYH WR FRPH XS ZLWK WZR GLIIHUHQW QDPHV 7KLV SUDFWLFH LV VLPLODU WR ZKDW IDPLOLHV GR ZKHQ WKH\ KDYH WZR SHRSOH ZLWK WKH VDPH QDPH 7KH IDPLOLHV FKDQJH WKH QDPHV WR EH DEOH WR GLVWLQJXLVK EHWZHHQ WKH WZR SHRSOH 6R \RX HQG XS ZLWK &DUO -XQLRU DQG &DUO 6HQLRU RU *UDQGPD -RQHV DQG *UDQGPD 6PLWK WR DYRLG DQ\ SRWHQWLDO FRQIXVLRQ ,Q DOO RI WKH IRU ORRS H[DPSOHV ZH KDYH ORRNHG DW WKH FRQWURO YDULDEOH LV GHFODUHG LQ WKH LQLWLDOL]DWLRQ SDUW RI WKH ORRS 7KLV LVQ W D UHTXLUHPHQW <RX FDQ VHSDUDWH WKH GHFODUDWLRQ RI WKH IRU ORRS FRQWURO YDULDEOH IURP WKH LQLWLDOL]DWLRQ RI WKH YDULDEOH DV LQ WKH IROORZLQJ int i; for (i = 1; i <= 5; i++) { System.out.println(i + " squared = " + (i * i)); } 98 ,Q HIIHFW ZH KDYH WDNHQ WKH int i SDUW RXW RI WKH ORRS LWVHOI DQG SXW LW RXWVLGH WKH ORRS 7KLV FKDQJH PHDQV WKDW WKH YDULDEOH V VFRSH LV JUHDWHU WKDQ LW ZDV EHIRUH ,WV VFRSH H[WHQGV WR WKH HQG RI WKH HQFORVLQJ VHW RI FXUO\ EUDFHV 2QH DGYDQWDJH RI WKLV DSSURDFK LV WKDW ZH FDQ UHIHU WR WKH ILQDO YDOXH RI WKH FRQWURO YDULDEOH DIWHU WKH ORRS 1RUPDOO\ ZH ZRXOGQ W EH DEOH WR GR VR EHFDXVH LWV VFRSH ZRXOG EH OLPLWHG WR WKH ORRS LWVHOI %XW GHFODULQJ WKH FRQWURO YDULDEOH RXWVLGH WKH ORRS LV D GDQJHURXV SUDFWLFH WKDW \RX ZLOO JHQHUDOO\ ZDQW WR DYRLG ,W DOVR SURYLGHV D JRRG H[DPSOH RI WKH SUREOHPV \RX FDQ HQFRXQWHU ZKHQ \RX GRQ W ORFDOL]H YDULDEOHV &RQVLGHU WKH IROORZLQJ FRGH IRU H[DPSOH int i; for (i = 1; i <= 5; i++) { for (i = 1; i <= 10; i++) { System.out.println("hi there."); } } $V QRWHG DERYH \RX VKRXOGQ W XVH WKH VDPH FRQWURO YDULDEOH ZKHQ \RX KDYH QHVWHG ORRSV %XW XQOLNH WKH SUHYLRXV H[DPSOH WKLV RQH DFWXDOO\ FRPSLOHV 6R LQVWHDG RI JHWWLQJ D KHOSIXO HUURU PHVVDJH IURP WKH -DYD FRPSLOHU \RX JHW D SURJUDP ZLWK D EXJ LQ LW <RX G WKLQN IURP UHDGLQJ WKHVH ORRSV WKDW LW SURGXFHV OLQHV RI RXWSXW EXW LW DFWXDOO\ SURGXFHV MXVW OLQHV RI RXWSXW 7KH LQQHU ORRS LQFUHPHQWV WKH YDULDEOH L XQWLO LW EHFRPHV DQG WKDW FDXVHV WKH RXWHU ORRS WR WHUPLQDWH DIWHU MXVW RQH LWHUDWLRQ ,W FDQ EH HYHQ ZRUVH ,I \RX UHYHUVH WKH RUGHU RI WKHVH ORRSV int i; for (i = 1; i <= 10; i++) { for (i = 1; i <= 5; i++) { System.out.println("hi there."); } } <RX JHW VRPHWKLQJ NQRZQ DV DQ LQILQLWH ORRS ,QILQLWH /RRS $ ORRS WKDW QHYHU WHUPLQDWHV 7KLV ORRS LV LQILQLWH EHFDXVH QR PDWWHU ZKDW WKH RXWHU ORRS GRHV WR WKH YDULDEOH L WKH LQQHU ORRS DOZD\V VHWV LW EDFN WR DQG LWHUDWHV XQWLO LW EHFRPHV 7KHQ WKH RXWHU ORRS LQFUHPHQWV WKH YDULDEOH WR DQG ILQGV WKDW LV OHVV WKDQ RU HTXDO WR VR LW DOZD\V JRHV EDFN WR WKH LQQHU ORRS ZKLFK RQFH DJDLQ VHWV WKH YDULDEOH EDFN WR DQG LWHUDWHV XS WR 7KLV SURFHVV JRHV RQ LQGHILQLWHO\ 7KHVH DUH WKH NLQGV RI LQWHUIHUHQFH SUREOHPV \RX FDQ JHW ZKHQ \RX IDLO WR ORFDOL]H YDULDEOHV &RPPRQ 3URJUDPPLQJ (UURU 5HIHUULQJ WR WKH ZURQJ ORRS YDULDEOH 7KH IROORZLQJ FRGH DWWHPSWV WR SULQW D WULDQJOH RI VWDUV +RZHYHU LW KDV D VXEWOH EXJ WKDW FDXVHV LW WR SULQW VWDUV LQILQLWHO\ 99 for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; i++) { System.out.print("*"); } System.out.println(); } 7KH SUREOHP LV RQ WKH VHFRQG OLQH LQ WKH VHFRQG IRU ORRS KHDGHU V XSGDWH VWDWHPHQW 7KH SURJUDPPHU PHDQW WR ZULWH j++ EXW LQVWHDG DFFLGHQWDOO\ ZURWH i++ $ WUDFH RI WKH FRGH ORRNV OLNH WKLV initialization initialization test body update test body update ... int i = 1; int j = 1; j <= i {...} i++ j <= i {...} i++ ... variable i is allocated and initialized to 1 variable j is allocated and initialized to 1 true because 1 <= 1, so we enter the inner loop execute the print with j equal to 1 increment i, which becomes 2 true because 1 <= 2, so we enter the inner loop execute the print with j equal to 1 increment i, which becomes 3 ... 7KH M YDULDEOH VKRXOG EH LQFUHDVLQJ EXW L LV LQVWHDG 7KH HIIHFW RI WKLV PLVWDNH LV WKDW WKH YDULDEOH M LV QHYHU LQFUHPHQWHG LQ WKH LQQHU ORRS DQG WKHUHIRUH WKH WHVW RI j <= i QHYHU IDLOV VR WKH LQQHU ORRS GRHVQ W WHUPLQDWH +HUH V DQRWKHU EURNHQ SLHFH RI FRGH 7KLV RQH WULHV WR SULQW D [ ER[ RI VWDUV EXW LW DOVR SULQWV LQILQLWHO\ for (int i = 1; i <= 6; i++) { for (int j = 1; i <= 4; j++) { System.out.print("*"); } System.out.println(); } 7KH SUREOHP LV RQ WKH VHFRQG OLQH WKLV WLPH LQ WKH LQQHU IRU ORRS KHDGHU V WHVW 7KH SURJUDPPHU PHDQW WR ZULWH j <= 4 EXW LQVWHDG DFFLGHQWDOO\ ZURWH i <= 4 6LQFH WKH YDOXH RI L LV QHYHU LQFUHPHQWHG LQ WKH LQQHU ORRS WKH WHVW RI i <= 4 QHYHU IDLOV VR WKH LQQHU ORRS DJDLQ GRHVQ W WHUPLQDWH 3VHXGRFRGH $V ZH ZULWH PRUH FRPSOH[ DOJRULWKPV ZH ZLOO ILQG WKDW ZH FDQ W MXVW ZULWH WKH HQWLUH DOJRULWKP LPPHGLDWHO\ ,QVWHDG ZH ZLOO LQFUHDVLQJO\ PDNH XVH RI WKH WHFKQLTXH RI ZULWLQJ SVHXGRFRGH 3VHXGRFRGH (QJOLVK OLNH GHVFULSWLRQV RI DOJRULWKPV 3URJUDPPLQJ ZLWK SVHXGRFRGH LQYROYHV VXFFHVVLYHO\ UHILQLQJ DQ LQIRUPDO GHVFULSWLRQ XQWLO LW LV HDVLO\ WUDQVODWHG LQWR -DYD )RU H[DPSOH \RX FDQ GHVFULEH WKH SUREOHP RI GUDZLQJ D ER[ DV 100 draw a box with 50 lines and 30 columns of asterisks. :KLOH WKLV GHVFULEHV WKH ILJXUH LW LV QRW VSHFLILF DERXW KRZ WR GUDZ LW ZKDW DOJRULWKP WR XVH 'R \RX GUDZ WKH ILJXUH OLQH E\ OLQH RU FROXPQ E\ FROXPQ" ,Q -DYD ILJXUHV OLNH WKHVH PXVW EH JHQHUDWHG OLQH E\ OLQH EHFDXVH RQFH D SULQWOQ KDV EHHQ SHUIRUPHG RQ D OLQH RI RXWSXW WKDW OLQH FDQQRW EH FKDQJHG 7KHUH LV QR FRPPDQG IRU JRLQJ EDFN WR D SUHYLRXV OLQH LQ DQ RXWSXW ILOH 7KHUHIRUH WKH ILUVW OLQH PXVW EH RXWSXW LQ LWV HQWLUHW\ ILUVW WKHQ WKH VHFRQG OLQH LQ LWV HQWLUHW\ DQG VR RQ $V D UHVXOW \RXU GHFRPSRVLWLRQV IRU WKHVH ILJXUHV ZLOO EH OLQH RULHQWHG DW WKH WRS OHYHO 7KXV D FORVHU DSSUR[LPDWLRQ LV for (each of 50 lines) { draw a line of 30 asterisks. } (YHQ WKLV FDQ EH PDGH PRUH VSHFLILF E\ LQWURGXFLQJ WKH LGHD RI ZULWLQJ D VLQJOH FKDUDFWHU RQ WKH RXWSXW OLQH YHUVXV PRYLQJ WR D QHZ OLQH RI RXWSXW for (each of 50 lines) { for (each of 30 columns) { write one asterisk on the output line. } go to a new output line. } 8VLQJ SVHXGRFRGH \RX FDQ JUDGXDOO\ FRQYHUW DQ (QJOLVK GHVFULSWLRQ LQWR VRPHWKLQJ HDVLO\ WUDQVODWHG LQWR D -DYD SURJUDP 7KH VLPSOH H[DPSOHV \RX KDYH VHHQ VR IDU DUH KDUGO\ ZRUWK WKH DSSOLFDWLRQ RI SVHXGRFRGH VR ZH ZLOO QRZ H[DPLQH WKH SUREOHP RI JHQHUDWLQJ D PRUH FRPSOH[ ILJXUH ********* ******* ***** *** * 7KLV ILJXUH PXVW DOVR EH JHQHUDWHG OLQH E\ OLQH for (each of 5 lines) { draw one line of the triangle. } 8QIRUWXQDWHO\ HDFK OLQH LV GLIIHUHQW 7KHUHIRUH \RX PXVW FRPH XS ZLWK D JHQHUDO UXOH WKDW ILWV DOO OLQHV 7KH ILUVW OLQH RI WKLV ILJXUH KDV D VHULHV RI DVWHULVNV RQ LW ZLWK QR OHDGLQJ VSDFHV 7KH VXEVHTXHQW OLQHV KDYH D VHULHV RI VSDFHV IROORZHG E\ D VHULHV RI DVWHULVNV 8VLQJ \RXU LPDJLQDWLRQ D ELW \RX FDQ VD\ WKDW WKH ILUVW OLQH KDV VSDFHV RQ LW IROORZHG E\ D VHULHV RI DVWHULVNV 7KLV DOORZV \RX WR ZULWH D JHQHUDO UXOH IRU PDNLQJ WKLV ILJXUH for (each write write go to } of 5 lines) { some spaces (possibly 0) on the output line. some asterisks on the output line. a new output line. 101 ,Q RUGHU WR SURFHHG \RX PXVW GHWHUPLQH D UXOH IRU WKH QXPEHU RI VSDFHV DQG D UXOH IRU WKH QXPEHU RI DVWHULVNV $VVXPLQJ WKDW WKH OLQHV DUH QXPEHUHG WKURXJK DQG ORRNLQJ DW WKH ILJXUH \RX FDQ ILOO LQ WKH IROORZLQJ FKDUW /LQH 6SDFHV $VWHULVNV <RX ZDQW WR ILQG D UHODWLRQVKLS EHWZHHQ OLQH QXPEHU DQG WKH RWKHU WZR FROXPQV 7KLV LV VLPSOH DOJHEUD EHFDXVH WKHVH FROXPQV DUH UHODWHG LQ D OLQHDU ZD\ 7KH VHFRQG FROXPQ LV HDV\ WR JHW IURP WKH OLQH QXPEHU ,W HTXDOV OLQH 7KH WKLUG FROXPQ LV D OLWWOH WRXJKHU %HFDXVH LW JRHV GRZQ E\ HYHU\ WLPH DQG WKH ILUVW FROXPQ JRHV XS E\ HYHU\ WLPH \RX QHHG D PXOWLSOLHU RI 7KHQ \RX QHHG DQ DSSURSULDWH FRQVWDQW 7KH QXPEHU VHHPV WR GR WKH WULFN VR WKDW WKH WKLUG FROXPQ HTXDOV OLQH <RX FDQ LPSURYH \RXU SVHXGRFRGH WKHQ DV IROORZV for line going 1 to 5 { write (line - 1) spaces on the output line. write (11 - 2 * line) asterisks on the output line. go to a new output line. } 7KLV LV VLPSOH WR WXUQ LQWR D SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 public class DrawV { public static void main(String args) { for (int line = 1; line <= 5; line++) { for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } for (int i = 1; i <= (11 - 2 * line); i++) { System.out.print("*"); } System.out.println(); } } } $ 'HFUHPHQWLQJ IRU /RRS 6RPHWLPHV ZH PDQDJH FRPSOH[LW\ E\ WDNLQJ DGYDQWDJH RI ZRUN WKDW ZH KDYH DOUHDG\ GRQH )RU H[DPSOH KRZ ZRXOG \RX SURGXFH WKLV ILJXUH" * *** ***** ******* ********* 102 <RX FRXOG IROORZ WKH VDPH SURFHVV \RX GLG DERYH DQG ILQG QHZ H[SUHVVLRQV WKDW SURGXFH WKH DSSURSULDWH QXPEHU RI VSDFHV DQG DVWHULVNV +RZHYHU WKHUH LV DQ HDVLHU ZD\ 7KLV ILJXUH LV WKH VDPH DV WKH SUHYLRXV RQH H[FHSW WKH OLQHV DSSHDU LQ UHYHUVH RUGHU :H FDQ DFKLHYH WKLV UHVXOW E\ UXQQLQJ WKH IRU ORRS EDFNZDUGV 6R LQVWHDG RI VWDUWLQJ DW DQG JRLQJ XS WR ZLWK D ++ XSGDWH ZH FDQ VWDUW DW DQG JR GRZQ WR XVLQJ D -- XSGDWH )RU H[DPSOH WKH IROORZLQJ ORRS for (int i = 10; i >= 1; i--) { System.out.println(i + " squared = " + (i * i)); } ZLOO SURGXFH WKH VTXDUHV RI WKH ILUVW WHQ LQWHJHUV EXW LQ UHYHUVH RUGHU 7KH VLPSOH ZD\ WR SURGXFH WKH XSZDUG SRLQWLQJ WULDQJOH WKHQ LV 1 2 3 4 5 6 7 8 9 10 11 12 13 public class DrawCone { public static void main(String args) { for (int line = 5; line >= 1; line--) { for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } for (int i = 1; i <= (11 - 2 * line); i++) { System.out.print("*"); } System.out.println(); } } } &ODVV &RQVWDQWV 7KH 'UDZ&RQH SURJUDP LQ WKH ODVW VHFWLRQ GUDZV D FRQH ZLWK OLQHV +RZ ZRXOG \RX PRGLI\ LW WR SURGXFH D FRQH ZLWK OLQHV" 2QH VLPSOH VWUDWHJ\ LV WR FKDQJH DOO WKH V WR V ZKLFK ZLOO SURGXFH WKLV RXWSXW ***** ******* ********* 7KLV RXWSXW LV REYLRXVO\ ZURQJ ,I \RX ZRUN WKURXJK WKH JHRPHWU\ RI WKH ILJXUH \RX ZLOO GLVFRYHU WKDW WKH SUREOHP LV ZLWK WKH QXPEHU LQ RQH RI WKH H[SUHVVLRQV 7KH QXPEHU FRPHV IURP WKLV IRUPXOD 2 * (number of lines) + 1 7KXV IRU OLQHV WKH DSSURSULDWH YDOXH LV %XW IRU OLQHV WKH DSSURSULDWH YDOXH LV 3URJUDPPHUV FDOO QXPEHUV OLNH WKHVH PDJLF QXPEHUV 7KH\ DUH PDJLF LQ WKH VHQVH WKDW WKH\ VHHP WR PDNH WKH SURJUDP ZRUN EXW WKHLU GHILQLWLRQ LV QRW DOZD\V REYLRXV *ODQFLQJ DW WKH SURJUDP RQH LV DSW WR DVN :K\ " :K\ " :K\ " :K\ " :K\ PH" 7R PDNH SURJUDPV PRUH UHDGDEOH DQG PRUH DGDSWDEOH \RX VKRXOG WU\ WR DYRLG PDJLF QXPEHUV ZKHQHYHU SRVVLEOH <RX GR VR E\ VWRULQJ WKH PDJLF QXPEHUV <RX FDQ XVH YDULDEOHV WR VWRUH WKHVH YDOXHV EXW WKDW LV PLVOHDGLQJ JLYHQ WKDW \RX DUH WU\LQJ WR UHSUHVHQW YDOXHV WKDW GRQ W FKDQJH -DYD 103 RIIHUV DQ DOWHUQDWLYH <RX FDQ GHFODUH YDOXHV WKDW DUH VLPLODU WR YDULDEOHV H[FHSW IRU WKH IDFW WKDW WKH\ DUH JXDUDQWHHG WR KDYH FRQVWDQW YDOXH 1RW VXUSULVLQJO\ WKH\ DUH FDOOHG FRQVWDQWV :H PRVW RIWHQ GHILQH FODVV FRQVWDQWV ZKLFK FDQ EH DFFHVVHG WKURXJKRXW WKH HQWLUH FODVV &RQVWDQW &ODVV &RQVWDQW $ QDPHG YDOXH WKDW FDQQRW EH FKDQJHG $ FODVV FRQVWDQW FDQ EH DFFHVVHG DQ\ZKHUH LQ WKH FODVV L H LWV VFRSH LV WKH HQWLUH FODVV 7KH ILUVW DGYDQWDJH RI D FRQVWDQW LV WKDW \RX FDQ QDPH LW ZKLFK DOORZV \RX WR FKRRVH D GHVFULSWLYH QDPH WKDW H[SODLQV ZKDW WKH FRQVWDQW UHSUHVHQWV <RX FDQ WKHQ XVH WKDW QDPH LQVWHDG RI UHIHUULQJ WR WKH VSHFLILF YDOXH WR PDNH \RXU SURJUDPV PRUH UHDGDEOH DQG DGDSWDEOH )RU H[DPSOH LQ WKH 'UDZ&RQH SURJUDP \RX PLJKW ZDQW WR LQWURGXFH D FRQVWDQW FDOOHG /,1(6 WKDW ZLOO UHSODFH WKH PDJLF QXPEHU UHFDOO IURP FKDSWHU WKDW ZH XVH DOO XSSHUFDVH OHWWHUV IRU FRQVWDQW QDPHV $OVR \RX FDQ XVH WKH FRQVWDQW DV SDUW RI DQ H[SUHVVLRQ WR FDOFXODWH D YDOXH 7KLV DSSURDFK DOORZV \RX WR UHSODFH WKH PDJLF QXPEHU ZLWK D IRUPXOD OLNH /,1(6 &RQVWDQWV DUH GHFODUHG ZLWK WKH NH\ZRUG final ZKLFK LQGLFDWHV WKH IDFW WKDW WKHLU YDOXHV FDQQRW EH FKDQJHG RQFH DVVLJQHG <RX FDQ GHFODUH WKHP DQ\ZKHUH \RX FDQ GHFODUH D YDULDEOH DV LQ final int LINES = 5; 7KHVH YDOXHV FDQ EH GHFODUHG LQVLGH D PHWKRG MXVW OLNH D YDULDEOH EXW WKH\ DUH RIWHQ XVHG E\ VHYHUDO GLIIHUHQW PHWKRGV $V D UHVXOW ZH JHQHUDOO\ GHFODUH FRQVWDQWV RXWVLGH RI PHWKRGV 7KLV UHTXLUHPHQW FDXVHV XV WR KDYH DQRWKHU UXQ LQ ZLWK RXU ROG SDO WKH static NH\ZRUG ,I ZH ZDQW WR GHFODUH D FRQVWDQW WKDW RXU VWDWLF PHWKRGV FDQ DFFHVV WKHQ WKH FRQVWDQW LWVHOI KDV WR EH VWDWLF $QG MXVW DV ZH GHFODUH RXU PHWKRGV WR EH SXEOLF ZH XVXDOO\ GHFODUH RXU FRQVWDQWV WR EH SXEOLF 7KH IROORZLQJ LV WKH JHQHUDO V\QWD[ IRU FRQVWDQW GHILQLWLRQV public static final <type> <identifier> = <expression>; )RU H[DPSOH KHUH DUH GHILQLWLRQV IRU WZR FRQVWDQWV public static final int HEIGHT = 10; public static final int WIDTH = 20; 7KHVH GHILQLWLRQV FUHDWH FRQVWDQWV FDOOHG +(,*+7 DQG :,'7+ WKDW ZLOO DOZD\V KDYH WKH YDOXHV DQG 7KHVH DUH NQRZQ DV FODVV FRQVWDQWV EHFDXVH ZH GHFODUH WKHP LQ WKH RXWHUPRVW VFRSH RI WKH FODVV DORQJ ZLWK WKH PHWKRGV RI WKH FODVV 7KDW ZD\ WKH\ DUH YLVLEOH LQ HDFK PHWKRG RI WKH FODVV +RZ ZRXOG \RX UHZULWH WKH 'UDZ&RQH SURJUDP ZLWK D FRQVWDQW WR HOLPLQDWH WKH PDJLF QXPEHUV" <RX ZRXOG LQWURGXFH D FRQVWDQW IRU WKH QXPEHU RI OLQHV public static final int LINES = 5; 1H[W \RX UHSODFH WKH LQ WKH RXWHU ORRS ZLWK WKLV FRQVWDQW 7KHQ \RX UHSODFH WKH LQQHU ORRS ZLWK WKH H[SUHVVLRQ /,1(6 7KH UHVXOW LV WKH IROORZLQJ SURJUDP LQ WKH VHFRQG 104 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class DrawCone2 { public static final int LINES = 5; public static void main(String args) { for (int line = LINES; line >= 1; line--) { for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } for (int i = 1; i <= (2 * LINES + 1 - 2 * line); i++) { System.out.print("*"); } System.out.println(); } } } 7KH DGYDQWDJH RI WKLV SURJUDP LV WKDW LW LV PRUH UHDGDEOH DQG PRUH DGDSWDEOH <RX FDQ PDNH D VLPSOH FKDQJH WR WKH FRQVWDQW /,1(6 WR PDNH LW SURGXFH D GLIIHUHQW VL]H ILJXUH &DVH 6WXG\ $ &RPSOH[ )LJXUH 1RZ FRQVLGHU DQ H[DPSOH WKDW LV HYHQ PRUH FRPSOH[ 7R VROYH LW ZH ZLOO JR WKURXJK WKUHH EDVLF VWHSV 'HFRPSRVH WKH WDVN LQWR VXEWDVNV HDFK RI ZKLFK ZLOO EHFRPH D VWDWLF PHWKRG )RU HDFK VXEWDVN PDNH D WDEOH IRU WKH ILJXUH DQG FRPSXWH IRUPXODV IRU HDFK FROXPQ RI WKH WDEOH LQ WHUPV RI WKH OLQH QXPEHU &RQYHUW WKH WDEOHV LQWR DFWXDO IRU ORRS FRGH IRU HDFK PHWKRG 7KH RXWSXW ZH ZDQW WR SURGXFH LV WKH IROORZLQJ +------+ |\ /| | \ / | | \/ | | /\ | | / \ | |/ \| +------+ 3UREOHP 'HFRPSRVLWLRQ DQG 3VHXGRFRGH ,Q RUGHU WR JHQHUDWH WKLV ILJXUH \RX KDYH WR ILUVW EUHDN LW GRZQ LQWR VXEILJXUHV ,Q GRLQJ VR \RX VKRXOG ORRN IRU OLQHV WKDW DUH VLPLODU LQ RQH ZD\ RU DQRWKHU 7KH ILUVW DQG ODVW OLQHV DUH H[DFWO\ WKH VDPH 7KH WKUHH OLQHV DIWHU WKH ILUVW OLQH DOO ILW RQH SDWWHUQ DQG WKH WKUHH OLQHV DIWHU WKDW ILW DQRWKHU 105 +------+ |\ /| | \ / | | \/ | | /\ | | / \ | |/ \| +------+ line top half bottom half line 7KXV \RX FDQ EUHDN WKH RYHUDOO SUREOHP GRZQ DV draw draw draw draw a solid line. the top half of the hourglass. the bottom half of the hourglass. a solid line. <RX VKRXOG VROYH HDFK LQGHSHQGHQWO\ (YHQWXDOO\ ZH ZLOO ZDQW WR LQFRUSRUDWH D FODVV FRQVWDQW WR PDNH WKH SURJUDP PRUH IOH[LEOH EXW OHW V ILUVW VROYH WKH SUREOHP ZLWKRXW ZRUU\LQJ DERXW PDJLF QXPEHUV 7KH VROLG OLQH WDVN FDQ EH IXUWKHU VSHFLILHG DV write write write go to a 6 a a plus on the output line. dashes on the output line. plus on the output line. new output line. 7KLV WUDQVODWHV HDVLO\ LQWR D VWDWLF PHWKRG // Produces a solid line public static void drawLine() { System.out.print("+"); for (int i = 1; i <= 6; i++) { System.out.print("-"); } System.out.println("+"); } /LQH 3DWWHUQ 7DEOH 7KH WRS KDOI RI WKH KRXUJODVV LV PRUH FRPSOH[ +HUH LV D W\SLFDO OLQH | \ / | 7KLV KDV IRXU SULQWLQJ FKDUDFWHUV DQG VRPH VSDFHV WKDW VHSDUDWH WKHP | bar spaces \ backslash spaces / slash spaces | bar 7KXV D ILUVW DSSUR[LPDWLRQ LQ SVHXGRFRGH 106 for (each write write write write write write write go to } of 3 lines) { a bar on the output line. some spaces on the output line. a back slash on the output line. some spaces on the output line. a slash on the output line. some spaces on the output line. a bar on the output line. a new line of output. $JDLQ \RX FDQ PDNH D WDEOH WR ILJXUH RXW WKH GHVLUHG H[SUHVVLRQV :ULWLQJ WKH VLQJOH FKDUDFWHUV ZLOO EH HDV\ HQRXJK WR WUDQVODWH LQWR -DYD EXW \RX QHHG WR EH PRUH VSHFLILF DERXW WKH VSDFHV 7KLV OLQH UHDOO\ KDV WKUHH VHWV RI VSDFHV +HUH LV D WDEOH WKDW VKRZV KRZ PDQ\ WR XVH LQ HDFK FDVH /LQH 6SDFHV 6SDFHV 6SDFHV 7KH ILUVW DQG WKLUG VHWV RI VSDFHV ILW WKH UXOH OLQH OLQH 7KHUHIRUH WKH SVHXGRFRGH VKRXOG UHDG for (line write write write write write write write go to } DQG WKH VHFRQG QXPEHU RI VSDFHV LV going 1 to 3) { a bar on the output line. (line - 1) spaces on the output line. a back slash on the output line. (6 - 2 * line) spaces on the output line. a slash on the output line. (line - 1) spaces on the output line. a bar on the output line. a new line of output. ,QLWLDO 6WUXFWXUHG 9HUVLRQ 2XU SVHXGRFRGH IRU WKH WRS KDOI RI WKH KRXUJODVV LV HDVLO\ WUDQVODWHG LQWR D VWDWLF PHWKRG QDPHG GUDZ7RS $ VLPLODU VROXWLRQ H[LVWV IRU WKH ERWWRP KDOI RI WKH KRXUJODVV 3XW WRJHWKHU WKH SURJUDP ORRNV OLNH WKLV 107 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public class DrawFigure { public static void main(String args) { drawLine(); drawTop(); drawBottom(); drawLine(); } // Produces a solid line public static void drawLine() { System.out.print("+"); for (int i = 1; i <= 6; i++) { System.out.print("-"); } System.out.println("+"); } // This produces the top half of the hourglass figure public static void drawTop() { for (int line = 1; line <= 3; line++) { System.out.print("|"); for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } System.out.print("\\"); for (int i = 1; i <= (6 - 2 * line); i++) { System.out.print(" "); } System.out.print("/"); for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } System.out.println("|"); } } // This produces the bottom half of the hourglass figure public static void drawBottom() { for (int line = 1; line <= 3; line++) { System.out.print("|"); for (int i = 1; i <= (3 - line); i++) { System.out.print(" "); } System.out.print("/"); for (int i = 1; i <= 2 * (line - 1); i++) { System.out.print(" "); } System.out.print("\\"); for (int i = 1; i <= (3 - line); i++) { System.out.print(" "); } System.out.println("|"); } } } 108 $GGLQJ D &ODVV &RQVWDQW 7KH 'UDZ)LJXUH SURJUDP ZRUNV LQ WKDW LW SURGXFHV WKH RXWSXW ZH VWDUWHG ZLWK EXW LW LV QRW YHU\ IOH[LEOH :KDW LI ZH ZDQWHG WR SURGXFH D VLPLODU ILJXUH RI D GLIIHUHQW VL]H" 7KH RULJLQDO SUREOHP LQYROYHG DQ KRXUJODVV ILJXUH WKDW KDG OLQHV LQ WKH WRS KDOI DQG WKUHH OLQHV LQ WKH ERWWRP KDOI :KDW LI ZH ZDQWHG WKH IROORZLQJ RXWSXW ZLWK OLQHV LQ WKH WRS KDOI DQG OLQHV LQ WKH ERWWRP KDOI" +--------+ |\ /| | \ / | | \ / | | \/ | | /\ | | / \ | | / \ | |/ \| +--------+ 2EYLRXVO\ WKH SURJUDP ZRXOG EH PRUH XVHIXO LI ZH FRXOG PDNH LW IOH[LEOH HQRXJK WR SURGXFH HLWKHU RXWSXW :H GR VR E\ HOLPLQDWLQJ WKH PDJLF QXPEHUV ZLWK WKH LQWURGXFWLRQ RI D FODVV FRQVWDQW <RX PLJKW WKLQN ZH FRXOG LQWURGXFH WZR FRQVWDQWV IRU WKH KHLJKW DQG ZLGWK EXW EHFDXVH RI WKH UHJXODULW\ RI WKLV ILJXUH WKH KHLJKW LV GHWHUPLQHG E\ WKH ZLGWK DQG YLFH YHUVD 6R ZH ZDQW WR LQWURGXFH D VLQJOH FODVV FRQVWDQW /HW V XVH WKH KHLJKW RI WKH KRXUJODVV KDOYHV public static final int SUB_HEIGHT = 4; :H UHIHU WR LW DV VXE KHLJKW UDWKHU WKDQ KHLJKW EHFDXVH WKLV LV WKH KHLJKW RI HDFK RI WKH WZR KDOYHV 1RWLFH KRZ ZH XVH WKH XQGHUVFRUH FKDUDFWHU WR VHSDUDWH WKH GLIIHUHQW ZRUGV LQ WKH QDPH RI WKH FRQVWDQW 6R KRZ GR ZH PRGLI\ WKH RULJLQDO SURJUDP WR LQFRUSRUDWH WKLV FRQVWDQW" :H ORRN WKURXJK LW IRU DQ\ PDJLF QXPEHUV DQG PRGLI\ ZKHQ DSSURSULDWH )RU H[DPSOH WKH GUDZ/LQH PHWKRG GUDZV GDVKHV IRU WKH VXEKHLJKW RI ,I \RX ORRN DW WKH QHZ ILJXUH \RX ZLOO VHH WKDW LW KDV GDVKHV IRU D VXEKHLJKW RI <RX FDQ XVH WKH VDPH NLQG RI UHDVRQLQJ WKDW ZH XVHG ZLWK RXU WDEOHV ,I WKH VXEKHLJKW LV JRLQJ XS E\ DQG WKH QXPEHU RI GDVKHV JRHV XS E\ WKHQ ZH UH JRLQJ WR QHHG D PXOWLSOLHU RI 6R WKH QXPEHU RI GDVKHV ZLOO LQYROYH 68%B+(,*+7 :H DOVR PLJKW QHHG D FRQVWDQW DOWKRXJK QRW LQ WKLV FDVH EHFDXVH WKH H[SUHVVLRQ JLYHV XV H[DFWO\ ZKDW ZH DUH ORRNLQJ IRU GDVKHV IRU D VXEKHLJKW RI GDVKHV IRU D VXEKHLJKW RI 6R WKH GUDZ/LQH PHWKRG ZRXOG EH UHZULWWHQ DV IROORZV // Produces a solid line public static void drawLine() { System.out.print("+"); for (int i = 1; i <= (2 * SUB_HEIGHT); i++) { System.out.print("-"); } System.out.println("+"); } 109 7KHUH DUH TXLWH D IHZ PDJLF QXPEHUV LQ WKH GUDZ7RS DQG GUDZ%RWWRP PHWKRGV ,Q SDUWLFXODU WKH QXPEHUV DQG DSSHDU VHYHUDO WLPHV ,Q VRPH FDVHV LW LV IDLUO\ REYLRXV ZKDW WR GR )RU H[DPSOH HDFK PHWKRG KDV DQ RXWHU IRU ORRS WKDW XVHV WKH PDJLF QXPEHU :K\ " %HFDXVH HDFK VXEILJXUH KDV D KHLJKW RI 2EYLRXVO\ WKDW VKRXOG EH UHSODFHG ZLWK 68%B+(,*+7 7KH QXPEHU LV QRW TXLWH VR REYLRXV <RX FRXOG PDNH DQ HGXFDWHG JXHVV WKDW LI WKH PDJLF QXPEHU LV ZKHQ WKH VXEKHLJKW LV WKHQ SHUKDSV LW V HTXDO WR WZLFH WKH VXEKHLJKW ,Q IDFW WKDW WXUQV RXW WR EH WKH ULJKW DQVZHU ,I JXHVVLQJ GRHVQ W ZRUN \RX FDQ DOZD\V IDOO EDFN RQ WKH WDEOH WHFKQLTXH :RUN RXW D QHZ WDEOH IRU WKH ILJXUH RI VXEKHLJKW DQG ILJXUH RXW ZKDW H[SUHVVLRQV WR XVH IRU LW ,I \RX GR VR \RX OO ILQG WKDW WKH PDJLF QXPEHU LQ WKH ROG H[SUHVVLRQV LV UHSODFHG E\ WKH PDJLF QXPEHU ,I WKH PDJLF QXPEHU QHHGV WR JR XS E\ ZKHQ WKH VXEKHLJKW JRHV XS E\ FOHDUO\ ZH QHHG D PXOWLSOLHU RI 7KH QXPEHUV DQG WXUQ RXW QRW WR EH UHODWHG WR WKH VXEKHLJKW $JDLQ WKLV LV VRPHWKLQJ \RX FDQ PDNH DQ HGXFDWHG JXHVV DERXW DQG YHULI\ E\ H[HFXWLQJ WKH SURJUDP RU \RX FDQ ZRUN RXW D QHZ VHW RI IRUPXODV WR VHH ZKHWKHU WKHVH QXPEHUV FKDQJH ZLWK D QHZ VXEKHLJKW +HUH LV WKH QHZ YHUVLRQ RI WKH SURJUDP ZLWK D FODVV FRQVWDQW IRU WKH VXEKHLJKW ,W XVHV D VXEKHLJKW YDOXH RI EXW ZH FRXOG FKDQJH WKLV QXPEHU WR WR JHW WKH VPDOOHU YHUVLRQ DQG ZH FRXOG FKDQJH LW WR VRPH RWKHU QXPEHU WR JHW \HW DQRWKHU YHUVLRQ RI WKH ILJXUH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class DrawFigure2 { public static final int SUB_HEIGHT = 4; public static void main(String args) { drawLine(); drawTop(); drawBottom(); drawLine(); } // Produces a solid line public static void drawLine() { System.out.print("+"); for (int i = 1; i <= (2 * SUB_HEIGHT); i++) { System.out.print("-"); } System.out.println("+"); } // This produces the top half of the hourglass figure public static void drawTop() { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print("|"); for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } System.out.print("\\"); for (int i = 1; i <= (2 * SUB_HEIGHT - 2 * line); i++) { System.out.print(" "); } System.out.print("/"); for (int i = 1; i <= (line - 1); i++) { System.out.print(" "); } 110 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 System.out.println("|"); } } // This produces the bottom half of the hourglass figure public static void drawBottom() { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print("|"); for (int i = 1; i <= (SUB_HEIGHT - line); i++) { System.out.print(" "); } System.out.print("/"); for (int i = 1; i <= 2 * (line - 1); i++) { System.out.print(" "); } System.out.print("\\"); for (int i = 1; i <= (SUB_HEIGHT - line); i++) { System.out.print(" "); } System.out.println("|"); } } } 7KLV SURJUDP UDLVHV DQ LPSRUWDQW LVVXH ,W GHFODUHV D FRQVWDQW FDOOHG 68%B+(,*+7 WKDW LV XVHG WKURXJKRXW WKH SURJUDP %XW WKH FRQVWDQW LV QRW GHFODUHG ORFDOO\ LQ WKH LQGLYLGXDO PHWKRGV 7KDW VHHPV WR YLRODWH RXU SULQFLSOH RI ORFDOL]LQJ ZKHQHYHU SRVVLEOH :KLOH ORFDOL]LQJ RI YDULDEOHV LV D JRRG LGHD WKH VDPH LV QRW DOZD\V WUXH IRU FRQVWDQWV ,Q WKLV FDVH D FODVV ZLGH GHILQLWLRQ LV PRUH DSSURSULDWH :H ORFDOL]H YDULDEOHV WR DYRLG SRWHQWLDO LQWHUIHUHQFH 7KDW DUJXPHQW GRHVQ W KROG IRU FRQVWDQWV VLQFH WKH\ DUH JXDUDQWHHG QRW WR FKDQJH $QRWKHU DUJXPHQW IRU XVLQJ ORFDO YDULDEOHV LV WKDW LW PDNHV RXU VWDWLF PHWKRGV PRUH LQGHSHQGHQW 7KDW DUJXPHQW KDV VRPH PHULW EXW QRW HQRXJK ,W LV WUXH WKDW FODVV FRQVWDQWV LQWURGXFH GHSHQGHQFLHV EHWZHHQ PHWKRGV EXW RIWHQ WKDW LV UHDOO\ ZKDW \RX ZDQW )RU H[DPSOH WKH WKUHH PHWKRGV RI WKH KRXUJODVV SURJUDP VKRXOG QRW EH LQGHSHQGHQW RI HDFK RWKHU ZKHQ LW FRPHV WR WKH VL]H RI ILJXUHV (DFK VXEILJXUH KDV WR XVH WKH VDPH VL]H FRQVWDQW ,PDJLQH WKH SRWHQWLDO GLVDVWHU LI HDFK PHWKRG KDG LWV RZQ 68%B+(,*+7 HDFK ZLWK D GLIIHUHQW YDOXH 1RQH RI WKH SLHFHV ZRXOG ILW WRJHWKHU )XUWKHU 9DULDWLRQV 7KLV VROXWLRQ PD\ VHHP FXPEHUVRPH EXW LW LV HDVLHU WR DGDSW WR D QHZ WDVN )RU H[DPSOH VXSSRVH WKDW \RX ZDQW WR JHQHUDWH WKH IROORZLQJ RXWSXW 111 +----------+ |\ /| | \ / | | \ / | | \ / | | \/ | | /\ | | / \ | | / \ | | / \ | |/ \| +----------+ | /\ | | / \ | | / \ | | / \ | |/ \| |\ /| | \ / | | \ / | | \ / | | \/ | +----------+ 7KLV RXWSXW XVHV D VXEKHLJKW RI DQG LQFOXGHV ERWK D GLDPRQG SDWWHUQ DQG DQ ; SDWWHUQ :H FDQ SURGXFH WKLV RXWSXW E\ FKDQJLQJ WKH VXEKHLJKW FRQVWDQW WR public static final int SUB_HEIGHT = 4; $QG UHZULWLQJ WKH PDLQ PHWKRG DV IROORZV WR SURGXFH ERWK WKH RULJLQDO GLDPRQG SDWWHUQ DQG WKH QHZ ; SDWWHUQ ZKLFK ZH JHW VLPSO\ E\ UHYHUVLQJ WKH RUGHU RI WKH FDOOV RQ WKH WZR KDOYHV public static void main(String args) { drawLine(); drawTop(); drawBottom(); drawLine(); drawBottom(); drawTop(); drawLine(); } &KDSWHU 6XPPDU\ -DYD JURXSV GDWD LQWR W\SHV 7KHUH DUH WZR PDMRU FDWHJRULHV RI GDWD W\SHV SULPLWLYH GDWD DQG REMHFWV 3ULPLWLYH W\SHV LQFOXGH LQW LQWHJHUV GRXEOH UHDO QXPEHUV FKDU LQGLYLGXDO WH[W FKDUDFWHUV DQG ERROHDQ ORJLFDO YDOXHV 9DOXHV DQG FRPSXWDWLRQV DUH FDOOHG H[SUHVVLRQV 7KH VLPSOHVW H[SUHVVLRQV DUH LQGLYLGXDO YDOXHV DOVR FDOOHG OLWHUDOV 6RPH H[DPSOH OLWHUDOV DUH 42 3.14 'Q' DQG false ([SUHVVLRQV PD\ FRQWDLQ RSHUDWRUV VXFK DV (3 + 29) - 4 * 5 7KH GLYLVLRQ RSHUDWLRQ LV RGG LQ WKDW LW V VSOLW LQWR TXRWLHQW DQG UHPDLQGHU RSHUDWLRQV 112 5XOHV RI SUHFHGHQFH GHWHUPLQH WKH RUGHU LQ PXOWLSOH RSHUDWRUV DUH HYDOXDWHG LQ FRPSOH[ H[SUHVVLRQV ,Q SDUWLFXODU PXOWLSOLFDWLRQ DQG GLYLVLRQ DUH SHUIRUPHG EHIRUH DGGLWLRQ DQG VXEWUDFWLRQ 3DUHQWKHVHV FDQ EH XVHG WR IRUFH D SDUWLFXODU RUGHU RI HYDOXDWLRQ 'DWD FDQ EH FRQYHUWHG IURP RQH W\SH WR DQRWKHU E\ DQ RSHUDWLRQ FDOOHG D FDVW 9DULDEOHV DUH PHPRU\ ORFDWLRQV LQ ZKLFK D YDOXH FDQ EH VWRUHG $ YDULDEOH LV GHFODUHG ZLWK D QDPH DQG D W\SH $Q\ GDWD YDOXH ZLWK D FRPSDWLEOH W\SH FDQ EH VWRUHG LQ WKH YDULDEOH V PHPRU\ DQG XVHG ODWHU LQ WKH SURJUDP 3ULPLWLYH GDWD FDQ EH SULQWHG RQ WKH FRQVROH XVLQJ WKH 6\VWHP RXW SULQWOQ PHWKRG MXVW OLNH WH[W VWULQJV $ 6WULQJ FDQ EH FRQQHFWHG ZLWK DQRWKHU YDOXH FRQFDWHQDWHG ZLWK WKH + RSHUDWRU WR SURGXFH D ODUJHU VWULQJ 7KLV DOORZV \RX WR SULQW FRPSOH[ H[SUHVVLRQV ZLWK WH[W RQ WKH FRQVROH $ ORRS LV XVHG WR H[HFXWH D JURXS RI VWDWHPHQWV VHYHUDO WLPHV 7KH IRU ORRS LV RQH NLQG RI ORRS WKDW FDQ EH XVHG WR DSSO\ WKH VDPH VWDWHPHQWV RYHU D UDQJH RI QXPEHUV RU WR UHSHDW VWDWHPHQWV D GHWHUPLQHG QXPEHU RI WLPHV $ ORRS FDQ FRQWDLQ DQRWKHU ORRS DOVR FDOOHG D QHVWHG ORRS 7KH 6\VWHP RXW SULQW VWDWHPHQW ZULWHV D SDUWLDO OLQH RI RXWSXW WR WKH FRQVROH ZLWKRXW DGYDQFLQJ WR WKH QH[W OLQH 6\VWHP RXW SULQW LV XVHIXO ZKHQ ZULWLQJ IRU ORRSV WKDW SURGXFH PDQ\ SDUWV RI D OLQH RI RXWSXW $ YDULDEOH H[LVWV IURP WKH OLQH LW LV GHFODUHG WR WKH HQG RI WKH FXUO\ EUDFHV LQ ZKLFK LW ZDV GHFODUHG 7KLV UDQJH DOVR FDOOHG WKH VFRSH RI WKH YDULDEOH FRQVWLWXWHV WKH OHJDO SDUW RI WKH SURJUDP ZKHUH WKH YDULDEOH FDQ OHJDOO\ EH XVHG $ YDULDEOH GHFODUHG LQVLGH D PHWKRG RU ORRS LV FDOOHG D ORFDO YDULDEOH $ ORFDO YDULDEOH FDQ RQO\ EH XVHG LQVLGH LWV PHWKRG RU ORRS $Q DOJRULWKP FDQ EH HDVLHU WR ZULWH LI \RX ILUVW ZULWH DQ (QJOLVK GHVFULSWLRQ RI LW 6XFK D GHVFULSWLRQ LV DOVR FDOOHG SVHXGR FRGH ,PSRUWDQW FRQVWDQW YDOXHV ZULWWHQ LQWR WKH SURJUDP VKRXOG LQVWHDG EH GHFODUHG DV FODVV FRQVWDQWV ERWK WR H[SODLQ WKHLU QDPH DQG YDOXH DQG WR PDNH LW HDVLHU WR FKDQJH WKHLU YDOXH ODWHU 6HOI &KHFN 3UREOHPV 6HFWLRQ %DVLF 'DWD &RQFHSWV :KLFK RI WKH IROORZLQJ DUH OHJDO LQW OLWHUDOV" 22 1.5 -1 2.3 10.0 5. -6875309 '7' 7UDFH WKH HYDOXDWLRQ RI WKH IROORZLQJ H[SUHVVLRQV DQG JLYH WKHLU UHVXOWLQJ YDOXHV 2 + 3 * 4 - 6 14 / 7 * 2 + 30 / 5 + 1 (12 + 3) / 4 * 2 (238 % 10 + 3) % 7 12 - 2 - 3 6/2 + 7/3 6 * 7%4 3 * 4 + 2 * 3 113 (18 - 7) * (43 % 10) 2 + 19 % 5 - (11 * (5 / 2)) 813 % 100/3 + 2.4 26 % 10 % 4 * 3 22 + 4 * 2 23 % 8 % 3 177 % 100 % 10/2 89 % (5 + 5) % 5 392/10 % 10/2 8 * 2 - 7/4 37 % 20 % 3 * 4 7UDFH WKH HYDOXDWLRQ RI WKH IROORZLQJ H[SUHVVLRQV DQG JLYH WKHLU UHVXOWLQJ YDOXHV 17 % 10/4 4.0/2 * 9/2 2.5 * 2 + 8/5.0 + 10/3 12/7 * 4.4 * 2/4 4 * 3/8 + 2.5 * 2 (5 * 7.0/2 - 2.5)/5 * 2 12/7 * 4.4 * 2/4 41 % 7 * 3/5 + 5/2 * 2.5 10.0/2/4 8/5 + 13/2/3.0 (2.5 + 3.5)/2 9/4 * 2.0 - 5/4 9/2.0 + 7/3 - 3.0/2 813 % 100/3 + 2.4 27/2/2.0 * (4.3 + 1.7) - 8/3 4.0/2 * 9/2 53/5/(0.6 + 1.4)/2 + 13/2 2.5 * 2 + 8/5.0 + 10/3 2 * 3/4 * 2/4.0 + 4.5 - 1 89 % 10/4 * 2.0/5 + (1.5 + + 1.0/2) * 2 7UDFH WKH HYDOXDWLRQ RI WKH IROORZLQJ H[SUHVVLRQV DQG JLYH WKHLU UHVXOWLQJ YDOXHV 2 + 2 + 3 + 4 "2 + 2 " + 3 + 4 2 + " 2 + 3 " + 4 3 + 4 + " 2 + 2" "2 + 2 " + (3 + 4) "(2 + 2) " + (3 + 4) "hello 34 " + 2 * 4 6HFWLRQ 9DULDEOHV ,PDJLQH \RX DUH ZULWLQJ D SHUVRQDO ILWQHVV SURJUDP WKDW VWRUHV WKH XVHU V DJH JHQGHU KHLJKW LQ IHHW RU PHWHUV DQG ZHLJKW WR WKH QHDUHVW SRXQG RU NLORJUDP 'HFODUH YDULDEOHV ZLWK WKH DSSURSULDWH QDPHV DQG W\SHV WR KROG WKLV LQIRUPDWLRQ ,PDJLQH \RX DUH ZULWLQJ D VWXGHQW SURJUDP WKDW VWRUHV WKH VWXGHQW V \HDU )UHVKPDQ 6RSKRPRUH -XQLRU RU 6HQLRU QXPEHU RI FRXUVHV DQG *3$ RQ D VFDOH 'HFODUH YDULDEOHV ZLWK WKH DSSURSULDWH QDPHV DQG W\SHV WR KROG WKLV LQIRUPDWLRQ 6XSSRVH \RX KDYH DQ LQW YDULDEOH QDPHG number :KDW -DYD H[SUHVVLRQ SURGXFHV WKH ODVW GLJLW RI WKH QXPEHU WKH V SODFH " 6XSSRVH \RX KDYH DQ LQW YDULDEOH QDPHG number :KDW -DYD H[SUHVVLRQ SURGXFHV WKH VHFRQG WR ODVW GLJLW RI WKH QXPEHU WKH V SODFH " :KDW H[SUHVVLRQ SURGXFHV WKH WKLUG WR ODVW GLJLW RI WKH QXPEHU WKH V SODFH " &RQVLGHU WKH IROORZLQJ FRGH int first = 8; int second = 19; first = first + second; second = first - second; first = first - second; :KDW DUH WKH YDOXHV RI first DQG second DW WKH HQG RI WKLV FRGH" +RZ ZRXOG \RX GHVFULEH WKH QHW HIIHFW RI WKH FRGH VWDWHPHQWV LQ WKLV H[HUFLVH" 114 5HZULWH WKH FRGH IURP WKH SUHYLRXV H[HUFLVH WR EH VKRUWHU E\ GHFODULQJ WKH YDULDEOHV WRJHWKHU DQG E\ XVLQJ WKH VSHFLDO DVVLJQPHQW RSHUDWRUV VXFK DV DQG DV DSSURSULDWH :KDW DUH WKH YDOXHV RI a b DQG c DIWHU WKH IROORZLQJ FRGH VWDWHPHQWV" ,W PD\ KHOS \RX WR ZULWH GRZQ WKHLU YDOXHV DIWHU HDFK OLQH int a = 5; int b = 10; int c = a++ + ++b; a++; b--; 6HFWLRQ 7KH IRU /RRS $VVXPH WKDW \RX KDYH D YDULDEOH QDPHG count WKDW ZLOO WDNH RQ WKH YDOXHV DQG VR RQ <RX DUH JRLQJ WR IRUPXODWH H[SUHVVLRQV LQ WHUPV RI FRXQW WKDW ZLOO \LHOG GLIIHUHQW VHTXHQFHV )RU H[DPSOH WR JHW WKH VHTXHQFH \RX ZRXOG XVH WKH H[SUHVVLRQ (2 * count) )LOO LQ WKH IROORZLQJ WDEOH LQGLFDWLQJ DQ H[SUHVVLRQ WKDW ZLOO JHQHUDWH HDFK VHTXHQFH 6HTXHQFH ([SUHVVLRQ &RPSOHWH WKH FRGH IRU WKH IROORZLQJ IRU ORRS for (int i = 1; i < 6; i++) { // your code here } VR WKDW LW SULQWV WKH IROORZLQJ QXPEHUV RQH SHU OLQH -4 14 32 50 68 86 5HZULWH WKH IROORZLQJ FRGH DV D VHULHV RI HTXLYDOHQW 6\VWHP RXW SULQWOQ VWDWHPHQWV L H ZLWKRXW DQ\ 6\VWHP RXW SULQW VWDWHPHQWV 115 System.out.print("Twas "); System.out.print("brillig and the"); System.out.println(" "); System.out.print(" slithy toves did"); System.out.print(" "); System.out.println("gyre and"); System.out.println( "gimble"); System.out.println(); System.out.println( "in the wabe." ); 6XSSRVH WKDW \RX DUH WU\LQJ WR ZULWH D SURJUDP WKDW SURGXFHV WKH IROORZLQJ RXWSXW 1 3 5 7 9 11 13 15 17 19 21 1 3 5 7 9 11 7KH IROORZLQJ SURJUDP LV DQ DWWHPSW DW D VROXWLRQ EXW LW FRQWDLQV IRXU PDMRU HUURUV ,GHQWLI\ WKHP DOO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class BadNews { public static final int MAX_ODD = 21; public static void writeOdds() { // print each odd number for (int count = 1; count <= (MAX_ODD - 2); count++) { System.out.print(count + " "); count = count + 2; } // print the last odd number System.out.print(count + 2); } public static void main(String args) { // write all odds up to 21 writeOdds(); // now, write all odds up to 11 MAX_ODD = 11; writeOdds(); } } :KDW LV WKH RXWSXW RI WKH IROORZLQJ PHWKRG oddStuff public static void oddStuff() { int number = 4; for (int count = 1; count <= number; count++) { System.out.println(number); number = number / 2; } } :KDW LV WKH RXWSXW RI WKH IROORZLQJ PHWKRG unknown 116 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Strange { public static final int MAX = 5; public static void unknown() { int number = 0; for (int count = MAX; count >= 1; count--) { number += (count * count); } System.out.println("The result is: " + number); } public static void main(String args) { unknown(); } } :KDW LV WKH RXWSXW RI WKH IROORZLQJ ORRS" int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ ORRS" System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+"); :KDW LV WKH RXWSXW RI WKH IROORZLQJ ORRS" for (int i = 1; i <= 3; i++) System.out.println("How many lines"); System.out.println("are printed?"); :KDW LV WKH RXWSXW RI WKH IROORZLQJ ORRS" System.out.print("T-minus "); for (int i = 5; i >= 1; i--) { System.out.print(i + ", "); } System.out.println("Blastoff!"); :KDW LV WKH RXWSXW RI WKH IROORZLQJ VHTXHQFH RI ORRSV" for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print((i * j) + " "); } System.out.println(); } 117 :KDW LV WKH RXWSXW RI WKH IROORZLQJ VHTXHQFH RI ORRSV" for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10 - i; j++) { System.out.print(" "); } for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ VHTXHQFH RI ORRSV" for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1; k <= 4; k++) { System.out.print("*"); } System.out.print("!"); } System.out.println(); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ VHTXHQFH RI ORRSV" 1RWLFH WKDW LW LV WKH VDPH DV WKH SUHYLRXV H[HUFLVH H[FHSW WKDW WKH SODFHPHQW RI WKH EUDFHV KDV FKDQJHG for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1; k <= 4; k++) { System.out.print("*"); } } System.out.print("!"); System.out.println(); } :KDW LV WKH RXWSXW RI WKH IROORZLQJ VHTXHQFH RI ORRSV" 1RWLFH WKDW LW LV WKH VDPH DV WKH SUHYLRXV H[HUFLVH H[FHSW WKDW WKH SODFHPHQW RI WKH EUDFHV KDV FKDQJHG for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1; k <= 4; k++) { System.out.print("*"); System.out.print("!"); } System.out.println(); } } 6HFWLRQ 0DQDJLQJ &RPSOH[LW\ :ULWH D -DYD SURJUDP WKDW SURGXFHV WKH IROORZLQJ RXWSXW 8VH QHVWHG IRU ORRSV WR FDSWXUH WKH VWUXFWXUH RI WKH ILJXUH 118 !!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!////// \\\\\\\\!!!!!!//////// \\\\\\\\\\!!////////// 0RGLI\ \RXU SURJUDP IURP WKH SUHYLRXV H[HUFLVH VR WKDW LW XVHV D JOREDO FRQVWDQW IRU WKH ILJXUH V KHLJKW 7KH SUHYLRXV RXWSXW XVHG D FRQVWDQW KHLJKW RI +HUH LV WKH RXWSXW IRU D FRQVWDQW KHLJKW RI !!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// ([HUFLVHV ,Q SK\VLFV D FRPPRQ XVHIXO HTXDWLRQ IRU ILQGLQJ WKH SRVLWLRQ V RI D ERG\ LQ OLQHDU PRWLRQ DW D JLYHQ WLPH W EDVHG RQ LWV LQLWLDO SRVLWLRQ V LQLWLDO YHORFLW\ Y DQG UDWH RI DFFHOHUDWLRQ D LV WKH IROORZLQJ :ULWH FRGH WR GHFODUH YDULDEOHV IRU V Y D DQG W DQG WKHQ ZULWH WKH FRGH WR FRPSXWH V EDVHG RQ WKHVH YDOXHV :ULWH D IRU ORRS WKDW SURGXFHV WKH IROORZLQJ RXWSXW 1 4 9 16 25 36 49 64 81 100 )RU DGGHG FKDOOHQJH WU\ WR PRGLI\ \RXU FRGH IURP WKH SUHYLRXV H[HUFLVH VR WKDW LW GRHV QRW QHHG WR XVH WKH PXOWLSOLFDWLRQ RSHUDWRU ,W FDQ EH GRQH +LQW /RRN DW WKH GLIIHUHQFHV EHWZHHQ DGMDFHQW QXPEHUV 7KH )LERQDFFL QXPEHUV DUH D VHTXHQFH RI LQWHJHUV ZKHUH WKH ILUVW WZR HOHPHQWV DUH DQG HDFK IROORZLQJ HOHPHQW LV WKH VXP RI WKH WZR SUHFHGLQJ HOHPHQWV 7KH PDWKHPDWLFDO GHILQLWLRQ RI HDFK NWK )LERQDFFL LV WKH IROORZLQJ 7KH ILUVW )LERQDFFL QXPEHUV DUH 1 1 2 3 5 8 13 21 34 55 89 144 :ULWH D IRU ORRS WKDW FRPSXWHV DQG SULQWV WKH ILUVW :ULWH IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW )LERQDFFL QXPEHUV 119 ***** ***** ***** ***** :ULWH IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW * ** *** **** ***** :ULWH IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW 1 22 333 4444 55555 666666 7777777 :ULWH IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW 1 2 3 4 5 :ULWH IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW 1 22 333 4444 55555 :ULWH IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW ZLWK HDFK OLQH ---------------------------------------_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^1122334455667788990011223344556677889900 ---------------------------------------- FKDUDFWHUV ZLGH ,W V FRPPRQ WR SULQW D URWDWLQJ LQFUHDVLQJ OLVW RI VLQJOH GLJLW QXPEHUV DW WKH VWDUW RI D SURJUDP V RXWSXW DV D YLVXDO JXLGH WR QXPEHU WKH FROXPQV RI WKH RXWSXW WR IROORZ :LWK WKLV LQ PLQG ZULWH QHVWHG IRU ORRSV WR SURGXFH WKH IROORZLQJ RXWSXW ZLWK HDFK OLQH FKDUDFWHUV ZLGH | | | | | | 123456789012345678901234567890123456789012345678901234567890 120 0RGLI\ \RXU FRGH IURP WKH SUHYLRXV H[HUFLVH VR WKDW LW FRXOG HDVLO\ EH PRGLILHG WR GLVSOD\ D GLIIHUHQW UDQJH RI QXPEHUV LQVWHDG RI D GLIIHUHQW QXPEHU RI UHSHWLWLRQV RI WKRVH QXPEHUV LQVWHDG RI WRWDO FKDUDFWHUV DQG NHHS WKH YHUWLFDO EDUV PDWFKLQJ XS FRUUHFWO\ 8VH FODVV FRQVWDQWV LQVWHDG RI PDJLF QXPEHUV ([DPSOH RXWSXWV WKDW FRXOG EH JHQHUDWHG E\ FKDQJLQJ \RXU FRQVWDQWV ZRXOG EH | | | | | | | | | | 12340123401234012340123401234012340123401234012340 | | | | | | | 12345670123456701234567012345670123456701234567012345670 :ULWH QHVWHG IRU ORRSV WKDW SURGXFH WKH IROORZLQJ RXWSXW ,QGLFDWH WKH OHYHO RI HDFK VWDWHPHQW LQ \RXU FRGH XVLQJ LQGHQWDWLRQ DQG D FRPPHQW 000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999 0RGLI\ WKH FRGH VR WKDW LW QRZ SURGXFHV WKH IROORZLQJ RXWSXW 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 0RGLI\ WKH FRGH VR WKDW LW QRZ SURGXFHV WKH IROORZLQJ RXWSXW 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 :ULWH D PHWKRG QDPHG printDesign WKDW SURGXFHV WKH IROORZLQJ RXWSXW 8VH IRU ORRSV WR FDSWXUH WKH VWUXFWXUH RI WKH ILJXUH -----1--------333------55555----7777777--999999999- :ULWH D SVHXGR FRGH DOJRULWKP WKDW ZLOO SURGXFH WKH IROORZLQJ ILJXUH DV RXWSXW +===+===+ | | | | | | | | | +===+===+ | | | | | | | | | +===+===+ 121 8VH \RXU SVHXGR FRGH IURP WKH SUHYLRXV H[HUFLVH WR ZULWH D -DYD SURJUDP WKDW SURGXFHV WKH SUHFHGLQJ ILJXUH DV RXWSXW 8VH IRU ORRSV WR SULQW WKH UHSHDWHG SDUWV RI WKH ILJXUH 2QFH \RX JHW LW WR ZRUN DGG D FODVV FRQVWDQW VR WKDW WKH VL]H RI WKH ILJXUH FRXOG EH FKDQJHG VLPSO\ E\ FKDQJLQJ WKH FRQVWDQW V YDOXH 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WKDW SURGXFHV WKH IROORZLQJ RXWSXW ****** ***** **** *** ** * //////////// ****** //////////\\ ***** ////////\\\\ **** //////\\\\\\ *** ////\\\\\\\\ ** //\\\\\\\\\\ * \\\\\\\\\\\\ :ULWH D SURJUDP WKDW SURGXFHV WKH IROORZLQJ RXWSXW +------+ | ^^ | | ^ ^ | |^ ^| | ^^ | | ^ ^ | |^ ^| +------+ |v v| | v v | | vv | |v v| | v v | | vv | +------+ :ULWH D SURJUDP WKDW SURGXFHV WKH IROORZLQJ RXWSXW +---------+ | * | | /*\ | | //*\\ | | ///*\\\ | | \\\*/// | | \\*// | | \*/ | | * | +---------+ | \\\*/// | | \\*// | | \*/ | | * | | * | | /*\ | | //*\\ | | ///*\\\ | +---------+ 122 :ULWH D SURJUDP WKDW SURGXFHV WKH IROORZLQJ RXWSXW 8VH D FODVV FRQVWDQW WR PDNH LW SRVVLEOH WR FKDQJH WKH QXPEHU RI VWDLUV LQ WKH ILJXUH O ******* /|\ * * / \ * * O ****** * /|\ * * / \ * * O ****** * /|\ * * / \ * * O ****** * /|\ * * / \ * * O ****** * /|\ * * / \ * * ******************************** :ULWH D SURJUDP WKDW SURGXFHV WKH IROORZLQJ URFNHW VKLS ILJXUH DV LWV RXWSXW 8VH D FODVV FRQVWDQW WR PDNH LW SRVVLEOH WR FKDQJH WKH VL]H RI WKH URFNHW WKH IROORZLQJ RXWSXW XVHV D VL]H RI /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| +=*=*=*=*=*=*+ |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| +=*=*=*=*=*=*+ /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\ 6WXDUW 5HJHV 0DUW\ 6WHSS 123 124 ...
View Full Document

Ask a homework question - tutors are online