Control Structure
Repetition
Outlines
 Repetition Statements
 while statement
 do..while statement
 for statement
 Nested loops
 Repetition Control Structures
Repetition Statements
 Repetition statement (or loop) a block of code to be
executed for a fixed number of times or until a certain
condition is met.
 In JAVA, repetition can be done by using the
following repetition statements:
a) while
b) do…while
c) for
The while statement
 The while statement evaluates
expression/condition, which must return a boolean
value. If the expression/condition is true, the
statement(s) in the while block is/are executed.
 Syntax:
while (expression/condition)
Statement(s);
 It continues testing the expression/condition and
executing its block until the expression/condition
evaluates to false
The while statement
Output ? 1 2 3 4
Example 1
int i=1;
while (i<5){
System.out.print(i + “”);
i++;
}
Output ? SUM : 1
SUM : 7
Good Bye
Example 2
int sum=0, number =1;
while (number <= 10)
{
sum+=number;
number = number + 5;
System.out.println(“SUM :” + sum);
}
System.out.println(“Good Bye”);
The do…while statement
 It is similar to while loops except it first executes the
statement(s) inside the loop, and then evaluates the
expression/condition. If the expression is true, the
statement(s) in the do loop is/are executed again.
 Syntax
do
statement(s);
while (expression);
 It continues executing the statement until the
expression/condition becomes false.
 Note that the statement within the do loop is always
executed at least once.
The do…while statement
Output ? 0 1 2 3
Example 3
int i=0;
do{
System.out.print(i +
“”);
i++;
}while(i<=3);
Output ? SUM : 2
SUM : 9
Example 4
int sum=0, number =2;
do{
sum+=number;
number = number + 5;
System.out.println(“SUM :” + sum);
} while (number <= 10);
The for statement
 Usually used when a loop will be executed a set
number of times.
 Syntax:
for(initial statement; loop condition; update statement)
statement(s);
 The for loop executes as follow:
1) The initial statement is executed.
2) The loop expression/condition is evaluated. If it is
TRUE, the loop statement is executed followed by the
execution of the update statement
3) Repeat step 2) until the loop condition evaluates to
FALSE.
The for statement
Output ? 1 2 3 4
Example 5
for (i=1; i<5; i++)
System.out.print(i);
Output ? YAHOO ***YAHOO ***
Example 6
for (i=1; i<3; i++){
System.out.print(“YAHOO” + “”);
System.out.print(“***”);
}
Output ? YAHOO YAHOO YAHOO YAHOO YAHOO ***
Example 7
for (i=1; i<=5; i++)
System.out.print(“YAHOO”);
System.out.print(“***”);
Nested Loops
 The placing of one loop inside the body of
another loop is called nesting.
 When you "nest" two loops, the outer loop
takes control of the number of complete
repetitions of the inner loop.
 While all types of loops may be nested, the
most commonly nested loops are for loops.
Nested for Loops
 When working with nested loops, the outer loop
changes only after the inner loop is completely
finished (or is interrupted.).
Example 8
int num1, num2;
for(num2 = 0; num2 <= 2; num2++)
{
for(num1 = 0; num1 <= 1; num1++)
{
System.out.println(num2 + " " + num1);
}
}
Output ?
0 0
0 1
1 0
1 1
2 0
2 1
Infinite Loop
 By using any repetition statements, make sure that
the loop will eventually terminate.
 An infinite loop occurs when a condition always
evaluates to true and continues to execute endlessly.
int product =0;
for (product = 0;product < 10;)
{ product = product * 2;
System.out.println(product);
}
Repetition Control Structures
Repetition can be controlled by:
 Counter controlled loop
 Sentinel controlled loop
 Flag controlled loop
Exercise
Counter Controlled Loop
 Know exactly how many times a set of statements
needs to be executed.
Output ? 1 3 5 7 9
int num =1;
while (num < 10)
{
System.out.print (num);
num = num +2;
}
Example 10:
back
Sentinel Controlled Loop
 You might not know exactly how many times a set of
statements needs to be executed.
 It uses a "special" or sentinel value to indicate that the
