Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Java Workshop

You're reading from   The Java Workshop Learn object-oriented programming and kickstart your career in software development

Arrow left icon
Product type Paperback
Published in Oct 2019
Publisher Packt
ISBN-13 9781838986698
Length 606 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (3):
Arrow left icon
Eric Foster-Johnson Eric Foster-Johnson
Author Profile Icon Eric Foster-Johnson
Eric Foster-Johnson
Andreas Göransson Andreas Göransson
Author Profile Icon Andreas Göransson
Andreas Göransson
David Cuartielles David Cuartielles
Author Profile Icon David Cuartielles
David Cuartielles
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Getting Started 2. Learning the Basics FREE CHAPTER 3. Object-Oriented Programming 4. Collections, Lists and Java's Built-In APIs 5. Exceptions 6. Libraries, Packages, and Modules 7. Databases and JDBC 8. Sockets, Files, and Streams 9. Working with HTTP 10. Encryption 11. Processes 12. Regular Expressions 13. Functional Programming with Lambda Expressions 14. Recursion 15. Processing Data with Streams 16. Predicates and Other Functional Interfaces 17. Reactive Programming with Java Flow 18. Unit Testing Appendix

Looping and Performing Repetitive Tasks

In this chapter, we cover using loops to perform repetitive tasks. The main types of loop are as follows:

  • for loops
  • while loops
  • do-while loops

for loops repeat a block a set number of times. Use a for loop when you are sure how many iterations you want. A newer form of the for loop iterates over each item in a collection.

while loops execute a block while a given condition is true. When the condition becomes false, the while loop stops. Similarly, do-while loops execute a block and then check a condition. If true, the do-while loop runs the next iteration.

Use while loops if you are unsure how many iterations are required. For example, when searching through data to find a particular element, you normally want to stop when you find it.

Use a do-while loop if you always want to execute the block and only then check if another iteration is needed.

Looping with the for Loop

A for loop executes the same block...

You have been reading a chapter from
The Java Workshop
Published in: Oct 2019
Publisher: Packt
ISBN-13: 9781838986698
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime