Lesson13 - DLP14 - Java Conditional Statements

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ARELLANO UNIVERSITY – PLARIDEL CAMPUS

53 General Kalentong St. Mandaluyong


Senior High School
Dynamic Learning Program

Name: Date:
Grade/Section: Rating:
Teacher:

SUBJECT Computer Programming II


TOPIC JAVA CONDITIONAL STATEMENTS
REFERENCE  https://www.tutorialspoint.com/java/java_basic_syntax.htm
 w3schools.com/java/java_ref_keywords.asp

OBJECTIVES At the end of the lesson, students should be able to;


a. Know what conditional statement is;
b. Differentiate the use of each conditional statement;
c. Use different conditional statements in JAVA.

CONCEPTS
Conditional Statements

Allows a program to take action based on the given condition. It makes our program
smarter. Typically, it compares two values so that our program can decide what
action should be taken.

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.

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition


is true.
Syntax

if (condition) {

// block of code to be executed if the condition is true

}
ARELLANO UNIVERSITY – PLARIDEL CAMPUS
53 General Kalentong St. Mandaluyong
Senior High School
Dynamic Learning Program

Example

if (20 > 18) {

System.out.println("20 is greater than 18");

The else Statement

Use the else statement to specify a block of code to be executed if the condition
is false.

Syntax

if (condition) {

// block of code to be executed if the condition is true

} else {

// block of code to be executed if the condition is false

Example

int time = 20;

if (time < 18) {

System.out.println("Good day.");

} else {

System.out.println("Good evening.");

// Outputs "Good evening."


ARELLANO UNIVERSITY – PLARIDEL CAMPUS
53 General Kalentong St. Mandaluyong
Senior High School
Dynamic Learning Program

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {

// block of code to be executed if condition1 is true

} else if (condition2) {

// block of code to be executed if the condition1 is false and condition2 is true

} else {

// block of code to be executed if the condition1 is false and condition2 is false

Example
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
Java Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
ARELLANO UNIVERSITY – PLARIDEL CAMPUS
53 General Kalentong St. Mandaluyong
Senior High School
Dynamic Learning Program

}
Example
int day = 4;
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;
}
// Outputs "Thursday" (day 4)

The default Keyword

The default keyword specifies some code to run if there is no case match:

QUESTIONS/ACTIVITY QUESTIONS:
1.It is use to specify a block of code to be executed if the condition is false.
Else statement
2.It is use to specify a new condition to test, if the first condition is false
Else if statement
3.It is use to specify a block of code to be executed, if a specified condition is true
If statement
ARELLANO UNIVERSITY – PLARIDEL CAMPUS
53 General Kalentong St. Mandaluyong
Senior High School
Dynamic Learning Program

4. It is use to specify many alternative blocks of code to be executed.


Switch
5. It allows a program to take action based on the given condition.
Conditional Statement

ANALYSIS Let the students answer the questions.

ASSESSMENT/ PERFORMANCE TASK #5


APPLICATION In any text editor, create a program that will ask the user to input the grade of the
student in first quarter and second quarter. Let the program compute the average
and display the description based on its value.

If average:
Average Scale Description
90 - 100 Outstanding
85 - 89 Very Satisfactory
80 - 84 Satisfactory
75 - 79 Fairly Satisfactory
Below 75 Did Not Meet
Expectations

(Submit a picture of your code and output)


ARELLANO UNIVERSITY – PLARIDEL CAMPUS
53 General Kalentong St. Mandaluyong
Senior High School
Dynamic Learning Program
ARELLANO UNIVERSITY – PLARIDEL CAMPUS
53 General Kalentong St. Mandaluyong
Senior High School
Dynamic Learning Program

Prepared by: Mr. John Louie M. Miguel


Teacher

Checked by: Mr. Ace Bhyron Cancino


ICT- Coordinator

Noted by: Mr. Janno H. Vizco


Academic Coordinator

Mrs. Junna C. Bulay


Assistant Principal

Approved by: Mrs. Vilma S. Dominguez


Principal

You might also like