Professional Documents
Culture Documents
ITE 186 Day 5
ITE 186 Day 5
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
In representing the following statements above, I have provided a flowchart to visualize the flow of the
concepts of for loop:
Output:
Java is fun
Java is fun
Java is fun
Java is fun
Java is fun
while (testExpression) {
// body of loop
}
The above code is explained on the following:
To represent as well the following statements above, I have provided a flowchart to visualize the flow of
the concepts of while loop:
// declare variables
int i = 1, n = 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
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)
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
}
In the above programs, the textExpression is always true. Hence, the loop body will run for infinite
times.
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.
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?
Answer Key
Skill Building Activities:
Exercise 1 Solution:
Exercise 2 Solution:
import java.util.Scanner;
int base;
int power;
int result = 1;
}
System.out.println("Result: "+ result);
}
}