Beruflich Dokumente
Kultur Dokumente
Reasonstopayattentiontowhatyouwriteinyourprograms:
Canyoureadthis? THEPAOMNNEHALPWEOROFTHEHMUANMNID
Aoccdrnig to rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in What oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist and lsat ltteers are in the rghit pclae. The rset can be a taotl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Amzanig huh?
Youprobablyread: THEPHENOMENALPOWEROFTHEHUMANMIND
According to research at Cambridge University, it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letters are in the right place. The rest can be a total mess and you can still read it without a problem. This is because the human mind does not read every letter by itself, but the word as a whole. Amazing huh?
Similarly,whenreadingsometext,youmayfigureoutthataparticularsentenceisa questionevenintheabsenceofthe?symbol. o Why.Gotmypoint,right? CompilershoweverdoNOThavethesamepowerashumanbeingstoreadpoorlywritten words(asitstandstoday),forexample,omitting;attheendofastatementinJAVAyields anerror SowheneverthereisawordtheydoNOTunderstand,theysimplydoNOThandlethese words Whenyouwriteprograms,ifthereareanywordsthecompilerdoesNOTunderstand,you getanerror. Sopaycarefulattentiontodetailsandallsyntaxruleswhenwritingprograms.
TipsforavoidingmakingsyntacticerrorsinJAVA
ThefollowingnotesgivesomeoftherulesforwritingJAVAcodesandhintsonhowtomakesure weabidebythoserules: Any{shouldhaveitscorresponding}. o Wheneveryouwritea{,beforewritinganythingelse,writethecorresponding}and indentittotheproperlevel. o Althoughyourcompilermayacceptsomecodes,semantically,youmaybewriting thewrongcode. Any/*shouldhaveitscorresponding*/. o Wheneveryouwritea/*,beforewritinganythingelse,writethecorresponding*/at thestartofanewline). o Otherwise,youmaymessupasyourcompilertreatsallthatfollowsthe/*as commentandifdowntheroad,youhaveinsertedanothercomment,theendofyour secondcommentwillbetreatedastheclosingcommentsymbol
/* comments1 statements /*comments2 */
All'statements'abovewillbetreatedascomments o Alongthesameline,commentscannotbenestedwithineachother Any(shouldhaveitscorresponding) Allstatementsneedtoendwitha; o exceptforconditionalstatements JAVAisacasesensitivelanguage o Keywordsarealsocasesensitive(ifandIFaredifferenttoyourcompiler) o variablesyoudeclarearealsocasesensitive. e.gheight,Height,hEight,...HEIGHTareallconsidereddifferentsymbols inJAVA CommonMistakesbeginnersandsometimessemipro'smake: Using=insteadof==andviceversa Typingandinadvertantly,pressingtheCapsLockkey,andhavingallstatements beinguppercaseJAVAiscasesensitive,remember. Invertingtheorderofsomecharacters(writingmianinsteadofmain).
Note:Theabovenamingconventionisonewechooseforthismodule.Youmayfindother conventionsthatwillbeusedatyourfutureworkplace,forexample,precedinganinteger variablewithani,astringwithans,afloatwithanfetc. Indentation o Makeslogicclear(althoughyourcompilerdoesnotneedindentingtounderstand yourcode) Modularizeyourprograms o Allowsyoutowritecodesthatcanbeusedmorethanonce Allowsyoutowriteprogramsfasterandmakelesserrors Moreonthislater(whenwetalkaboutfunctions) Comments o CommentsshouldcontainProgramname,author,datewritten,purpose,version o Althoughforsmallprograms,itmayseemfutiletowritetheabovecomments,get
Forexample,mostofyourprograms(forthetimebeing)willhavethefollowingform:
/* *************************************************** Purpose: Author: Date: Version: *************************************************** */ import java.util.*; import java.util.Scanner; public class Classname { public static void main(String[] args){ } }
ThetablebelowshowssomeofthesymbolsavailableinC++. Symbol Description // LineComment /* */ { Beginningofacomment Endofacomment Beginningofablockofstatements Comments Usedwheneverweneedtoinsertaone linecomment Usedtoenclosecommentswithinour program Usedtogroupabunchofstatements
} * / = == + ()
Endofablockofstatements Multiplicationoperator Divisionoperator Assignment Testforequality Addition Subtraction Usedinarithmeticoperationsfor precedence Usedwithif,whileconditions
Considerthefollowingcodes:
while(ans=='y') { System.out.println("enter number " +counter+1); arr[counter]=sc.nextInt(); counter++; System.out.println("Do you want to continue y/n? \n"); ans=sc.next(); if (!ans.equals(y)) break; }//end while