ICT4-LG24
ICT4-LG24
Grade 10 - ICT 4
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
Lesson Proper
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
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
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
int i = 0;
do
{
System.out.print(i + “ “);
i = i + 5;
}
while (i <= 20);
Sample Run:
0 5 10 15 20
i = i + 5;
changes the value of i to 25, so i <= 20 becomes false, which halts the loop.
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:
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.)
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.
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