Conditional statements are fundamental in programming as they allow you to execute different blocks of code based on certain conditions. In Java, the primary conditional statements are if
, if-else
, if-else-if
, and switch
.
if
Statement
if
StatementThe if
statement is used to execute a block of code only if a specified condition is true.
Syntax:
Example:
public class Main { public static void main(String[] args) { int number = 10; if (number > 0) { System.out.println("The number is positive."); } } }
Explanation:
- The condition
number > 0
is checked. - If the condition is true, the message "The number is positive." is printed.
if-else
Statement
if-else
StatementThe if-else
statement provides an alternative block of code to execute if the condition is false.
Syntax:
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
Example:
public class Main { public static void main(String[] args) { int number = -10; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } } }
Explanation:
- The condition
number > 0
is checked. - If the condition is true, the message "The number is positive." is printed.
- If the condition is false, the message "The number is not positive." is printed.
if-else-if
Ladder
if-else-if
LadderThe if-else-if
ladder is used to check multiple conditions.
Syntax:
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 }
Example:
public class Main { public static void main(String[] args) { 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."); } } }
Explanation:
- The condition
number > 0
is checked first. - If the condition is true, the message "The number is positive." is printed.
- If the condition is false, the next condition
number < 0
is checked. - If the second condition is true, the message "The number is negative." is printed.
- If both conditions are false, the message "The number is zero." is printed.
switch
Statement
switch
StatementThe switch
statement is used to execute one block of code among many based on the value of a variable.
Syntax:
switch (expression) { case value1: // code to be executed if expression == value1 break; case value2: // code to be executed if expression == value2 break; // you can have any number of case statements default: // code to be executed if none of the cases are true }
Example:
public class Main { public static void main(String[] args) { int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid day"); break; } } }
Explanation:
- The value of
day
is checked against each case. - If
day
is 3, the message "Wednesday" is printed. - The
break
statement is used to exit the switch block once a match is found. - If no match is found, the
default
case is executed.
Practical Exercises
Exercise 1:
Write a Java program that checks if a number is even or odd using an if-else
statement.
Solution:
public class Main { public static void main(String[] args) { int number = 5; if (number % 2 == 0) { System.out.println("The number is even."); } else { System.out.println("The number is odd."); } } }
Exercise 2:
Write a Java program that prints the name of the month based on the number (1 for January, 2 for February, etc.) using a switch
statement.
Solution:
public class Main { public static void main(String[] args) { 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; } } }
Common Mistakes and Tips
- Forgetting the
break
statement in aswitch
case: This can lead to fall-through, where multiple cases are executed. - Using
==
for string comparison: Use.equals()
method for comparing strings. - Not covering all possible cases: Always include a
default
case in aswitch
statement to handle unexpected values.
Conclusion
In this section, you learned about the different types of conditional statements in Java, including if
, if-else
, if-else-if
, and switch
. These constructs allow you to control the flow of your program based on various conditions. Practice the exercises provided to reinforce your understanding and prepare for more complex control flow mechanisms in the next module.
Java Programming Course
Module 1: Introduction to Java
- Introduction to Java
- Setting Up the Development Environment
- Basic Syntax and Structure
- Variables and Data Types
- Operators
Module 2: Control Flow
Module 3: Object-Oriented Programming
- Introduction to OOP
- Classes and Objects
- Methods
- Constructors
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Module 4: Advanced Object-Oriented Programming
Module 5: Data Structures and Collections
Module 6: Exception Handling
Module 7: File I/O
Module 8: Multithreading and Concurrency
- Introduction to Multithreading
- Creating Threads
- Thread Lifecycle
- Synchronization
- Concurrency Utilities
Module 9: Networking
- Introduction to Networking
- Sockets
- ServerSocket
- DatagramSocket and DatagramPacket
- URL and HttpURLConnection