Professional Documents
Culture Documents
Week ULO
1
Discuss the program structure and the basic elements of Java
2
programming and apply them appropriately in a program.
3
4 Use Control structures to efficiently implement branch and
5 looping algorithm in programs
6
7 Explore user-defined methods and ways of manipulating
8 records and to correctly apply all concepts in a self-developed
9 application
Use Control structures to efficiently implement branch and looping algorithm in programs.
METALANGUAGE
Control Structure are methods of processing a program. They are either in sequence,
branching or looping.
Compound Statements refers to a block of more than one statement that needs to be
executed for a certain condition. They must be within braces {}.
Counter-controlled while loops are used when the user knows exactly the number of times
the loop will be executed.
Sentinel-controlled while loops are used when the user might not know the exact number of
times the loop will be executed but the last entry with a special value is known.
Continue statement is used to skip through the remaining statement and proceed with the
next iteration of the loop.
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
ESSENTIAL KNOWLEDGE
1. Repetition or Looping
2. For Loop
3. Do…While Loop
4. Break and Continue Statements
5. Nested Control Structure
1. Repetition or Looping
• Example
Formulas used to find average grades for students in a class
• Syntax
while (logical expression)
statement
• Expression is always true in an infinite loop
• Statements must change the value of expression to false
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
• Example
Fibonacci Sequence
The Fibonacci Sequence is the series of numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it:
the 2 is calculated by adding the two numbers before it (1+1),
the 3 is calculated by adding the two numbers before it (1+2),
the 5 is (2+3),
and so on!
if (nthFibonacci == 1)
current = previous1;
else if (nthFibonacci == 2)
current = previous2;
else
{
counter = 3;
previous2 = current;
counter++;
}
}
• Used when exact number of entry pieces is unknown but last entry
(special/sentinel value) is known
• General form:
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
2. For Loop
• Syntax
for (initial expression; logical expression; update expression)
statement
• Execution
o Initial statement executes
o The loop condition is evaluated
o If loop condition evaluates to true, execute for loop statement and execute
update statement
o Repeat until loop condition is false
• Example
The following for loop prints the first 10 nonnegative integers:
for (i = 0; i < 10; i++)
System.out.print(i + " ");
System.out.println();
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
• Example
3. Do…While Loop
• Syntax
• If you do not know and the program cannot determine in advance the number of
repetitions needed, and it could be zero, the while loop is the right choice
• If you do not know and the program cannot determine in advance the number of
repetitions needed, and it is at least one, the do...while loop is the right choice
• Break statement
o Used to exit early from a loop
o Used to skip remainder of switch structure
o Can be placed within if statement of a loop
o If condition is met, loop exited immediately
• Continue statement
o Used in while, for, and do...while structures
o When executed in a loop, the remaining statements in the loop are
skipped; proceeds with the next iteration of the loop
o When executed in a while/do…while structure, expression evaluated
immediately after continue statement
o In a for structure, the update expression is executed after the continue
statement; then the loop condition executes
while (numofstud<totalstud)
{
if (age_of_stud < 20)
grp_of_stud=’A’;
else if (age_of_stud < 25)
grp_of_stud=’B’;
else
grp_of_stud=’C’;
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
numofstud++;
}
SELF-HELP