Witscale Test Center

5.2 Conditional statements: if, if-else and switch


5.2 Conditional statements: if, if-else and switch

In Java, you can selectively execute a piece of code based on certain condition. Java provides two types of conditional statements, the if (if and if-else) and switch statements. When you use the conditional statements in your program, you can give a condition to select the course of action to take. You can write a simple conditional code for choosing between two execution paths using if-else statement. If you need to write for multiple conditions with multiple execution paths, you can use nested if statements. Moreover, if the multiple conditions are based on single argument then you can use the switch statement. Let us start with the simplest conditional statement, the if statement.