Conditional Structures (if, else, switch)

BerkayHasip
3 min readDec 27, 2023

--

Java conditional structures are used to control the flow of a program based on some conditions. There are three main types of conditional structures in Java: if, else, and switch.

If Statement

The if statement is used to execute a block of code only if a certain condition is true. The syntax of the if statement is:

if (condition) {
// code to execute if condition is true
}

The condition is a boolean expression that evaluates to either true or false. If the condition is true, the code inside the curly braces is executed. If the condition is false, the code is skipped.

For example, the following code checks if a number is positive or not:

int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}

The output of this code is:

The number is positive.

Else Statement

The else statement is used to execute a block of code if the condition of the if statement is false. The syntax of the else statement is:

if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

The else statement must be placed after the if statement. Only one else statement can be used for each if statement.

For example, the following code checks if a number is positive, negative, or zero:

int number = 0;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}

The output of this code is:

The number is zero.

Switch Statement

The switch statement is used to execute different blocks of code based on the value of a variable or an expression. The syntax of the switch statement is:

switch (variable or expression) {
case value1:
// code to execute if variable or expression equals value1
break;
case value2:
// code to execute if variable or expression equals value2
break;
...
default:
// code to execute if none of the cases match
break;
}

The switch statement compares the value of the variable or expression with each case value. If a match is found, the code after the case is executed. The break statement is used to exit the switch statement after executing the code. The default case is optional and is used to execute some code if none of the cases match.

For example, the following code prints the name of a month based on a number:

int month = 4;
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
break;
}

The output of this code is:

April

Summary

In this article, we learned about the Java conditional structures: if, else, and switch. We saw how to use them to control the flow of a program based on some conditions. We also saw some examples of how to use them in different scenarios.

--

--

BerkayHasip
BerkayHasip

Written by BerkayHasip

Website : berkayhasip.com / Hi! Student , Java lover. Self-taught, blog my journey. Create apps, freelance, advise, and blog. Eager for diverse projects

No responses yet