Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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
Java Programming for Beginners

You're reading from   Java Programming for Beginners Learn the fundamentals of programming with Java

Arrow left icon
Product type Paperback
Published in Oct 2017
Publisher Packt
ISBN-13 9781788296298
Length 318 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
SkillSprints Inc., Mark Lassoff SkillSprints Inc., Mark Lassoff
Author Profile Icon SkillSprints Inc., Mark Lassoff
SkillSprints Inc., Mark Lassoff
Arrow right icon
View More author details
Toc

Switch, case, and break

In this section, we're going to take a look at the switch statement, which is another way that we can modify the control flow of our program.

To begin, let's create a new project in NetBeans. At my end at least, I'm going to get rid of all these comments. To demonstrate the power of the switch statement, we're going to start by writing a program using only if blocks, then we'll convert the program to one that uses switch statements. The following are the steps for the program that uses only if blocks:

  1. To begin, let's simply declare a variable x, (int x =1;), and here is our goal: If the value of x is 1, 2, or 3, we'd like to print out the responses RED, BLUE, or GREEN, respectively. If x is not one of those numbers, we'll just print out a default response.
  1. Doing this with if blocks is pretty straightforward, if...
lock icon The rest of the chapter is locked
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
Banner background image