Conditional Statements in Java
Conditional statements allow you to control the flow of your program by executing certain code blocks based on a condition. The most common conditional statements in Java are if-else statements and switch statements.
Example code:
csharp
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
// and so on for the other days of the week...
default:
System.out.println("Invalid day");
}
No comments:
Post a Comment