You are on page 1of 10

ITE 186: Computer Programming 2

Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

Lesson Title: Iterative Control Structures Materials:


Lesson Targets: SAS
At the end of this module, students will be able to: References:
1. Apply algorithms and flowchart to write loop programs. ● Farrell,Joyce.Java
2. Demonstrate on how to write basic loop statements Programming: Concepts and
using for, while, do..while from simple, ladderized and Applications. Second Edition.
nested in python and translate it in Java. Cengage Learning Asia. 2012.
3. Compare for, while and do..while loops on their flow ● https://github.com/java-
control. tutorial-for-beginners
4. Explain the basic module of python and compare it to
the Java packages.

A. LESSON PREVIEW/REVIEW
Introduction
Good day, everyone. In this module you will learn how to use loops in Java with the help of
examples and also through learning by doing. I will introduce to you another structure which
controls a program with the help of iterative structures. Once you learn this concept, I encourage
you to solve some exercises provided in the handouts to practice the importance of using them in
application development.

B. MAIN LESSON
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.
In Java, there are three types of loops.
● for loop
● while loop
● do...while loop

Java for Loop


Java for loop is used to run a block of code for a certain number of times. The syntax of for loop is:

for (initialExpression; testExpression; updateExpression) {


// body of the loop
}

The above code is explained on the following:


The initialExpression initializes and/or declares variables and executes only once.
1. The condition is evaluated. If the condition is true, the body of the for loop is executed.
2. The updateExpression updates the value of initialExpression.
3. The condition is evaluated again. The process continues until the condition is false.

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

In representing the following statements above, I have provided a flowchart to visualize the flow of the
concepts of for loop:

Example 1: Display a Text Five Times


class Main {
public static void main(String[] args) {
int n = 5;
// for loop
for (int i = 1; i <= n; ++i) {
System.out.println("Java is fun");
}
}
}

Output:
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun

Here is how the program above works

Iteration Variable Condition: i < = n Action


1st i=1 true Java is fun is printed.
n=5 i is increased to 2.
2nd i=2 true Java is fun is printed.
n=5 i is increased to 3
3rd i=3 true Java is fun is printed.
n=5 i is increased to 4.

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

4TH i=4 True Java is fun is printed.


n=5 i is increased to 5.
5th i=5 True Java is fun is printed.
n=5 i is increased to 6.
6th i=6 False The loop is terminated.
n=5

Java while Loop


Java while loop is used to run a specific code until a certain condition is met. The syntax of the while
loop is:

while (testExpression) {
// body of loop
}
The above code is explained on the following:

1. A while loop evaluates the textExpression inside the parenthesis ().


2. If the textExpression evaluates to true, the code inside the while loop is executed.
3. The textExpression is evaluated again.
4. This process continues until the textExpression is false.
5. When the textExpression evaluates to false, the loop stops.

To represent as well the following statements above, I have provided a flowchart to visualize the flow of
the concepts of while loop:

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

Example 1: Display Numbers from 1 to 5

// Program to display numbers from 1 to 5


class Main {
public static void main(String[] args) {

// declare variables
int i = 1, n = 5;

// while loop from 1 to 5


while(i <= n) {
System.out.println(i);
i++;
}
}
}
Output:
1
2
3
4
5

Iteration Variable Condition: i < = n Action


1st i=1 true 1 is printed.
n=5 i is increased to 2.
2nd i=2 true 2 is printed.
n=5 i is increased to 3.
3rd i=3 true 3 is printed.
n=5 i is increased to 4..
4TH i=4 True 4 is printed.
n=5 i is increased to 5..
5th i=5 True 5 is printed.
n=5 i is increased to 6
6th i=6 False The loop is
n=5 terminated.

Java do ..while Loop


The do...while loop is similar to while loop. However, the body of do...while loop is executed once before
the test expression is checked. For example,

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

The above code is explained on the following:


1. The body of the loop is executed at first. Then the textExpression is evaluated.
2. If the textExpression evaluates to
true, the body of the loop inside the
do statement is executed again.
3. The textExpression is evaluated
once again.
4. If the textExpression evaluates to
true, the body of the loop inside the
do statement is executed again.
5. This process continues until the
textExpression evaluates to false.
Then the loop stops.
To represent as well the following statements
above, I have provided a flowchart to visualize
the flow of the concepts of do while loop:

Example 1: Display Numbers from 1 to 5

// Java Program to display numbers from 1 to 5

import java.util.Scanner;
// Program to find the sum of natural numbers from 1 to 100.

class Main {
public static void main(String[] args) {
int i = 1, n = 5;
// do...while loop from 1 to 5
do {
System.out.println(i);
i++;
} while(i <= n);
}
}
Output:
1
2
3

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

4
5
Iteration Variable Condition: i < = n Action
i=1 Not checked 1 is printed.
n=5 i is increased to 2.
1st i=2 true 2 is printed.
n=5 i is increased to 3.
2nd i=3 true 3 is printed.
n=5 i is increased to 4..
3rd i=4 True 4 is printed.
n=5 i is increased to 5..
4th i=5 True 5 is printed.
n=5 i is increased to 6
5th i=6 False The loop is
n=5 terminated.

Skill-building Activities
Let us practice! After completing each exercise, you may refer to the Key to Corrections for feedback.
Try to complete each exercise before looking at the feedback.

Exercise 1:
1. Write a program to calculate the sum of first 10 natural number.

Exercise 2:
1. Write a program in which two numbers are entered through the keyboard.
2. Create a solution where it finds the value of one number raised to the power of another. (Do not
use Java built-in method)

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

Check for Understanding


What is the output of the following expressions?

Ans:

C. LESSON WRAP-UP
FAQs
If the condition of a loop is always true, the loop runs for infinite times (until the memory is full).
For example,
// infinite while loop
while(true){
// body of loop
}

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

Here is an example of an infinite do...while loop.


// infinite do...while loop
int count = 1;
do {
// body of loop
} while(count == 1)

In the above programs, the textExpression is always true. Hence, the loop body will run for infinite
times.

Thinking about Learning


a) Mark your place in the work tracker which is simply a visual to help you track how much work you
have accomplished and how much work there is left to do.
Period 1 Period 2 Period 3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

b) Think about your learning by filling up “My Learning Tracker”. Write the learning targets, score, and
learning experience for the session and deliberately plan for the next session.

Date Learning Target/Topic Scores Action Panel

What’s What module # did you do? What were What contributed to the quality of
the date What were the learning targets? your scores in your performance today? What will
today? What activities did you do? the activities? you do next session to maintain your
performance or improve it?

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

Answer Key
Skill Building Activities:
Exercise 1 Solution:

public class SumNumbers


{
public static void main(String[] args)
{
int sum = 0;
for(int i=1; i<=10; i++)
{
sum += i;
}
System.out.println("Sum: " + sum);
}
}

Exercise 2 Solution:

import java.util.Scanner;

public class PowerDemo


{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);

int base;
int power;
int result = 1;

System.out.print("Enter the base number ");


base = console.nextInt();

System.out.print("Enter the power ");


power = console.nextInt();

for(int i = 1; i <= power; i++)


{
result *= base;

This document is the property of PHINMA EDUCATION


ITE 186: Computer Programming 2
Module #5 Student Activity Sheet

Name: Class number:


Section: Schedule: Date:

}
System.out.println("Result: "+ result);
}
}

This document is the property of PHINMA EDUCATION

You might also like