Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

ComputerProgramming.grade11.Q3.w5 for STUDENT

The document is a learning activity sheet for Grade 11 Computer Programming (Java) focusing on loop constructs. It outlines objectives, materials needed, and provides detailed explanations of for loops, while loops, and do-while loops, including syntax, examples, and applications in GUI programming. Additionally, it includes assessments and exercises for students to demonstrate their understanding of the concepts covered.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ComputerProgramming.grade11.Q3.w5 for STUDENT

The document is a learning activity sheet for Grade 11 Computer Programming (Java) focusing on loop constructs. It outlines objectives, materials needed, and provides detailed explanations of for loops, while loops, and do-while loops, including syntax, examples, and applications in GUI programming. Additionally, it includes assessments and exercises for students to demonstrate their understanding of the concepts covered.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Computer Programming (Java) – Grade 11

Learning Activity Sheets


Quarter 3

Republic Act 8293, section 176 states that: No copyright shall subsist in any
work of the Government of the Philippines. However, prior approval of the government
agency or office wherein the work is created shall be necessary for exploitation of such
work for profit. Such agency or office may, among other things, impose as a condition
the payment of royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this activity sheet are owned by their respective copyright
holders. Every effort has been exerted to locate and seek permission to use these
materials from their respective copyright owners. The publisher and authors do not
represent nor claim ownership over them.

Published by the Department of Education – Schools Division of Tacloban City


Schools Division Superintendent: Mariza S. Magan
Assistant Schools Division Superintendent: Edgar Y. Tenasas

Development Team of the Activity Sheet

Writers: Maneth B. Vivero


Leah Amor V. Felicilda
Evaluator: Almer H. Bugal
Management Team:
CID Chief: Mark Chester Anthony G. Tamayo
Division EPS of LRMS: Gretel Laura M. Cadiong
Division Learning Area EPS: Evelyn P. Malubay

Department of Education - Region No. VIII – Schools Division Office of


Tacloban City
Office Address: Real St., Tacloban City
COMPUTER PROGRAMMING (JAVA) GRADE 11
LEARNING ACTIVITY SHEET
Quarter 3 Week 5

Objectives: (TLE_ICTJAVA11-12POAD-IIf-i-29)
• Demonstrate using loop constructs in accordance with java framework.
• Create program that applies looping statements (for, while, do-while loop) applying
API/GUI in the program.

Materials:
• Computer
• NetBeans IDE

Let’s kick it off!


• What comes in your mind if you hear the word loop?
• Take a good look, analyse and try to understand the figure below.

Figure 1.0
https://www.tutorialspoint.com/java/java_loop_control.htm

1|Q3W5
Are you taking it?
1. What is loop?
2. What are the different loop statements?
3. What is its syntax?
4. How to apply it in programming applying GUI?

Here’s how it is!

Java Loop Controls

In computer programming, loops are used to repeat a block of code. For example, if you want
to show a message 100 times, then rather than typing the same code 100 times, you can use a
loop. A loop statement allows us to execute a statement or group of statements multiple
times.
Looping in Java is defined as performing some lines of code in an ordered fashion until a
condition is false. The condition is important because we do not want the loop to be running
forever. As soon as this condition is false, the loop stops.
In Java there are three primary types of loops:-
1. for loop
2. while loop
3. do-while loop

For Loop vs While Loop vs Do While Loop

Comparison For Loop While Loop Do While Loop


Introduction The Java for loop is a The Java while loop The Java do while
control flow statement that is a control flow loop is a control flow
iterates a part of statement that statement that
the programs multiple times. executes a part of theexecutes a part of the
programs repeatedly programs at least
on the basis of given once and the further
boolean condition. execution depends
upon the given
boolean condition.
When to use If the number of iteration is If the number of If the number of
fixed, it is recommended to iteration is fixed, it is iteration is fixed, it is
use for loop. recommended to use recommended to use
for loop. for loop.
Syntax for(init;condition;incr/decr){ while(condition){ do{
// code to be executed //code to be executed //code to be executed
} } }while(condition);

Example //for loop //while loop //do-while loop


for(int i=1;i<=10;i++){ int i=1; int i=1;

2|Q3W5
System.out.println(i); while(i<=10){ do{
} System.out.println(i); System.out.println(i);
i++; i++;
} }while(i<=10);

Output of 1 1 1
the given 2 2 2
example 3 3 3
code 4 4 4
snippet: 5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10

Java For Loop


Syntax:

for (initialization; condition; inc/dec){


//statement or code to be executed
}

In simple for loop we can initialize the variable, check condition and increment/decrement
value. It consists of four parts:

1. Initialization: It is the initial condition which is executed once when the loop starts.
Here, we can initialize the variable, or we can use an already initialized variable. It is
an optional condition.
2. Condition: It is the second condition which is executed each time to test the condition
of the loop. It continues execution until the condition is false. It must return boolean
value either true or false. It is an optional condition.
3. Statement: The statement of the loop is executed each time until the second condition
is false.
4. Increment/Decrement: It increments or decrements the variable value. It is an
optional condition.

3|Q3W5
Example: Output:

//Java Program to demonstrate the example of for loop 1


//which prints table of 1 2
public class ForExample {
3
public static void main(String[] args) {
4
//Code of Java for loop
for(int i=1;i<=10;i++){ 5

System.out.println(i); 6
} 7
}
8
}
9

10

While Loop

A while loop is a control flow statement that allows code to be executed repeatedly based
on a given Boolean condition. The while loop can be thought of as a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements... // body of the loop
}