loop is to end.
 This must be a value that doesn't occur normally in
the data.
Example 11 (complete program)
import java.util.Scanner;
class sentinelLoop {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an Integer, or -1 to stop: ");
int choice= input.nextInt();
while (choice!=-1)
{
System.out.println("INSIDE LOOPING");
System.out.print("Enter an Integer, or -1 to stop: ");
choice= input.nextInt();
}
System.out.println("Sentinel value detected. Good Bye.");
}
}
Example 11 ….
Enter an Integer, or -1 to stop: 2
INSIDE LOOPING
Enter an Integer, or -1 to stop: 5
INSIDE LOOPING
Enter an Integer, or -1 to stop: 0
INSIDE LOOPING
Enter an Integer, or -1 to stop: -1
Sentinel value detected. Good Bye.
OUTPUT?
back
Flag Controlled Loop
 Use a boolean variable to control the loop
boolean found = false;
while (!found){
:
:
if(expression)
found = true;
}
back
Exercise 1:
What is the output of the following program?
public class LoopExercise1
{
public static void main (String args[])
{
int choice=1, total=0;
while (choice <4){
total = choice++;
System.out.print(total); }
}
}
Exercise 2:
What is the output of the following program?
public class LoopExercise2
{
public static void main (String args[]){
for (int number =2; number <20; number++)
{
number = number *2;
if (number <15)
System.out.println(number);}
}
}
Exercise 3:
How many times is the following loop body repeated?
public class LoopExercise3 {
public static void main (String args[])
{
int i=1;
do {
if ((i % 2)== 0)
System.out.print(i);
i++;
} while(i<5);
}
}

More Related Content

PDF
Java conditional statements
PPT
Operators in C++
PPT
Control structures repetition
PPT
Looping statements in Java
PPTX
Decision making and branching in c programming
PPTX
Types of Statements in Python Programming Language
PPT
Control statements
PDF
Python decision making
Java conditional statements
Operators in C++
Control structures repetition
Looping statements in Java
Decision making and branching in c programming
Types of Statements in Python Programming Language
Control statements
Python decision making

What's hot (20)

PPT
RECURSION IN C
 
PPTX
Variables, Data Types, Operator & Expression in c in detail
PPTX
Conditional statement c++
PPTX
Operators and expressions in c language
PPT
Variables in C Programming
DOC
Jumping statements
PPTX
Lesson 5 php operators
PPTX
Pointers in c++
PDF
Control statements
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
The string class
PPTX
Conditional and control statement
PPTX
Function overloading and overriding
PPT
C++ Function
 
PPTX
Loops in Python
PPTX
Exception handling c++
PPTX
Type casting
PPTX
Introduction Of C++
RECURSION IN C
 
Variables, Data Types, Operator & Expression in c in detail
Conditional statement c++
Operators and expressions in c language
Variables in C Programming
Jumping statements
Lesson 5 php operators
Pointers in c++
Control statements
Unit ii chapter 2 Decision making and Branching in C
The string class
Conditional and control statement
Function overloading and overriding
C++ Function
 
Loops in Python
Exception handling c++
Type casting
Introduction Of C++
Ad

Similar to Repetition Structure (20)

PPT
Control structures ii
PPTX
Chapter 5.3
PPTX
Introduction to Java Programming - Lecture 11.pptx
PPT
Programming loop
PPT
Chap05
PPTX
Looping statements
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPT
9781439035665 ppt ch05
PPTX
Looping statements
PPTX
presentation on powerpoint template.pptx
PPSX
Control structures
PPTX
Java loops for, while and do...while
PPT
15-Loops.ppt
PPT
9781111530532 ppt ch05
PPT
9781111530532 ppt ch05
PDF
DSA 103 Object Oriented Programming :: Week 3
PPTX
130707833146508191
PDF
how to write loops in java explained vividly
PDF
Java Repetiotion Statements
PPT
loops in java seekh lo prh lo kaam aey .ppt
Control structures ii
Chapter 5.3
Introduction to Java Programming - Lecture 11.pptx
Programming loop
Chap05
Looping statements
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
9781439035665 ppt ch05
Looping statements
presentation on powerpoint template.pptx
Control structures
Java loops for, while and do...while
15-Loops.ppt
9781111530532 ppt ch05
9781111530532 ppt ch05
DSA 103 Object Oriented Programming :: Week 3
130707833146508191
how to write loops in java explained vividly
Java Repetiotion Statements
loops in java seekh lo prh lo kaam aey .ppt
Ad

