DECISION CONTROL CONSTRUCTS IN JAVA Decisin cntrl statements can change the executin flw f a prgram. Decisin cntrl statements in Java are: if statement Cnditinal peratr switch statement If statement The if statement is fllwed by a bdy blck. The if statement evaluate lgical expressins and make a decisin whether t execute the blck r nt based n whether the lgical expressin returns a true r false. Syntax: if if(cnditin) {//d smething If the cnditin is true the if blck is executed, else nt. The cnditin shuld be a blean variable r a lgical expressin that will evaluate t blean values true r false. If we specify anything ther than a blean variable r an expressin that desn't evaluate t true r false, yu will get cmpilatin errr. Even if yu pass a String variable with a value f true r false, yu will still get a cmpilatin errr. Curly braces { are nt required when nly ne statement is there fr the if, "else if" r else clause, it is always a gd practice t use curly braces { t enclse if bdy fr better clarity, and t avid accidental mistakes. if, else-if, else The if statement blck can be fllwed by any number f ptinal else if statement blcks and ne ptinal else blck. Syntax: if, else-if, else if (cnd1) { //d smething
else if (cnd2) { //d smething else if (cnd3) { //d smething else { //d smething Only ne f the if r else if blcks will be executed fr the first true cnditin. If n if r else if cnditins are true, then the else blck is executed (if ne is available). Aviding accidental mistakes Usage f curly braces { fr enclsing if bdy Curly braces { are nt required when nly ne statement is there fr the if, "else if" r else clause. if(cnditin) statement1; Hwever, it is always a gd practice t use curly braces { t enclse if bdy fr better clarity, and t avid accidental mistakes like belw: if(cnditin) statement1; statement2; else statement3; Here, the if statement has nly a single statement, statement1. The secnd statement, statement2 is actually nt part f "if" blck and hence statement1 will end the if lp.
This is fllwed by the else clause, which is nw nt part f any if blck and hence yu will get a cmpilatin errr. A mre serius prblem ccurs when the extra statement is in the else blck as n exceptin is thrwn, but the statement will always get executed irrespective f the else blck. Anther prblem with nt using a blck statement is the dangling else prblem. In a cmplex nested if scenari, an else might be assciated with a different if than what we intended. The else keywrd is paired with the clsest if. Using the assignment peratr = instead f equality peratr == 1. If yu use the assignment peratr = instead f equality peratr ==, an assignment will happen instead f evaluatin. 2. When yu say if(x=y), y is assigned t x and x is evaluated. If x and y are blean, value f x is verwritten with the value f y and then x is evaluated. If the types cmpared are nt blean, yu will get a cmpile time errr as yu are evaluating smething which is nt a blean. Example: assignment peratr = instead f equality peratr == blean iscmpleted=false; if(iscmpleted=true){ System.ut.println("Executed!!!"); This will print "Executed!!!" Here, iscmpleted=true will assign true t iscmpleted verwriting its current value and then iscmpleted is evaluated; hence the evaluatin will always return true irrespective f the value f iscmpleted. The true is actually nt necessary in this case and we culd have just used if(iscmpleted) and avided the abve mistake. Cnditinal peratr The cnditinal peratr can be cnsidered as a limited frm f "if" statement. It is als called the ternary peratr as it has three cmpnents.
Syntax: cnditinal peratr LgicalExpressin? ThenExpressin : ElseExpressin If the LgicalExpressin evaluates t true, then the result f the ThenExpressin is returned. Otherwise the result f the ElseExpressin is returned. Example:cnditinal peratr int a = 1; int b = 5; int r; r= a>b?a : b; System.ut.println(r); This will print 5. Here the expressin (a>b) evaluate t false and hence the value f b is assigned t r. [Tip] The use f the cnditinal peratr is discuraged ver if-else due t its readability issues. Switch statement The switch statement prvide multi-branch selectins based upn integer, enumeratin, r Stringexpressin. Syntax: switch statement switch (expressin) { case value: statements; case value: statements; default: statements
Example:switch statement int abc = 7; switch (abc) { case 1: System.ut.println("ONE"); case 5: System.ut.println("FIVE"); default: System.ut.println("DEFAULT"); case 7: System.ut.println("SEVEN"); Imprtant prperties f switch statement in Java 1. Any f byte, char, shrt, and int data types (and their wrapper classes) can be used with an integer switch statement; lng and duble are nt allwed. 1. Crrespnding wrapper classes such as Character, Byte, Shrt, and Integer are als allwed. 2. Prir t Java 7, nly integer variables culd be used with a switch statement. 2. In switch statement, the value f a cnstant expressin is checked and then the cntrl is passed t a case that matches the cnstant expressin. 1. If n case matches the value f the expressin, cntrl is passed t the default clause, if present. 3. A default can cme in between the cases in the switch statement and desn't have t be always the last. But usually it is written in the end. 4. The break keywrd is used t end the cde sequence fr a case, and t exit the switch statement.
1. If there is n break fr a case, all the remaining cases (including default) will be executed until a break is fund r the end f switch is reached. 2. Break is nt always needed at the end f the default clause if default is the last case, but, it is usually included, especially if default is nt the last case. 5. With a String switch statement, 1. a null value assigned t a string variable used within a switch statement will lead t java.lang.nullpinterexceptin exceptin. 2. the cmparisn made within a switch statement is case-sensitive. Surce : http://javajee.cm/decisin-cntrl-cnstructs-in-java