You are on page 1of 10

Muntinlupa National High School -

Main Senior High


Department

Technical, Vocational and Livelihood


Information and Communications Technology

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

Decision making in programming is close to taking real-life decisions. Even in programming we


are faced with certain circumstances where we want to execute a certain block of code when some
condition is met. Java decision constructs taking statements empower you to decide, based on a
condition's outcome.

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

Instruction: For each question, encircle the single best answer.

1. How many options do you have when using a single if-else statement?

a. 3 b. 4 c. 2 d. 1

2. What is the output of the following code fragment?


int age = 8; if (age
< 21)
System.out.print("You can’t "); else
System.out.print("You can ");
System.out.println("go out.");

a. You can’t b. You can c. You can’t go out. d. You can go out.

3. What is the output of the following code fragment?


int age = 38; if (age
> 20)
System.out.print("You can "); else
{
System.out.print("You can’t ");
System.out.println("go out.");
}
a. You can’t b. You can c. You can’t go out. d. You can go out.

4. What is the output of the following code fragment?

int grade = 65;


if (grade > 74)
{
System.out.print("You pass ");
}
else
{
System.out.print("You fail ");
}

System.out.println("the exam.");

a. You pass b. You fail c. You pass the exam. d. You fail the exam.

5. What is the output of the following code fragment?

int grade = 80;


if (grade > 74)
{
System.out.print("You pass ");
}
else
{
System.out.print("You fail ");
}

System.out.println("the exam.");

a. You pass b. You fail c. You pass the exam. d. You fail the exam.

Learning Activities

Think of your everyday life as an example of a conditional. Depending on whether a condition


is met, what is something you do? Imagine, for instance, that you have a free period in your daily
schedule and use a conditional to decide what to do during that time. You think for it each day:
(example)

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.

Now we have the Java’s selection or conditional statements:

• 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.

Java Decision Constructs

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(condition) statement1; statement2;

// Here if the condition is true, if block // will


consider only statement1 to be inside // its
block.
Java Program
Flowchart

// Java program to illustrate if statement


package ifstatement;

public class IfStatement { public


static void main(String args[])
{
int i = 7;
if (i >
12)
System.out.println("7 is less than 12");

// This statement will be executed


// as if considers one statement by default
System.out.println("I am Not in if");
}
}

Output: I’m Not in if

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
}

Flowchart Java Program


// Java program to illustrate if-else statement package
ifelsestatement;

public class IfElseStatement {


public static void main(String args[])
{
int i = 7;
if (i <
12)
System.out.println("7 is smaller than 12");
else
System.out.println("7 is greater than 12");
}
}

Output: 7 is smaller than 12

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
}
}

Flowchart Java Program

// Java program to illustrate nested-if statement package


nestedifstatement;
public class NestedIfStatement
{
public static void main(String args[])
{
int i = 7;
if (i ==7) // First
condition
{
if (i < 10)
System.out.println(i+" is smaller than 10");

// 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 ");
}
}
}

Output: 7 is smaller than 10


7 is smaller than 20 too

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

An if statement is a boolean expression which is accompanied by one or more


if statement statements.
if
if...else An if statement can be preceded by another else statement, executing when
statement the boolean expression is false.
nested if You may use one if or else-if the statement(s) inside another if or
statement elsestatements
switch A switch statement does variable checking for equality among the lists of
statement values.
Self-Evaluation

1. In your words, define decision constructs.


______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________

2. Compare switch statement and nested if statement.


______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________

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

Instruction: Give the output of the following code fragment


public class PostTest1 }
{ Output: ______________ public class PostTest2
public static void main(String[] args)
{ int a = {
10; int b = 25; if public static void main(String[] args)
(a < b) { int x = 2;
{ boolean isValid = false;
System.out.print("x");
} else if (b if(!isActive)
> 5) { x =x+ 3; System.out.print(x);
{ isValid = true;
System.out.print("y"); } if(isValid)
} else { x=x-3;
{ System.out.print(x);
System.out.print("z"); }
} }
} } Output: ______________
Output: ______________
public class PostTest3
{ public class PostTest4
public static void main(String[] args) {
{ public static void main(String[] args)
int age = 18; {
if (a < int xy = 2;
17) { int z=10;
System.out.print("You’re a minor");
} if(z>xy)
else { z=z-xy;
{ System.out.print(z);
System.out.print("You’re an adult"); } else
} {
} xy=xy+3;
} System.out.print(xy);
}
}
}
Output: ______________
Authors: Albino C. Galgo Jr. and Marjory E. Ciervo References:
https://www.codesdope.com/practice/java-decide-if-or-else/ https://study.com/academy/practice/quiz-
worksheet-java-conditional-statements.html
https://beginnersbook.com/2017/08/if-else-statement-in-java/
https://www.learnjavaonline.org/en/Conditionals https://www.inf.unibz.it/~calvanese/teaching/04-05-
ip/lecture-notes/uni05.pdf

You might also like