You are on page 1of 13

College of Computing Education

3rd Floor, DPT Building


Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

UNIT LEARNING OUTCOME (ULO)

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

ULO 2 (WEEK 4-6)

Use Control structures to efficiently implement branch and looping algorithm in programs.

METALANGUAGE

In this ULO, you will encounter the following terms frequently:

Control Structure are methods of processing a program. They are either in sequence,
branching or looping.

Relational Operators allow you to make comparisons in a program. The condition is


represented by a logical expression in Java.

Logical Operators are also Boolean operators.

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.

Flag-controlled while loops uses boolean variable to control the loop.

EOF-controlled while loops are used to read CONSOLE or FILE INPUTs .

Break statement is used to exit early from a loop.

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

WEEK 5: CONTROL STRUCTURE II: REPETITION

ESSENTIAL KNOWLEDGE

There will be FIVE sections in this week’s learning:

1. Repetition or Looping
2. For Loop
3. Do…While Loop
4. Break and Continue Statements
5. Nested Control Structure

1. Repetition or Looping

• Why is repetition needed?


There are many situations in which the same statements need to be executed
several times

• 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

• Typically, while loops are written in the following form:


• There are four types of control in WHILE loop
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

1.1 Counter-Controlled While Loop

• Used when exact number of data or entry pieces 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

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!

• Fibonacci formula for any Fibonacci sequence:


xn = xn-1 + xn-2
where:

xn is term number "n"


xn−1 is the previous term (n−1)
xn−2 is the term before that (n−2)

Example: term 9 is calculated like this:


x9 = x9−1 + x9−2
= x8 + x7
= 21 + 13

Fibonacci Sequence in Java


• Input: first two Fibonacci numbers in sequence, position in sequence of desired
Fibonacci number (n)
• int previous1 = Fibonacci number 1
• int previous2 = Fibonacci number 2
• int nth Fibonacci = position of nth Fibonacci number
• Output: nth Fibonacci number

if (nthFibonacci == 1)
current = previous1;
else if (nthFibonacci == 2)
current = previous2;
else
{
counter = 3;

while (counter <= nthFibonacci)


{
current = previous2 + previous1;
previous1 = previous2;
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

previous2 = current;
counter++;
}
}

• Final result found in last value of current

1.2 Sentinel-Controlled While Loop

• 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

1.3 Flag-Controlled While Loop

• Boolean value used to control loop


• General form:

1.4 EOF-Controlled While Loop

• The expression console.hasNext() evaluates to true if there is an


input in the input stream; otherwise it returns false
• Expressions such as console.nextInt() act as the loop condition
• A general form of the EOF-controlled while loop that uses the
Scanner object console to input data is of the form:
• Suppose that inFile is a Scanner object initialized to the input file; in
this case, the EOF-controlled while loop takes the following 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

• Specialized form of while loop


• Simplifies the writing of count-controlled loops

• 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

• Does not execute if initial condition is false


• Update expression changes value of loop control variable, eventually making it
false
• If logical expression is always true, result is an infinite loop
• Infinite loop can be specified by omitting all three control statements
• If logical expression is omitted, it is assumed to be true
• for statement ending in semicolon is empty

• Example

Input: N integers (positive, negative, and zeros)


int N = 20; //N easily modified
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Output: number of 0s, number of even integers, number of odd integers

• The switch statement can also be written as an if...else statement as follows:

3. Do…While Loop

• Syntax

• Statements executed first, and then logical expression evaluated


• Statement(s) executed at least once and then continued if logical expression is
true
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

• A do...while loop can be used for input validation.


• Suppose that a program prompts a user to enter a test score, which must be
greater than or equal to 0 and less than or equal to 50.
• If the user enters a score less than 0 or greater than 50, the user should be
prompted to re-enter the score.
• The following do...while loop can be used to accomplish this objective:

• All three loops have a place in Java


• If you know or the program can determine in advance the number of repetitions
needed, the for loop is the correct choice
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

• 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

4. Break and Continue Statements

• 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

5. Nested Control Structure


• Provides new power, subtlety, and complexity
• if, if…else, and switch structures can be placed within while loops

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++;
}

• for loops can be found within other for loops

SELF-HELP

You can check out https://www.geeksforgeeks.org/java-while-loop-with-examples/

You might also like