Professional Documents
Culture Documents
Supplementary Learning
(Java) NC III
[Quarter 1, Module 6]
(Illustration is created from canva.com)
Supplementary Learning Material
Computer Programming (Java) NC III
Quarter 1, Module 6
Instruction
This module is designed to help you learn Java Programming at home. This is intended to
deliver useful and stimulating learning experiences to help you improve your programming skills.
By the end of each lesson you will revisit the related concepts. You will then discover what this
learning session is all about. Once the concepts are implemented, you must do self-check exercises
which will lead to an application of both concepts and skills. You will be driven by how to manage the
learning process. Introduction
As you complete this module, think of things or events on how you will decide about them in
your daily life.
Essential Learning Competency
• Understand that conditionals are statements made while meeting those conditions.
• Evaluate a conditional statement and with given input, predict the result.
• Write conditional statements, set out criteria for when certain actions should be taken by a
program.
Pre-test
1. How many options do you have when using a single if-else statement?
a. 3 b. 4 c. 2 d. 1
a. You can’t b. You can c. You can’t go out. d. You can go out.
System.out.println("the exam.");
a. You pass b. You fail c. You pass the exam. d. You fail the exam.
System.out.println("the exam.");
a. You pass b. You fail c. You pass the exam. d. You fail the exam.
Learning Activities
If I have online games later, I’ll go online and get some homework done. Else, I’ll hang out with
my friends playing online games.
Write down your conditional here. If you need more space, please feel free to add as many other
conditions as you want, and you can work on an additional sheet of paper.
If _____________________, ________________________________
Else, ___________________________________________________
Decision making structures have one or more conditions to be assessed or checked by the
system, along with a statement or statements to be executed if the condition is considered to be valid,
and optionally, other statements to be executed if the condition is considered to be false.
A programming language uses control statements to control the flow of program execution,
depending on certain conditions. They are used to advance and branch the execution path, based on
changes to a program's status.
• if
• if-else
• nested-if/ nested if-else/ nested if-else if-else
• if-else-if
• switch-case
These statements allow users to control the execution flow of your program based on the
conditions that are only known during run time.
Don’t you worry, we will go through each item. So, stay focus and follow this module carefully.
So, let’s start.
if: if statement is the basic statement of making decision. It is used to decide whether a particular
statement or block of statements will be executed, i.e. if a certain condition is true then a statement
block will be executed otherwise not.
Syntax: if(condition)
{
// Statements to execute if
// condition is true
}
Here, either true or false would be the result after evaluation. If the statement accepts boolean values-if
the value is true, then the statement block under it will be executed.
After if(condition) part, if we do not have or put the left curly brace '{' and the right curly brace
'}, then by default if statement would assume that the immediate one statement is within its row. For
instance,
if-else: The if statement alone informs us that it will execute a series of statements if a condition
is valid and if the condition is false it will not. And what if we want to do something else if the condition is false.
Here comes the other declaration. When the condition is false, we can use the other statement with if statement
to execute a code block.
Syntax:
if (condition)
{
// Executes this block if the condition is true
}
else
{
// Executes this block if the condition is false
}
nested-if: A nested if is an if statement which has an if-else statement inside it. Java allows us to have nested if
when there are certain conditions needed to be tested further.
In other words, an if-else statement is inside of either “if” or “if-else” statement then this is called nested if or
nested if-else conditional statement.
Syntax: if (condition1)
{
// Executes when condition1 is true if
(condition2)
{
// Executes when condition2 is true
}
}
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i <
20)
System.out.println(i+" is smaller than 20 too");
else
System.out.println(i+" is greater than 20 ");
}
}
}
if-else-if ladder: In this conditional statement, a user can choose between several choices. The if statements
are executed from top to bottom. As soon as one of the conditions controlling the if is true, it is executed
immediately, and bypasses the rest of the if statement ladder. When none of the conditions are valid, then the
last else statement will be executed.
Syntax:
if (condition)
statement; else if
(condition) statement;
.
.
else
statement;
switch-case: The switch statement is a reference for multiway branches. It offers simple way of
dispatching execution to various code parts depending on the expression value.
• Expression may be an enumeration of type int, chart, int, or short. Expression may also be of the
String starting with the JDK7 version.
• Switch doesn't allow duplicate case values.
• The default statement may be present.
• After a series of statements of switch, the break statement is used as termination point.
• The break statement is optional. In its absence, the execution continues into the next statement.
Java programming language offers decision-making statements of the following types with its
description:
sion
Statement Description
Type
3. Write down a conditional statement of three of your daily activities using the format below:
a.
If _____________________, _____________________________________
Else, ________________________________________________________
b.
If _____________________, _____________________________________ Else,
________________________________________________________
c.
If _____________________, _____________________________________
Else, ________________________________________________________ Post-Test