0% found this document useful (0 votes)
5 views27 pages

Lesson5. Repetition

The document provides an overview of repetition structures in Java programming, including while loops, do-while loops, and for loops. It outlines learning outcomes, syntax, and examples for each type of loop, emphasizing the importance of looping in executing repetitious tasks. Additionally, it includes activities for students to apply their knowledge of repetition structures in programming assignments.

Uploaded by

secret21006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Lesson5. Repetition

The document provides an overview of repetition structures in Java programming, including while loops, do-while loops, and for loops. It outlines learning outcomes, syntax, and examples for each type of loop, emphasizing the importance of looping in executing repetitious tasks. Additionally, it includes activities for students to apply their knowledge of repetition structures in programming assignments.

Uploaded by

secret21006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

COMPUTER

Programming 2
(Java)
Dr. Marygin E. Sarmiento
Professor
Repetition Structure
Do While
While
For Loop
Learning outcomes
At the end of the lesson, the students
are expected to:
 Define Repetition Concept
 Enumerate the types of Repetition
 Determine the Syntax of each
Repetition
 Develop their own program using
Repetition
Introduction
Looping is the process of repeatedly
executing one or more steps of an
algorithm or program; it is essential in
programming, as most programs
perform repetitious tasks

To make the program more


powerful, a loop structure can be
included in a program. It is a structure
that allows a block of statements to be
executed repeatedly.
A Boolean expression will be
evaluated within the looping structure.
If the expression is true, the block of
statements called the loop body will
be executed and then Boolean
expression evaluated again. The loop
body will continue to execute as long
as the expression is true. It will end
when the Boolean expression is false.
Java supports three kinds of iteration
statements: while loop, do..while loop,
and for loop.
WHILE LOOP

A while loop can be used to repeat a statement or


block while it’s controlling Boolean expression is true. If the
conditional expression controlling the while loop is initially
false, then the loop body will not be executed at all.

Syntax:
While (condition)
Statement;// executed when the
condition is true

There are three separate actions, which must be


included in a whileloop in order to prevent the loop from
executing infinitely:
The loop control variable has to be initialized.
The loop control variable is tested in the while statement.
The body of the while statement must take some action
that alters the value of the loop control variable.
The following is a segment of a program showing
the use of the while statement.
Output
Example:
THE DO…WHILE LOOP

In programming, there may be situations where we


might need to execute the loop body at least once. Thus,
we will have to write a look to check the conditional
expression after the first iteration. The do..while loop will
allow the program to execute the statements in the loop
body before testing the condition.

It is similar to the while loop, where it allows an


iteration process to take place. However, there is a
difference between the two loop statements. The while
loop tests the conditional expression before executing the
statements in the loop body.

Hence, if the conditional expression is false, the


statements in the loop body will not be executed at all.
However, the do..while loop ensures the statements in the
loop body is executed at least once before checking the
conditional expression.
Syntax:
do
{
Statements;//executed when condition is
true
}
while (condition);

Example:
Example 2

import java.io.*;
public class DoWhileLoop
{
public static void main (String [] args) throws Exception
{
char alpha;
do
{
System.out.print(“Enter a character:”);
alpha = Character.toUpperCase((char)
System.in.read());
System.in.read(); System.in.read();
{
while (alpha != ‘X’);

System.out.println(“Exit program”);
}
}
Example 3
packageswitchcase_DoLoop;

import java.io.*;
public class do_switch_doLoop
{public static void main (String args[]) throws Exception
{BufferedReader input = new BufferedReader( new
InputStreamReader(System.in));
int s, r, square_area;
double pi, Circle_area;
int width, length, Rec_Area;
String ans;
do
{//begin of do
System.out.println("[C]Circle [R] Rectangle [S] Square [Q] Quit");
System.out.print("Please Enter Your Choice: ");
ans = input.readLine();
switch (ans)
{//begin of switch
case "C":{
r =100;
pi = 3.1416;
System.out.println("The Value for Radius: " + r);
System.out.println("The Value for pi: " + pi);
Circle_area = 2*pi*(r*r);
System.out.println("The area of a circle is: "+ Circle_area); }
System.out.println();
break;
case "R": {
width = 300;
length = 5;
System.out.println("The value for Width "+width);
System.out.println("The value for Length "+length);
Rec_Area = length * width;
System.out.println("The area of a rectangle is "+ Rec_Area); }
break;
case "S":{
s = 10;
System.out.println("The value for value for side is " +s);
square_area = s*s;
System.out.println("The area of a square is "+ square_area); }
break;
case "Q": System.out.println("Good Bye");
default: System.out.println("Try Again");
}//end of switch
System.out.print("Back to main menu Y/N? " );
ans = input.readLine();
}//end of do
while (!ans.equals("N"));
}//end of main
}//end of class
Output
THE FOR LOOP

The for loop is another iteration construct


that allows the statements in the loop body to
be executed a specific number of times. The
for loop includes the following three sections
separated by a semicolon:

Initialization – to initialize the loop


control variable.

Condition – a Boolean variable that tests


the loop control variable against target value.
The statements in the loop body will be
executed if the expression is true. It will
terminate if the expression is false.
Counter progression – an expression that increment or
decrement the loop control variable.

Syntax:

for (initialization; condition; counter progression)


{
Statements;
}
Example:
ASSIGNMENT
This program will describe the use of repetition structure. The user will
input name and it will display on the screen 10 times.

import java.io.*;
public class nametentimes
{
public static void main (String [] args) throws Exception
{
BufferedReader input = new BufferedReader(InputStreamReader
(System.in());

String name;
do
{
System.out.print(“Enter Your Name:”);
name = input.readLine();
}
while (alpha <11);

System.out.println(“\n” + name);
}
}
ACTIVITIES

Activity # 1.

Modify the program in your assignment and


convert it to the remaining types of repetition
structures such as whole loop and for loop
statement. It displays the same output but it is
written in different ways, applying the syntax for
every repetition statement.

Activity # 2

Create a program that will display the


multiplication table. Use any repetition structure.
Display 10 numbers in horizontal and vertical format as
shown below.
ACTIVITY # 3
Create a program that will compute the
total bill of the customer. The program will
input item purchased, quantity, price and
cash given by the customer. It will display
automatically the total bill and the change of
the customer If the cash is greater than the
total bill. If the customer reaches 1000 pesos,
he will avail 30% discount of his total bill. Use
if else statement and repetition structure to
display the “Do you want to try again Y/N?”.
ACTIVITY # 4:
Create a program that will compute the
prelim, midterm, final grades of the students.
The program will input grades for quiz,
recitation, project, and exam rating. The
class standing, class average and the
prelim, midterm and final grades will be
computed and displayed automatically.
Display also the grade equivalent and if the
grade of the student is greater than 75 then
the remark will be “passed” otherwise
“failed”. Use any selection and repetition
structures. Use these formulas:

You might also like