More from PRN USM (19)

PPT
Graphical User Interface (GUI) - 2
PPT
Graphical User Interface (GUI) - 1
PPT
File Input & Output
PPT
Exception Handling
PPT
Inheritance & Polymorphism - 2
PPT
Inheritance & Polymorphism - 1
PPT
Array
PPT
Class & Object - User Defined Method
PPT
Class & Object - Intro
PPT
Selection Control Structures
PPT
Numerical Data And Expression
PPT
Introduction To Computer and Java
PPS
Corporate Social Responsibility And Challenges In Creating Smoke Free Environ...
PPS
Empowering Women Towards Smokefree Homes
PPS
Sfe The Singaporean Experience
PPS
Corporate Social Responsibility And Challenges In Creating Smoke Free Environ...
PPS
Malaysian Health Promotion Board (Mhpb) Objectives, Functions And Priorities
PPS
Role Of Ng Os In Tobacco Control
PPS
Application Of Grants From Mhpb
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 1
File Input & Output
Exception Handling
Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 1
Array
Class & Object - User Defined Method
Class & Object - Intro
Selection Control Structures
Numerical Data And Expression
Introduction To Computer and Java
Corporate Social Responsibility And Challenges In Creating Smoke Free Environ...
Empowering Women Towards Smokefree Homes
Sfe The Singaporean Experience
Corporate Social Responsibility And Challenges In Creating Smoke Free Environ...
Malaysian Health Promotion Board (Mhpb) Objectives, Functions And Priorities
Role Of Ng Os In Tobacco Control
Application Of Grants From Mhpb

Recently uploaded (20)

PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
History, Philosophy and sociology of education (1).pptx
 
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
 
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Virtual and Augmented Reality in Current Scenario
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Environmental Education MCQ BD2EE - Share Source.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
History, Philosophy and sociology of education (1).pptx
 
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Unit 4 Computer Architecture Multicore Processor.pptx
Cambridge-Practice-Tests-for-IELTS-12.docx
Paper A Mock Exam 9_ Attempt review.pdf.
LDMMIA Reiki Yoga Finals Review Spring Summer
202450812 BayCHI UCSC-SV 20250812 v17.pptx
 
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Hazard Identification & Risk Assessment .pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx

