Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

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:

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

Physically Detached Yet Academically Attached


Your Source Code:
public class EvenNum
{
public static void main(String[] args) {
// NIÑA MAE TERIOTE
System.out.println("Created By: NIÑA MAE TERIOTE");
System.out.println("Even numbers From 1 to 50");
System.out.println("--Using for loop--");
int n = 50;
for (int i = 1; i<=50; i++){
if(i % 2 == 0){
System.out.print(i + " ");
}
}
System.out.println("");
System.out.println("--Using while loop--");

int i =2;
while(i<=n){
if(i % 2 == 0){
System.out.print(i +" ");
i = i + 2;
}
}
System.out.println("");
System.out.println("--Using do-while loop--");
i = 2;
do{
if(i % 2 ==0){
System.out.print(i + " ");
i = i + 2;

}
}
while(i<=50);
}
}

Your Output:

SCP-CC103 | 3
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:


public class Sum50
{
public static void main(String[] args) {
// NIÑA MAE TERIOTE
System.out.println("Created By: NIÑA MAE TERIOTE");
int n = 50;
int sum = 0;

for(int i = 1; i <= n; i++)


{
sum = sum + i; // LINE A

}
System.out.println("The sum of numbers from 1 to 50 is " + sum);
}

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 | 4
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIONS TECHNOLOGY

Physically Detached Yet Academically Attached

Your Source Code:


public class TableOfSquares
{
public static void main(String[] args) {
// NIÑA MAE TERIOTE
System.out.println("Created By: NIÑA MAE TERIOTE");
System.out.println("--Using for loop--");
for(int j=1 ;j<=20;j++){
System.out.println("Number:"+(j)+"\tSquare:"+(j*j));
}
System.out.println("--Using while loop--");
int i = 1;
while (i<=20){
System.out.println("Number:"+(i)+"\tSquare:"+(i*i));
i++;
}
System.out.println("--Using do-while loop--");
int k = 1;
do {
System.out.println("Number:"+(k)+"\tSquare:"+(k*k));
k++;
}while(k<=20);
}
}
Your Output:

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

Physically Detached Yet Academically Attached


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:


import java.util.Scanner;
public class SumOfNOdd
{
public static void main(String[] args) {
// NIÑA MAE TERIOTE
System.out.println("Created By: NIÑA MAE TERIOTE");
System.out.print("Value of N: ");
Scanner scan = new Scanner (System.in);
int n = scan.nextInt();

int number = 0,i ,


sum = 0;
for( i = 1; i <= n; i++)
{

sum+=2*i-1;
}

System.out.print("The sum if first" + n + "odd integers is" + sum );

}
}

Your Output:

SCP-CC103 | 6
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:


import java.util.Scanner;
public class SumOfNOdd
{
public static void main(String[] args) {
// NIÑA MAE TERIOTE
System.out.println("Created By: NIÑA MAE TERIOTE");
System.out.print("Value of N :");
Scanner scan = new Scanner (System.in);
double n = scan.nextInt();

int i;
int sum = 0;
for( i = 1; i <= n; i++)
{

sum+=(i* i);
}

System.out.print("The sum an entered integer is " + sum );

}
}

Your Output:

SCP-CC103 | 7

You might also like