0% found this document useful (0 votes)
2 views

ICT4-LG24

This learning guide for Grade 10 ICT focuses on the repetition control structures in Java, specifically while, do...while, and for loops. It provides syntax, implementation examples, and emphasizes the importance of loop conditions to avoid infinite loops. Additionally, it includes an activity for students to write a Java program that calculates the sum of a series based on user input.

Uploaded by

hamoudiguinal2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ICT4-LG24

This learning guide for Grade 10 ICT focuses on the repetition control structures in Java, specifically while, do...while, and for loops. It provides syntax, implementation examples, and emphasizes the importance of loop conditions to avoid infinite loops. Additionally, it includes an activity for students to write a Java program that calculates the sum of a series based on user input.

Uploaded by

hamoudiguinal2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Learning Guide

Grade 10 - ICT 4

Lesson 24 : Repetition Control Structure

Pre-requisite Concept : Understanding the Building Blocks of Java

Time : 3 hours/week

Introduction

This learning guide will help us understand on the loop structure. The discussion
will include the syntax and the basic implementation in Java programming language.

Objectives

1. Understand the structures of while, do…while, and for statements.


2. Use while, do…while or for statements in a simple Java program.

Lesson Proper

while Looping (Repetition) Structure

Sometimes it is necessary to repeat a set of statements several times. One way


to do this is to type the set of statements in the program over and over. For example,
if you want to repeat a set of statements 100 times, you type the set of statements 100
times in the program. However, this way of repeating a set of statements is
impractical, if not possible. Fortunately, there is a simpler approach. As noted earlier,
Java has three repetition, or looping, structures that allow you to repeat a set of
statements until certain conditions are met. This section discusses the first looping
structure, a while loop.

The general form of the while statement is:

while (logical expression)


statement
In Java, while is a reserved word. The logical expression is called a loop condition or
simply condition. The statement is called the body of the loop. Moreover, the
statement can be either a simple or compound statement. also, note that the
parentheses around the logical expression are part of the syntax.

The logical expression provides an entry condition. If it initially evaluates to true, the
statement executes. The loop condition – the logical expression – is then reevaluated.
If it again evaluates to true, the statement executes again. The statement (body of the
loop) continues to execute endlessly is called an infinite loop. To avoid an infinite loop,
make sure that the loop’s body contains one or more statements that ensure that the
loop condition – the logical expression in the while statement – will eventually be false.

Example

Consider the following Java program segment:

int i = 0; //Line1
while (i <= 20) //Line2
{
System.out.print(i + “ “); //Line3
i = i + 5; //Line4
}
System.out.println(); //Line5

Sample Run:

0 5 10 15 20

In Line1, the variable i is set to 0. The logical expression in the while statement (in
Line2), i <= 20, is then evaluated. Because the expression i <= 20 evaluates to true,
the body of the while loop executes next. The body of the while loop consists of the
statements in Lines 3 and 4. The statement in Line 3 outputs the value of i, which is 0;
the statement in Line4 changes the value of i to 5. After executing the statements in
Lines 3 and 4, the logical expression in the while loop (Line2) is evaluated again.
Because i is 5, the expression i <= 20 evaluates to true and the body of the while loop
executes again. This process of evaluating the logical expression and executing the
body of the while loop continues until the expression i <= 20 (Line2) no longer
evaluates to true.

do…while Looping (Repetition) Structure

The general form of the do…while statement is:

do
statement
while (logical expression);
In Java, do is reserved word. As with the other repetition structures, the do…while
statement can be either a simple or compound statement. If it is a compound
statement, enclose it between braces. The logical expression is called the loop
condition.

The statement executes first, and then the logical expression is evaluated. If the logical
expression evaluates to true, the statement executes again. As long as the logical
expression in a do…while statement is true, the statement executes. To avoid an
infinite loop, you must, as before, make sure that the body of the loop contains a
statement that ultimately makes the logical expression evaluate to false and assures
that it exits properly.

Example

Consider the following Java program segment:

int i = 0;
do
{
System.out.print(i + “ “);
i = i + 5;
}
while (i <= 20);

Sample Run:

0 5 10 15 20

After the value 20 is outputted, the statement:

i = i + 5;

changes the value of i to 25, so i <= 20 becomes false, which halts the loop.

for Looping (Repetition) Structure

The general form of the for statement is:

for (initial expression; logical expression; update expression)


statement

In Java, for is a reserved word. The logical expression is called the loop condition. The
initial expression, logical expression, and update expression (called for loop control
expressions) are enclosed within parentheses and control the body (statement) of the
for statement. Note that the for loop control expressions are separated by semicolons,
and that the body of a for loop can have either a simple or compound statement.
The for loop executes as follows:

1. The initial expression executes.


2. The logical expression is evaluated. If the loop condition evaluates to true:
a. Execute the body of the for loop.
b. Execute the update statement (the third expression in the parentheses).
3. Repeat Step 2 until the loop condition evaluates to false.

The initial statement usually initializes a variable (called the for loop control, or indexed,
variable).

Example

The following for loop prints the first 10 non-negative integers: (Assume that i is an int
variable.)

for (i = 0; i < 10; i++)


System.out.print(i + “ “);
System.out.println();

The initial expression, i = 0; initializes i to 0. Next, the logical expression, i < 10, is
evaluated. Because 0 < 10 is true, the print statement executes and outputs 0. The
update expression, i++, then executes, which sets the value of i to 1. Once again, the
logical expression evaluates to false, the for loop terminates, and the statement
following the for loop executes.

Activity

Answer the following problems. Write your answers in a one whole sheet of paper.
Don’t forget to write your name, grade level, section and activity number.

Write a Java program to produce the sum of the series

where n is entered by the user. The n should be a positive integer. Use the
Scanner class to get the input and System.out.println/System.out.print to display
the outputs.
References

1. Computer Advantage: Understanding Structured Programming (Second


Edition), 2009 By Teresita P. Pasadas.et. al.
(Textbook)
2. Java Programming: From Problem Analysis to Program Design (5 th Edition),
2012 By D. S. Malik
3. Developing Useful Computer Applications for Today’s World Fundamentals of
Web Application Development II (2008)
By Dennis Cesar R. Patiño
4. Introduction to Computer by Antonio M. Andres, Sr. (2003)

You might also like