MODULE 4 – CONDITIONAL STATEMENTS
Pre-test: Find the error: As the instruction stated, find the error and write the correct line on a one-half cross-
wised sheet of paper or element for the said program stated below: (10 points)
class IfElse {
public static main(String[] args) {
int number = 10
// checks if number is greater than 0
if (number => 0) {
System.out.printline("The number is positive.");
}
else {
System out.println("The number is not positive.")
}
System.out.println("This statement is always executed.);
;
{
}
MODULE 4 – CONDITIONAL STATEMENTS
Lesson 1 – Conditional Statements
Java, like all other programming languages, is equipped with specific statements that allow us to check a
condition and execute certain parts of code depending on whether the condition is true or false. Such statements
are called conditional, and are a form of composite statement.
In Java, there are two forms of conditional statements:
• the if-else statement, to choose between two alternatives;
• the switch statement, to choose between multiple alternatives.
Java supports the usual logical conditions from mathematics:
o Less than: a < b
o Less than or equal to: a <= b
o Greater than: a > b
o Greater than or equal to: a >= b
o Equal to a == b
o Not Equal to: a != b
Java has the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
1.1 - The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is true.
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
MODULE 4 – CONDITIONAL STATEMENTS
1.2 - The else statement
Use the else statement to specify a block of code to be executed if the condition is false.
Example explained:
In the example above, time (20) is greater than 18, so the condition is false. Because of this, we
move on to the else condition and print to the screen "Good evening". If the time was less than
18, the program would print "Good day".
1.3 - The else-if statement
Use the else if statement to specify a new condition if the first condition is false.
MODULE 4 – CONDITIONAL STATEMENTS
Example explained:
In the example above, time (22) is greater than 10, so the first condition is false.
The next condition, in the else if statement, is also false, so we move on to the else condition
since condition1 and condition2 is both false - and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
1.4 - Switch-case statement
Use the switch statement to select one of many code blocks to be executed.
This is how it works:
o The switch expression is evaluated once.
o The value of the expression is compared with the values of each case.
o If there is a match, the associated block of code is executed.
o The break and default keywords are optional, and will be described later in this chapter
MODULE 4 – CONDITIONAL STATEMENTS
1.5 - The break Keyword
When Java reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
1.6 - The default Keyword
The default keyword specifies some code to run if there is no case match:
Evaluation: Create a Program: Using your DCoder App, create a program based on these scenarios:
MODULE 4 – CONDITIONAL STATEMENTS
1. Calculator program using if-else program
2. Calculator program using switch-case program
3. Metric system using if-else program
4. Metric system using switch-case program
Some Notes:
1. Post-test will be recorded and the instructor will collect your answers.
2. Activities will also be recorded and the instructor will collect your exercises.
Bibliography/References:
https://www.inf.unibz.it/~calvanese/teaching/04-05-ip/lecture-notes/uni05.pdf
https://www.w3schools.com/java/java_conditions.asp
https://www.w3schools.com/java/java_switch.asp