


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



http://www.tutorialspoint.com/java/java_decision_making.htm Copyright Ā© tutorials point.com
if(Boolean_expression) { //Statements will execute if the Boolean expression is true }
public class Test { public static void main(String args[]){ int x = 10 ; if( x < 20 ){ System.out.print("This is if statement"); } } }
This is if statement
if(Boolean_expression){ //Executes when the Boolean expression is true }else{ //Executes when the Boolean expression is false }
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 statement The if...else if...else St at ement :
Synt ax:
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 30 Nest ed if...else St at ement :
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 $ What is Next?