Conditional Statements In Java
Let’s see the following conditional statements
1. if statement
2. nested if statement
3. if-else statement
4. if-else-if statement
5. Switch Case Statement
if statement:
The if statement is the most basic of all the control flow statements. The if statement tells our program to execute a certain section of code only if a particular test evaluates to true.
Syntax:
if(condition){ Statement(s); }
Sample Program:
package ClassThreeControlFlowStatements; public class IfStatement { public static void main(String[] args) { int num = 100; if (num<=100){ System.out.println("Value of num is "+num); } } }
Nested if statement:
An if statement inside another the statement. If the outer if condition is true then the section of code under outer if condition would execute and it goes to the inner if condition. If inner if condition is true then the section of code under inner if condition would execute.
Syntax:
if(condition_1) { Statement1(s); if(condition_2) { Statement2(s); } }
Sample Program:
package ClassThreeControlFlowStatements; public class NestedIfStatement { public static void main(String[] args) { int num = 100; if (num<=100){ System.out.println("Value of num is "+num); if (num>50){ System.out.println("Value of num is "+num); } } } }
if-else statement:
If a condition is true then the section of code under if would execute else the section of code under else would execute.
Syntax:
if(condition) { Statement(s); } else { Statement(s); }
Sample Program:
package ClassThreeControlFlowStatements; public class IfElseStatement { public static void main(String[] args) { int num = 100; if (num>100){ System.out.println("Value is greater than 100"); }else { System.out.println("Value is less than 100"); } } }
if-else-if statement:
if(condition_1) { /*if condition_1 is true execute this*/ statement(s); } else if(condition_2) { /* execute this if condition_1 is not met and * condition_2 is met */ statement(s); } else if(condition_3) { /* execute this if condition_1 & condition_2 are * not met and condition_3 is met */ statement(s); } . . . else { /* if none of the condition is true * then these statements gets executed */ statement(s); }
Sample Program:
package ClassThreeControlFlowStatements; public class IfElseIfStatement { public static void main(String[] args) { int marks = 76; char grade; if (marks >= 80) { grade = 'A'; } else if (marks >= 70) { grade = 'B'; } else if (marks >= 60) { grade = 'C'; } else if (marks >= 50) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
Switch Case:
The switch statement in Java is a multi branch statement. We use this in Java when we have multiple options to select. It executes particular option based on the value of an expression.
Switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types such as Character, Byte, Short, and Integer.
Syntax:
switch(expression) { case valueOne: //statement(s break; case valueTwo: //statement(s break; : : : default: //optional //statement(s) //This code will be executed if all cases are not matched }
Sample Program:
Switch Case without break statement:
package ClassThreeControlFlowStatements; public class SwitchCaseProgram { public static void main(String[] args) { /*The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases.*/ int num=200; switch(num){ case 100: System.out.println("Value of Case 1 is "+num); case 200: System.out.println("Value of Case 2 is "+num); default: System.out.println("Value of default is "+num); } } }
Switch Case with break statement:
package ClassThreeControlFlowStatements; public class SwitchCaseProgramWithBreak { public static void main(String[] args) { int num=200; switch(num){ case 100: System.out.println("Value of Case 1 is "+num); break; case 200: System.out.println("Value of Case 2 is "+num); break; // In this only case 2 will be executed and rest of the cases will be ignored. default: System.out.println("Value of default is "+num); } } }
Must Read: Java Tutorial