• While loop starts with the checking of condition. If it evaluated to true, then the loop
body statements are executed otherwise first statement following the loop is executed.
For this reason it is also called Entry control loop
• Once the condition is evaluated to true, the statements in the loop body are executed.
Normally the statements contain an update value for the variable being processed for
the next iteration.
• When the condition becomes false, the loop terminates which marks the end of its life
cycle.

Example Program:
// Java program to illustrate while loop
class whileLoopDemo
{
public static void main(String args[])
{

4|Q3W5
int x = 1;
Output:
// Exit when x becomes greater than 4 Value of x:1
while (x <= 4)
{ Value of x:2
System.out.println("Value of x:" + x); Value of x:3
// Increment the value of x for Value of x:4
// next iteration
x++;
}
}
}

Do while Loop
do while loop is similar to while loop with only difference that it checks for condition after
executing the statements, and therefore is an example of Exit Control Loop.

Syntax:
do
{
statements..
}
while (condition);

1. do while loop starts with the execution of the statement(s). There is no checking of any
condition for the first time.
2. After the execution of the statements, and update of the variable value, the condition is
checked for true or false value. If it is evaluated to true, next iteration of loop starts.
3. When the condition becomes false, the loop terminates which marks the end of its life
cycle.
4. It is important to note that the do-while loop will execute its statements atleast once
before any condition is checked, and therefore is an example of exit control loop.

Example Program
// Java program to illustrate do-while loop
class dowhileloopDemo
{
public static void main(String args[])
{
int x = 21;
do
{

5|Q3W5
// The line will be printed even
// if the condition is false Output:
System.out.println("Value of x:" + x); Value of x: 21
x++;
}
while (x < 20);
}
}

Applying GUI

1. Create New JFrame Form


2. Add Button
3. Set the Button Text Property : Display
4. Change Varible name of the button :
btndispla

Double click the button and you will be directed to the source code. Insert the following
source code.

package viveromodular;
import javax.swing.*; // import java
public class NewJFrame extends javax.swing.JFrame {
JFrame frame; // Declare Jframe
int x=1; // Declare variable x and initialize to 1. This is used for looping
public NewJFrame() {
initComponents();
}
private void btndisplayActionPerformed(java.awt.event.ActionEvent evt) {
//insert the following code for looping. Displays Meesage 4 times
while(x<=4){
JOptionPane.showMessageDialog(frame, "Hello LNHS!");
x++;
}
}

Output:

6|Q3W5
Now do it!

Instructions: Answer the following


1. What is the difference among For Loop, While loop ,Do-While Loop

For Loop (5 points) While Loop (5 Points) Do-While Loop (5 Points)

2. Why is loop important in programming? 5 points


_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________

7|Q3W5
Ace it!
ASSESSMENT
Name: ______________________________________ Grade and Section: ________________
Date Answered: _____________________________________ Score: ____________________

I. Multiple Choice:
Instruction: Read and Analyse the questioncarefully. Encircle the letter of the correct answer.

1. How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
a. 8
b. 9
c. 10
d. 0
2. What is the output of the following code?
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
a. 1
b. 2
c. 3
d. 4
3. What is the value of x when the loop stops?
public static void main(String[] args) {
int x,y;
x = 5;

y = 1;
while (x > 0) {
x = x - 1;
y = y * x;
System.out.println(y);
}
}
a. 1
b. 0
c. -1
d. 5
4. How many times will the following code print "Welcome to Java"?
int count = 0;
do {
System.out.println("Welcome to Java");
count++;
} while (count < 10);

8|Q3W5
a. 0
b. 10
c. 9
d. 11
5. Which of the following loops prints "Welcome to Java" 10 times?
a. for (int count = 1; count <= 10; count++) {
System.out.println("Welcome to Java");
b. for (int count = 2; count < 10; count++) {
System.out.println("Welcome to Java");
}
c. for (int count = 1; count < 10; count++) {
System.out.println("Welcome to Java");
}
d. for (int count = 0; count <= 10; count++) {
System.out.println("Welcome to Java");
}
6. The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
i++;
}
a. 1 2 3 4 5 6 7 8 9
b. 1 2 3 4 5 6 7 8 9 10
c. 1 2 3 4 5
d. 1 3 5 7 9
7. What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
a. 1
b. 36
c. 45
d. 9
8. How many times is the println statement executed?
for (int i = 0; i < 10; i++)
System.out.println(i );
a. 100
b. 10
c. 45
d. 20
9. A for loop includes a _____, which increases or decreases through each step of the loop
a. Infinite
b. Incremental/Decremental
c. Counter
d. Initialization
10. What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {

9|Q3W5
number = number - 3;
System.out.print(number + " ");
}
a. 6,3,0
b. 6,3
c. 3,0
d. 0

II. Write the output of the following code snippe.


1. public static void main(String[] args) { Output:
int x,y;
x = 5;
y = 1;
while (x > 0) {
x = x - 1;
y = y * x;
System.out.println(y);
}
}

2. public static void main(String[] args) { Output:


int N;
N = 1;
while (N <= 32) {
N = 2 * N;
System.out.println(N);
}
}

3. Write a for loop code snippet that will print out all the multiples of 3 from 3 to 36, that
is: 3 6 9 12 15 18 21 24 27 30 33 36.

Code Snippet:

Congratulations! You made it! You just finished Module 5.

10 | Q 3 W 5

You might also like