Java - Conditional Statements
Conditional statements allows you to control the flow of your program based on certain conditions. Conditional statements help your program make decisions and execute different blocks of code based on the evaluation of expressions.
There are three main types of conditional statements in Java:
if Statement
The if statement is the most basic type of conditional statement in Java. It allows you to execute a block of code if a certain condition is true. The syntax for the if statement is as follows:
if (condition) {
// Code to be executed if the condition is true
}
Here's an example that demonstrates the usage of the if statement:
class IfStatement{
public static void main(String[] args){
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
}
}
}
Output:
x is greater than 5
if-else Statement
The if-else statement provides an alternative block of code to be executed if the condition in the if statement is false. The syntax for the if-else statement is as follows:
if (condition) {
// Code to be executed if the condition is true
}
else {
// Code to be executed if the condition is false
}
Here's an example that demonstrates the usage of the if-else statement:
class Age{
public static void main(String[] args){
int age = 18;
if (age >= 18) {
System.out.println("You are eligible to vote");
}
else {
System.out.println("You are not eligible to vote");
}
}
}
Output:
You are eligible to vote
else-if Statement
The else-if statement allows you to test multiple conditions and execute different blocks of code based on the evaluation of those conditions. The syntax for the if-else if-else statement is as follows:
if (condition1) {
// Code to be executed if condition1 is true
}
else if (condition2) {
// Code to be executed if condition2 is true
}
else {
// Code to be executed if all conditions are false
}
Here's an example that demonstrates the usage of the if-else statement:
class Grade{
public static void main(String[] args){
int score = 80;
if (score >= 90) {
System.out.println("Your grade is A");
}
else if (score >= 80) {
System.out.println("Your grade is B");
}
else if (score >= 70) {
System.out.println("Your grade is C");
}
else {
System.out.println("Your grade is F");
}
}
}
Output:
Your grade is B
Complex Conditional Statements
It's important to note that the conditions in the if statements are boolean expressions, meaning they evaluate to either true or false. You can use comparison operators (>, <,>=, <=,==, !=) and logical operators (&&, ||, !) to form complex conditions.
Complex conditional statements allow you to express more intricate decision-making processes in your Java programs. They enable you to evaluate multiple conditions simultaneously and execute code blocks based on the combined result of those conditions.
Logical OperatorsJava provides three logical operators that can be used to create complex conditions:
- AND (&&) Operator: The AND operator returns true if both of its operands are true; otherwise, it returns false.
- OR (||) Operator:: The OR operator returns true if at least one of its operands is true; otherwise, it returns false.
- NOT (!) Operator:: The NOT operator is a unary operator that reverses the logical state of its operand. If the operand is true, it returns false, and if the operand is false, it returns true.
See table below:
&& (AND) Table
A | B | A && B |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
|| (OR) Table
A | B | A || B |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
! (NOT) Table
A | !A |
---|---|
True | False |
False | True |
Parentheses are used to group conditions and control the order of evaluation. They allow you to specify the precedence of conditions within a complex conditional statement.
Here's an example that demonstrates the usage of complex conditional statements:
class Drive{
public static void main(String[] args){
int age = 25;
boolean hasLicense = false;
if (age >= 18 && hasLicense) {
System.out.println("You are eligible to drive");
}
else {
System.out.println("You are not eligible to drive");
}
}
}
Output:
You are eligible to drive
In this example, the complex condition in the if statement checks if the person is at least 18 years old (age >= 18) and has license. If both conditions are true, the code block within the if statement is executed.
Complex conditional statements allow you to create flexible and dynamic decision-making structures in your Java programs. By combining conditions using logical operators and utilizing parentheses to control evaluation order, you can handle a wide range of scenarios and make complex decisions based on multiple factors.
Nested if Statement
You can also nest if statements within other if statements to create more complex decision-making structures. This allows you to check for multiple conditions and execute different code blocks accordingly.
Here's an example of nested if statements:
class OddEven{
public static void main(String[] args){
int num = 10;
if (num > 0) {
if (num % 2 == 0) {
System.out.println("The number is positive and even");
}
else {
System.out.println("The number is positive and odd");
}
}
else {
System.out.println("The number is not positive");
}
}
}
Output:
The number is positive and even
In this example, the outer if statement checks if the number is positive. If it is, the inner if statement checks if the number is even by using the modulo operator (%). Based on the evaluation of these conditions, the corresponding message is printed.
Switch Statement
Another type of conditional statement in Java is the switch statement. It provides a way to execute different blocks of code based on the value of a variable or expression. The switch statement compares the value against multiple cases and executes the code block that matches the value.
Here's the syntax for the switch statement:
switch (expression) {
case value1:
// Code to be executed if expression matches value1
break;
case value2:
// Code to be executed if expression matches value2
break;
// Add more cases as needed
default:
// Code to be executed if expression doesn't match any case
}
Here's an example that demonstrates the usage of the switch statement:
class Day{
public static void main(String[] args){
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
// Add more cases as needed
default:
dayName = "Invalid day";
}
System.out.println("Today is " + dayName);
}
}
Output:
Today is Tuesday
In this example, the switch statement evaluates the value of the variable day and assigns the corresponding day name to the variable dayName. If none of the cases match, the default case is executed.
Conditional statements are powerful tools for controlling the flow of your program based on different conditions. They allow you to make decisions, execute different code blocks, and create dynamic and flexible programs. Understanding how to use if statements, if-else statements, if-else if-else statements, nested if statements, and switch statements is essential for writing effective Java programs.
Basic Topics
- Introduction to Java
- Data Types and Variables
- User Input
- Conditional Statements
- Loops
- Methods
- Arrays