0% found this document useful (0 votes)
1 views7 pages

6.1 Java - Lecture28 PDF

This lesson covers the concept of loops in Java, including while loops, do-while loops, and for loops, which are used to repeat blocks of code a specified number of times. Each loop consists of a counter declaration, a boolean expression, and a counter action. The lesson concludes with a note on the next topic, which is pre-tested vs post-tested loops.

Uploaded by

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

6.1 Java - Lecture28 PDF

This lesson covers the concept of loops in Java, including while loops, do-while loops, and for loops, which are used to repeat blocks of code a specified number of times. Each loop consists of a counter declaration, a boolean expression, and a counter action. The lesson concludes with a note on the next topic, which is pre-tested vs post-tested loops.

Uploaded by

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

Lesson 28

Repeating Blocks of
Code using Iteration
Statements (loops).

PRESENTED BY

Keith Vassallo icemalta.com


Loops
- Java provides several types of loops.
- Loops are used to iterate (repeat) a block of code a number of times.
- The number of times a loop ‘loops’ is known as the number of iterations
of that loop.

int m = 1; Counter declaration


while (m <= 10) { Boolean expression
System.out.println(m);
m++; Counter action
}

icemalta.com
- Note: m++ means ‘increase the value of m by 1’, or ‘increment m’.
Loops
- Each loop must contain:
• A counter declaration: This keeps track of the number of iterations the
loop has made.
• A boolean expression: When this evaluates to true, we run another
iteration. Otherwise, we stop the loop.
• A counter action: What to do to the counter after each iteration.
int m = 1;
while (m <= 10) {
System.out.println(m);
m++;

icemalta.com
}

- This will display the numbers from 1 to 10.


Loops
- The loop on the previous slide is called a while loop.
- Java also provides a do-while loop, which is very similar.

int k = 1;
do {
System.out.println(k);
k++;
} while (k <= 10);

- This loop does the same as the while loop, it displays the numbers 1 to 10.

icemalta.com
For Loop
- The for loop is popular since it encapsulates the elements required in a
loop into just one line.

for (int i=1; i <= 10; i++) {


System.out.println(i);
}

- This loop does the same as the other loops, it displays the numbers 1 to 10.

icemalta.com
Great work, you’ve completed this lesson!

Next up: Pre-Tested vs Post-Tested


Loops.

icemalta.com
© 2011-2017 Institute of Computer Education Ltd.

The contents of this document are copyright to the Institute of Computer Education Ltd, unless otherwise stated, and must not be reproduced without permission.

Every effort has been made to trace all of the copyright holders, but if any have been inadvertently overlooked the Institute of Computer Education Ltd. will be pleased to make the necessary arrangements at the first
opportunity. Please contact us directly. While the Institute of Computer Education Ltd. has taken all reasonable care in the preparation of this work, the Institute of Computer Education Ltd. makes no representation,
express or implied, with regard to the accuracy of the information contained in this work and cannot accept any legal responsibility or liability for any errors or omissions from the work or the consequences thereof.
The reader assumes sole responsibility for the selection of these materials to achieve its intended results. Products and services that are referred to in this work may be either trademarks and/or registered
trademarks of their respective owners. The editors and author/s make no claim to these trademarks. icemalta.com

You might also like