Repetition Structure

  • 2. Outlines  Repetition Statements  while statement  do..while statement  for statement  Nested loops  Repetition Control Structures
  • 3. Repetition Statements  Repetition statement (or loop) a block of code to be executed for a fixed number of times or until a certain condition is met.  In JAVA, repetition can be done by using the following repetition statements: a) while b) do…while c) for
  • 4. The while statement  The while statement evaluates expression/condition, which must return a boolean value. If the expression/condition is true, the statement(s) in the while block is/are executed.  Syntax: while (expression/condition) Statement(s);  It continues testing the expression/condition and executing its block until the expression/condition evaluates to false
  • 6. Output ? 1 2 3 4 Example 1 int i=1; while (i<5){ System.out.print(i + “”); i++; }
  • 7. Output ? SUM : 1 SUM : 7 Good Bye Example 2 int sum=0, number =1; while (number <= 10) { sum+=number; number = number + 5; System.out.println(“SUM :” + sum); } System.out.println(“Good Bye”);
  • 8. The do…while statement  It is similar to while loops except it first executes the statement(s) inside the loop, and then evaluates the expression/condition. If the expression is true, the statement(s) in the do loop is/are executed again.  Syntax do statement(s); while (expression);  It continues executing the statement until the expression/condition becomes false.  Note that the statement within the do loop is always executed at least once.
  • 10. Output ? 0 1 2 3 Example 3 int i=0; do{ System.out.print(i + “”); i++; }while(i<=3);
  • 11. Output ? SUM : 2 SUM : 9 Example 4 int sum=0, number =2; do{ sum+=number; number = number + 5; System.out.println(“SUM :” + sum); } while (number <= 10);
  • 12. The for statement  Usually used when a loop will be executed a set number of times.  Syntax: for(initial statement; loop condition; update statement) statement(s);  The for loop executes as follow: 1) The initial statement is executed. 2) The loop expression/condition is evaluated. If it is TRUE, the loop statement is executed followed by the execution of the update statement 3) Repeat step 2) until the loop condition evaluates to FALSE.
  • 14. Output ? 1 2 3 4 Example 5 for (i=1; i<5; i++) System.out.print(i);
  • 15. Output ? YAHOO ***YAHOO *** Example 6 for (i=1; i<3; i++){ System.out.print(“YAHOO” + “”); System.out.print(“***”); }
  • 16. Output ? YAHOO YAHOO YAHOO YAHOO YAHOO *** Example 7 for (i=1; i<=5; i++) System.out.print(“YAHOO”); System.out.print(“***”);
  • 17. Nested Loops  The placing of one loop inside the body of another loop is called nesting.  When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop.  While all types of loops may be nested, the most commonly nested loops are for loops.
  • 18. Nested for Loops  When working with nested loops, the outer loop changes only after the inner loop is completely finished (or is interrupted.).
  • 19. Example 8 int num1, num2; for(num2 = 0; num2 <= 2; num2++) { for(num1 = 0; num1 <= 1; num1++) { System.out.println(num2 + " " + num1); } } Output ? 0 0 0 1 1 0 1 1 2 0 2 1
  • 20. Infinite Loop  By using any repetition statements, make sure that the loop will eventually terminate.  An infinite loop occurs when a condition always evaluates to true and continues to execute endlessly. int product =0; for (product = 0;product < 10;) { product = product * 2; System.out.println(product); }
  • 21. Repetition Control Structures Repetition can be controlled by:  Counter controlled loop  Sentinel controlled loop  Flag controlled loop Exercise
  • 22. Counter Controlled Loop  Know exactly how many times a set of statements needs to be executed. Output ? 1 3 5 7 9 int num =1; while (num < 10) { System.out.print (num); num = num +2; } Example 10: back
  • 23. Sentinel Controlled Loop  You might not know exactly how many times a set of statements needs to be executed.  It uses a "special" or sentinel value to indicate that the loop is to end.  This must be a value that doesn't occur normally in the data.
  • 24. Example 11 (complete program) import java.util.Scanner; class sentinelLoop { public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an Integer, or -1 to stop: "); int choice= input.nextInt(); while (choice!=-1) { System.out.println("INSIDE LOOPING"); System.out.print("Enter an Integer, or -1 to stop: "); choice= input.nextInt(); } System.out.println("Sentinel value detected. Good Bye."); } }
  • 25. Example 11 …. Enter an Integer, or -1 to stop: 2 INSIDE LOOPING Enter an Integer, or -1 to stop: 5 INSIDE LOOPING Enter an Integer, or -1 to stop: 0 INSIDE LOOPING Enter an Integer, or -1 to stop: -1 Sentinel value detected. Good Bye. OUTPUT? back
  • 26. Flag Controlled Loop  Use a boolean variable to control the loop boolean found = false; while (!found){ : : if(expression) found = true; } back
  • 27. Exercise 1: What is the output of the following program? public class LoopExercise1 { public static void main (String args[]) { int choice=1, total=0; while (choice <4){ total = choice++; System.out.print(total); } } }
  • 28. Exercise 2: What is the output of the following program? public class LoopExercise2 { public static void main (String args[]){ for (int number =2; number <20; number++) { number = number *2; if (number <15) System.out.println(number);} } }
  • 29. Exercise 3: How many times is the following loop body repeated? public class LoopExercise3 { public static void main (String args[]) { int i=1; do { if ((i % 2)== 0) System.out.print(i); i++; } while(i<5); } }