ComputerProgramming.grade11.Q3.w5 for STUDENT
ComputerProgramming.grade11.Q3.w5 for STUDENT
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.
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
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?
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
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
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:
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
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!
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
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:
10 | Q 3 W 5