JAVA TUTORIALS - Java - Decision Making
Java - Decision Making
ADVERTISEMENTS
Syntax:
if(Boolean_expression) { //Statements will execute if the Boolean expression is true }ADVERTISEMENTSExample:
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if statement"); } } }ADVERTISEMENTS
This is if statementSyntax:
if(Boolean_expression){ //Executes when the Boolean expression is true }else{ //Executes when the Boolean expression is false }Example:
public class Test { public static void main(String args[]){ int x = 30; if( x < 20 ){ System.out.print("This is if statement"); }else{ System.out.print("This is else statement"); } } }
This is else statementSyntax:
if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true }else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true }else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true }else { //Executes when the none of the above condition is true. }Example:
public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is else statement"); } } }
Value of X is 30Syntax:
if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true } }Example:
public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } } }
X = 30 and Y = 10Syntax:
switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements }Example:
public class Test { public static void main(String args[]){ //char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }
$ java Test Well done Your grade is a C $