Java Tutorial
if (condition) statement1; else statement2;if the condition is true, statement1 is executed, otherwise statement2 is executed. else block is optional, if else block is not present, program execution goes to next statement. it can be using curly braces (that is block) for multiple statements in if else blocks.
if (condition) { statements; } else { statements; }Example:
// if statement in java class IfElseTest { public static void main(String args[]) { boolean a = true; boolean b = false; if (a || b) { System.out.println("Either a or b is true or both a and b is true"); } else { System.out.println("Both a and b is false"); } } }Output:
Either a or b is true or both a and b is true
// Nested if in java class IfElseTest { public static void main(String args[]) { boolean a = true; boolean b = false; if (a || b) { if ( a && b) { System.out.println("Both a and b is true"); } else { System.out.println("Either a or b is true"); } } else { System.out.println("Both a and b is false"); } } }Output:
Either a or b is true
if (condition) statement; else if(condition) statement; else if(condition) statement //... else statment;Example:
//if else if ladder statements in java class IfElseIfTest { public static void main(String args[]) { boolean a = true; boolean b = false; if (!a) { System.out.println("a is not true"); } else if ( a && b) { System.out.println("Both a and b is true"); } else if (a || b) { System.out.println("Either a or b is true"); } else { System.out.println("Both a and b is false"); } } }Output:
Either a or b is true
switch(expression) { case value1: Statements; break; case value2: Statements; break; //... default: statments; }Switch expression must be of type byte, short, int and char. Each of the value specified in case must be type compatible with the switch expression.
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page