From the course: Java 8 Essential Training
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using the switch statement - Java Tutorial
From the course: Java 8 Essential Training
Using the switch statement
- The if and else keywords give you complete flexibility in writing conditional code. But you can sometimes write conditional code more efficiently using the switch statement. A switch statement examines a single value and then compares it to multiple possibilities, and you can control the flow of execution based on when the two values match. I have code in place to collect a value from the user. I then parse that value as a primitive int. I'll run the code for the first time and show that in the run window I can click here, type in a number, and then the application finishes, but nothing else happens. My goal is to examine that value. I'll place the cursor after the existing code and I'll add the switch keyword. IntellJ IDEA adds parentheses. Place the value you want to examine within the parentheses. I'll place the value monthNumber. Then add a pair of braces, that's the switch code block. Now within the switch code…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Programming conditional logic3m 45s
-
(Locked)
Using the switch statement4m 51s
-
(Locked)
Creating looping code blocks5m 23s
-
(Locked)
Creating reusable code with methods4m 15s
-
(Locked)
Overloading methods with different signatures4m 51s
-
(Locked)
Passing arguments by reference or by value4m 47s
-
(Locked)
-
-
-
-
-
-