You are on page 1of 5

ST.

JOHN PAUL II COLLEGE OF DAVAO


COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

MidTerm Laboratory No. 1: Loop Structures

Objectives
1. To understand the concepts and theory of 3 different loop
structures.
2. To build programs using 3 different loop structures
3. To know how to trace the program using different loops

Background

In programming languages, loops are used to execute a set of


instructions/functions repeatedly when some conditions become true.
There are three types of loops in Java.

1. for loop
2. while loop
3. do-while loop

The Java for loop is used to iterate a part of the program several times.
If the number of iteration is fixed, it is recommended to use for loop.

while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed, it is recommended to use while loop.

The Java do-while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-while loop.

The Java do-while loop is executed at least once because condition is


checked after loop body.

General Instructions:

1. Answer the activities in the succeeding pages.


2. Submit it in a .PDF format with a filename convention
<SURNAME>_<ACT#> such that BASTE_ACT5.
3. The deadline of submission will be 5 days after it was discussed.

SCP-CC103 | 1
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Learning Resources and Tools

1. Laboratory Manual or SCP


2. Ballpen
3. PC/Laptop
4. Internet
5. IDE (Eclipse, Netbeans, ect)

NOTE: Put your complete name in all of your


program as a comment and in your outputs. You may
use JOptionPane.

Activity 1.1. Write a program that prints all even numbers from 1 to 50 inclusive
using the for loop, while loop, and do…while loop. The class name is EvenNum.
(20PTS)

Expected output:

Your Source Code:

Your Output:

SCP-CC103 | 2
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Activity 1.2. Write a program that sums the integers from 1 to 50 using any
looping statement. The class name is Sum50. (20PTS)

Expected output:

Your Source Code:

Your Output:

Activity 1.3. Write a program that prints every integer value from 1 to 20 along
with its squared value using the following looping structures (for, while, and do-while
loop). The class name is TableOfSquares. (30PTS)

Expected output:

SCP-CC103 | 3
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Your Source Code:

Your Output:

Activity 1.4. Write a fragment of code that will compute the sum of the first n
positive odd integers. For example, if n is 5, you should compute 1 + 3 + 5 + 7 +
9. Thus, the result is 25. Class name : SumOfNOdd. (30PTS)

Expected Output:

Your Source Code:

Your Output:

SCP-CC103 | 4
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Activity 1.5. Write a for statement to compute the sum of an N as an


entered integer 1 + 2² + 3² + 4² + 5² + ... + N². (50PTS)

Expected Output:

Your Source Code:

Your Output:

SCP-CC103 | 5

You might also like