Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
AnotherSimpleProgram:AddingTwoIntegers
AnotherSimpleProgram:AddingTwoIntegers
• Thisprogramusestheinputstreamobjectstd::cinandthestreamextrac<onoperator,>>,toobtaintwointegerstypedbyauseratthekeyboard,computesthesumofthesevaluesandoutputstheresultusingstd::cout.
Note:‘<<‘iscalled‘inser<onoperator’
AnotherSimpleProgram:AddingTwoIntegers
• Variabledeclara0on:
• number1,number2,sumarethenamesofvariables• Avariableisaloca<oninthecomputer’smemorywhereavalue
canbestoredforusebyaprogram.• Thesedeclara<onsspecifythatthevariablesnumber1,number2,
andsumaredataoftypeint,meaningthatthesevariableswillholdintegervalues,i.e.,wholenumberssuchas7,–11,0and31,914.
• Allvariablesmustbedeclaredwithanameandadatatypebeforetheycanbeusedinaprogram.
• Severalvariablesofthesametypemaybedeclaredinonedeclara<onorinmul<pledeclara<ons.Wecouldhavedeclaredallthreevariablesinonedeclara<onbyusingacomma-separatedlistasfollows:int number1, number2, sum;!
AnotherSimpleProgram:AddingTwoIntegers
• Variable’sname– Aseriesofcharactersconsis<ngofleQers,digitsandunderscores(_).
– DoesNOTbeginwithanumber.– C++iscasesensi<vei.e.,uppercaseandlowercaseleQersaredifferent,soa1andA1representdifferentvariables.
– MustNOTbeaspecialcharactere.g.,&,%,$,… – MustNOTbeakeyword.(Whatisakeyword?)
AnotherSimpleProgram:AddingTwoIntegers
• Variable’stypes– We’llsoondiscussthetypefloatforspecifying
decimalnumbers,andthetypecharforspecifyingcharacterdata.• Decimalnumbersarenumberswithdecimalpoints,
suchas3.4,0.0and–11.19.• Acharvariablemayholdonlyasinglelowercase
leQer,asingleuppercaseleQer,asingledigitorasinglespecialcharacter(e.g.,$or*).
– Typessuchasint,float,andchararecalledfundamentaltypes.
– Fundamental-typenamesarekeywords.
AnotherSimpleProgram:AddingTwoIntegers
• PlacementofVariableDeclara0ons– Declara<onsofvariablescanbeplacedalmostanywhereinaprogram,buttheymustappearbeforetheircorrespondingvariablesareusedintheprogram.
AnotherSimpleProgram:AddingTwoIntegers
• ObtainingtheFirstValuefromtheUser
– displaysEnterfirstinteger:followedbyaspace.Thismessageiscalledapromptbecauseitdirectstheusertotakeaspecificac<on.
– usesthestandardinputstreamobjectcin(ofnamespacestd)andthestreamextrac<onoperator,>>,toobtainavaluefromthekeyboard.
– Usingthestreamextrac<onoperatorwithstd::cintakescharacterinputfromthestandardinputstream,whichisusuallythekeyboard.
AnotherSimpleProgram:AddingTwoIntegers
• ObtainingtheSecondValuefromtheUser
AnotherSimpleProgram:AddingTwoIntegers
• Calcula0ngtheSumoftheValuesInputbytheUser
– Thisassignmentstatementaddsthevaluesofvariablesnumber1andnumber2andassignstheresulttovariablesumusingtheassignmentoperator=
AnotherSimpleProgram:AddingTwoIntegers
• DisplayingtheResult
– displaysthecharacterstringSumisfollowedbythenumericalvalueofvariablesumfollowedbystd::endl—aso-calledstreammanipulator.
– Thenameendlisanabbrevia<onfor“endline”andbelongstonamespacestd.Thestd::endlstreammanipulatoroutputsanewline.
MemoryConcepts
• Variablenamessuchasnumber1,number2andsumactuallycorrespondtoloca<onsinthecomputer’smemory.Everyvariablehasaname,atype,asizeandavalue.
Arithme<c
• C++providesthemodulusoperator,%,thatyieldstheremainderamerintegerdivision.Themodulusoperatorcanbeusedonlywithintegeroperands.Theexpressionx%yyieldstheremainderamerxisdividedbyy.Thus,7%4yields3and17%5yields2.
Arithme<c• Examples,
Arithme<c• RulesofOperatorPrecedence
C++appliestheoperatorsinarithme<cexpressionsinapreciseorderdeterminedbytheserulesofoperatorprecedence,whicharegenerallythesameasthoseinalgebra:1. Operatorsinexpressionscontainedwithinpairsof
parenthesesareevaluatedfirst.Parenthesesaresaidtobeatthe“highestlevelofprecedence.”Incasesofnested,orembedded,parentheses,suchas(a*(b+c)),theoperatorsintheinnermostpairofparenthesesareappliedfirst.
2. Mul0plica0on,division,andmodulusopera<onsareappliednext.Ifanexpressioncontainsseveralmul<plica<on,divisionandmodulusopera<ons,operatorsareappliedfromlemtoright.
3. Addi0onandsubtrac0onopera<onsareappliedlast.Ifanexpressioncontainsseveraladdi<onandsubtrac<onopera<ons,operatorsareappliedfromlemtoright.
Arithme<c• Examples
1:2+(2-2)*(2+2)/2=?2:3+3-3*(3/3)=?3:(4+4–4*4)/((4+4)*-1)=?4:(22%4–1)/2+1/2=?
ModifytheProgram
• Writeaprogramthattakesthreeintegersfromtheuserandoutputsthefollowingopera<on:
Decisionmaking(EqualityandRela<onalOperators)
• Theifstatementallowsaprogramtotakealterna<veac<onbasedonwhetheracondi<onistrueorfalse.– Ifthecondi<onistrue,thestatementinthebodyoftheifstatementisexecuted.Ifthecondi<onisfalse,thebodystatementisnotexecuted.
– Condi<onsinifstatementscanbeformedbyusingtheequalityoperatorsandrela<onaloperators.
Decisionmaking(EqualityandRela<onalOperators)
• Ifstatement if(condi<on){ //dosomething… }
Decisionmaking(EqualityandRela<onalOperators)
Decisionmaking(EqualityandRela<onalOperators)
• Let’swriteaprogramthatcomparestwointegers!
Decisionmaking(EqualityandRela<onalOperators)
using namesapce std!isadirec<vethatenablesaprogramtouseallnamesinanystandardC++headersuchas<iostream>
LabAssignment#1