From 57818a6538b7e60bd19785b1083d37fbc91aae65 Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Wed, 15 Mar 2023 14:45:44 -0400 Subject: [PATCH 01/24] Add start of if/else! ... Co-Authored-By: Mitch Masia <7084914+masiamj@users.noreply.github.com> --- src/SUMMARY.md | 11 ++- src/branching_logic/boolean_expressions.md | 30 ++++++ src/branching_logic/conditional_operator.md | 28 ++++++ src/branching_logic/else.md | 35 +++++++ src/branching_logic/else_if.md | 63 ++++++++++++ src/branching_logic/if.md | 38 ++++++++ src/branching_logic/nested_ifs.md | 30 ++++++ .../relation_to_delayed_assignment.md | 37 ++++++++ src/branching_logic/ternary_expression.md | 1 - src/branching_paths.md | 17 ++++ src/getting_started.md | 94 ++++++++++++++++++ src/getting_started/hello_world.md | 95 +------------------ src/strings/access_individual_characters.md | 2 +- 13 files changed, 380 insertions(+), 101 deletions(-) create mode 100644 src/branching_logic/boolean_expressions.md create mode 100644 src/branching_logic/conditional_operator.md create mode 100644 src/branching_logic/nested_ifs.md delete mode 100644 src/branching_logic/ternary_expression.md create mode 100644 src/branching_paths.md create mode 100644 src/getting_started.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 04368e4..4ca1698 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -13,15 +13,14 @@ - [tutorialspoint](./examples/tutorialspoint.md) - [tutorialspoint paid course](./examples/tutorialspoint_paid.md) - [w3schools](./examples/w3schools.md) --> -# Getting Started - -- [Hello, World](./getting_started/hello_world.md) # Modern Java + - [Prelude](./prelude.md) - [Asking for Help](./prelude/asking_for_help.md) - [Toy Problems](./prelude/toy_problems.md) +- [Getting Started](./getting_started.md) - [First Steps](./first_steps.md) - [Comments](./first_steps/comments.md) - [Semicolons](./first_steps/semicolon.md) @@ -95,12 +94,14 @@ # Control Flow I -- [Branching Logic](./branching_logic.md) +- [Branching Paths](./branching_paths.md) - [If](./branching_logic/if.md) - [Else](./branching_logic/else.md) + - [Nested Ifs](./branching_logic/nested_ifs.md) - [Else If](./branching_logic/else_if.md) - [Relation to Delayed Assignment](./branching_logic/relation_to_delayed_assignment.md) - - [Ternary Expression](./branching_logic/ternary_expression.md) + - [Conditional Operator](./branching_logic/conditional_operator.md) + - [Boolean Expressions](./branching_logic/boolean_expressions.md) - [Challenges](./branching_logic/challenges.md) - [Loops](./loops.md) - [While](./loops/while.md) diff --git a/src/branching_logic/boolean_expressions.md b/src/branching_logic/boolean_expressions.md new file mode 100644 index 0000000..0457428 --- /dev/null +++ b/src/branching_logic/boolean_expressions.md @@ -0,0 +1,30 @@ +# Boolean Expressions + +A common thing I've seen students do is set the initial value of some +`boolean` variable based on some condition. + +```java +int age = 22; + +boolean canRent; +if (age > 25) { + canRent = true; +} +else { + canRent = false; +} + +// or +// boolean canRent = age > 25 ? true : false; + +System.out.println(canRent); +``` + +This is valid code, but very often can be made simpler if you remember that the condition +itself already evaluates to a `boolean`. You can directly assign the variable to that value. + +```java +int age = 22; +boolean canRent = age > 25; +``` + diff --git a/src/branching_logic/conditional_operator.md b/src/branching_logic/conditional_operator.md new file mode 100644 index 0000000..d027414 --- /dev/null +++ b/src/branching_logic/conditional_operator.md @@ -0,0 +1,28 @@ +# Conditional Operator + +When the only operation being performed inside of an `if` and `else` pair +is setting the initial value of a variable, you can use the "conditional operator"[^ternary] +to perform that assignment instead. + + +```java +int age = 22; + +String message = age < 25 + ? "You cannot rent a car!" + : "You might be able to rent a car"; +``` + +You write a condition followed by a `?`, a value to use when that condition evaluates to `true`, a `:`, +and then a value to use when that condition evaluates to `false`. + +```java +CONDITION ? WHEN_TRUE : WHEN_FALSE +``` + + + + + +[^ternary]: Some people will call this a ternary expression. Ternary meaning "three things." +Same idea as tres leches. \ No newline at end of file diff --git a/src/branching_logic/else.md b/src/branching_logic/else.md index 3cf590b..f07eb9a 100644 --- a/src/branching_logic/else.md +++ b/src/branching_logic/else.md @@ -1 +1,36 @@ # Else + +When you want to do one thing when a condition evaluates to `true` +and another when that same condition evaluates to `false` you can use `else`. + +```java +int age = 30; // πŸ™Žβ€β™€οΈ +if (age < 25) { + System.out.println("You cannot rent a car!"); +} +else { + System.out.println("You might be able to rent a car."); +} +``` + +You write the word `else` immediately after the `}` at the end of an `if` statement, then +some code inside of a new `{` and `}`. + +```java +if (CONDITION) { + +} +else { + +} +``` + +When the condition evaluates to `false`, the code inside of `else`'s `{` and `}` will run. + +`else` cannot exist without a matching `if`, so this code does not work. + +```java +else { + System.out.println("No if."); +} +``` diff --git a/src/branching_logic/else_if.md b/src/branching_logic/else_if.md index 24b64c3..1bc247b 100644 --- a/src/branching_logic/else_if.md +++ b/src/branching_logic/else_if.md @@ -1 +1,64 @@ # Else If + +If you have an `if` nested in an `else` branch, you can simplify that by using +`else if`. + + +```java +boolean cool = true; // πŸ•ΆοΈ +int age = 30; // πŸ™Žβ€β™€οΈ +if (age < 25) { + System.out.println("You cannot rent a car!"); +} +else { + if (!cool) { + System.out.println("You failed the vibe check."); + } + else { + System.out.println("You are rad enough to rent a car."); + } +} +``` + +So the following will work the same as the code above. + +```java +boolean cool = true; // πŸ•ΆοΈ +int age = 30; // πŸ™Žβ€β™€οΈ + +if (age < 25) { + System.out.println("You cannot rent a car!"); +} +else if (!cool) { + System.out.println("You failed the vibe check."); +} +else { + System.out.println("You are rad enough to rent a car."); +} +``` + +You can have as many `else if`s as you need. Each one will only run if all the previous conditions +evaluate to `false`. + +```java +boolean cool = true; // πŸ•ΆοΈ +int age = 100; // πŸ‘΄ + +if (age < 25) { + System.out.println("You cannot rent a car!"); +} +else if (!cool) { + System.out.println("You failed the vibe check."); +} +else if (age > 99) { + System.out.println("You are too old to safely drive a car!"); +} +else if (age > 450) { + System.out.println("There can only be one! βš”οΈπŸ΄σ §σ ’σ ³σ £σ ΄σ Ώ"); +} +else { + System.out.println("You are rad enough to rent a car."); +} +``` + + diff --git a/src/branching_logic/if.md b/src/branching_logic/if.md index dac2f01..f1e912f 100644 --- a/src/branching_logic/if.md +++ b/src/branching_logic/if.md @@ -1 +1,39 @@ # If + +The way to represent a branching path in Java is by using an `if` statement. + +```java +int age = 5; // πŸ‘Ά +if (age < 25) { + System.out.println("You are too young to rent a car!"); +} +``` + +You write the word `if` followed by an expression which evaluates to a `boolean` inside of `(` and `)`. +This expression is the "condition". Then you write some code inside +of `{` and `}`. + + +```java +if (CONDITION) { + +} +``` + +When the condition evaluates to `true`, the code inside of the `{` and `}` will run. +If it evaluates to `false` that code will not run. + + +In this example the condition is `age < 25`. When `age` is less than 25 it will evaluate to `true` +and you will be told that you cannot rent a car. + + +```java +int age = 80; // πŸ‘΅ +if (age < 25) { + System.out.println("You are too young to rent a car!"); +} +``` + +If this condition evaluates to `false`, then the code inside of `{` and `}` +will not run. \ No newline at end of file diff --git a/src/branching_logic/nested_ifs.md b/src/branching_logic/nested_ifs.md new file mode 100644 index 0000000..f678696 --- /dev/null +++ b/src/branching_logic/nested_ifs.md @@ -0,0 +1,30 @@ +# Nested Ifs + +The code inside of the `{` and `}` can be anything, including more `if` statments. + +```java +int age = 5; // πŸ‘Ά +if (age < 25) { + System.out.println("You are too young to rent a car!"); + if (age == 24) { + System.out.println("(but it was close)"); + } +} +``` + +When an `if` is inside another `if` we say that it is "nested". + +If you find yourself nesting more than a few `if`s that might be a sign that +you should reach out for help. + +```java +if (...) { + if (...) { + if (...) { + if (...) { + // Seek professional help + } + } + } +} +``` \ No newline at end of file diff --git a/src/branching_logic/relation_to_delayed_assignment.md b/src/branching_logic/relation_to_delayed_assignment.md index 7cd75a3..adb716e 100644 --- a/src/branching_logic/relation_to_delayed_assignment.md +++ b/src/branching_logic/relation_to_delayed_assignment.md @@ -1 +1,38 @@ # Relation to Delayed Assignment + +Delayed assignment of variables becomes useful with `if` and `else`. + +So long as Java can figure out that a variable will always be given an initial value +inside of an `if` and `else`, you will be allowed to use that variable. + + +```java +int age = 22; + +String message; +if (age > 25) { + message = "You might be able to rent a car"; +} +else { + message = "You cannot rent a car!"; +} + +System.out.println(message); +``` + +If it will not always be given an initial value, then you will not be allowed to +use that variable. + +```java +int age = 22; + +String message; +if (age > 25) { + message = "You might be able to rent a car"; +} + +// message is not always given an initial value +// so you cannot use it. +System.out.println(message); +``` + diff --git a/src/branching_logic/ternary_expression.md b/src/branching_logic/ternary_expression.md deleted file mode 100644 index 1f48a9d..0000000 --- a/src/branching_logic/ternary_expression.md +++ /dev/null @@ -1 +0,0 @@ -# Ternary Expression diff --git a/src/branching_paths.md b/src/branching_paths.md new file mode 100644 index 0000000..786c15d --- /dev/null +++ b/src/branching_paths.md @@ -0,0 +1,17 @@ +# Branching Paths + +All the code I have shown you so far has run from top to bottom. That is, it has followed a single "path." + +Not all programs can follow a single path though. + +Imagine trying to rent a car online. The first thing you might be asked is your age. +This is because most car rental companies do not want to rent to people under the age of 25.[^insurance]. + +If you enter an age that is less than 25, the program should immediately tell you that you cannot +rent a car. If you enter an age that is greater than or equal to 25, the program should continue to prompt you +for more information. + +There are multiple "branching paths" that the program might take. + + +[^insurance]: For insurance reasons. \ No newline at end of file diff --git a/src/getting_started.md b/src/getting_started.md new file mode 100644 index 0000000..c193ec0 --- /dev/null +++ b/src/getting_started.md @@ -0,0 +1,94 @@ +# Getting Started + +There are a lot of ways to "get set up" to run Java code. + +For the purposes of this book, I am going to reccomend that you +start with [replit.com](https://replit.com). + +It requires an internet connection and you will have to make an account, but +of the available options it is the easiest to set up. + +If you are in school and your teacher has helped you get set up in some other +way it is okay to skip this section and just do it the way you were shown. + +All that matters is that in the end you have the ability to run and +edit the following code. + +~IF toplevel_anonymous_class + +```java +void main() { + System.out.println("Hello, World"); +} +``` + +~ELSE + +```java +public class Main { + public static void main(String[] args) { + System.out.println("Hello, World"); + } +} +``` + +~ENDIF + +## Step 1. Make an account + +Go to [replit.com](https://replit.com) and find the "Sign Up" button. +Websites change every now and then so these screenshots might be out of date. + +Picture of the sign up button on replit's website + +Click it and sign up for an account. + +Picture of the sign up form on replit's website + +## Step 2. Create a Java REPL + +Find the "Create REPL" button and click it. + +Picture of the create repl button on replit's website + +Then you should be presented with a menu that lets you search for the type of REPL to create. +Find the Java template and click "Create". + +Unfilled in create from template menu on replit + +Filled in create from template menu on replit + +## Step 3. Run code + +You should land on a screen with a big green run button, an open file called +"Main.java", and a blank window labeled "console". + +Picture of an unran hello world program + +Click it and you should see the text `Hello, world!` appear under the console window. + +Picture of a hello world program after running diff --git a/src/getting_started/hello_world.md b/src/getting_started/hello_world.md index 4fba1e3..bad5562 100644 --- a/src/getting_started/hello_world.md +++ b/src/getting_started/hello_world.md @@ -1,94 +1 @@ -# Hello, World - -There are a lot of ways to "get set up" to run Java code. - -For the purposes of this book, I am going to reccomend that you -start with [replit.com](https://replit.com). - -It requires an internet connection and you will have to make an account, but -of the available options it is the easiest to set up. - -If you are in school and your teacher has helped you get set up in some other -way it is okay to skip this section and just do it the way you were shown. - -All that matters is that in the end you have the ability to run and -edit the following code. - -~IF toplevel_anonymous_class - -```java -void main() { - System.out.println("Hello, World"); -} -``` - -~ELSE - -```java -public class Main { - public static void main(String[] args) { - System.out.println("Hello, World"); - } -} -``` - -~ENDIF - -## Step 1. Make an account - -Go to [replit.com](https://replit.com) and find the "Sign Up" button. -Websites change every now and then so these screenshots might be out of date. - -Picture of the sign up button on replit's website - -Click it and sign up for an account. - -Picture of the sign up form on replit's website - -## Step 2. Create a Java REPL - -Find the "Create REPL" button and click it. - -Picture of the create repl button on replit's website - -Then you should be presented with a menu that lets you search for the type of REPL to create. -Find the Java template and click "Create". - -Unfilled in create from template menu on replit - -Filled in create from template menu on replit - -## Step 3. Run code - -You should land on a screen with a big green run button, an open file called -"Main.java", and a blank window labeled "console". - -Picture of an unran hello world program - -Click it and you should see the text `Hello, world!` appear under the console window. - -Picture of a hello world program after running +# Getting Started diff --git a/src/strings/access_individual_characters.md b/src/strings/access_individual_characters.md index a1fb0a4..edca4d1 100644 --- a/src/strings/access_individual_characters.md +++ b/src/strings/access_individual_characters.md @@ -34,7 +34,7 @@ char r = assassin.charAt(indexOfR); System.out.println(r); ``` -If you give a number equal to or greater than the lenth of the `String` or a number less than zero, +If you give a number equal to or greater than the length of the `String` or a number less than zero, you will get an error. ```java From b163263e1016da36492dfad239ec0c5e0fff3801 Mon Sep 17 00:00:00 2001 From: Ethan McCue Date: Sat, 18 Mar 2023 12:46:18 -0400 Subject: [PATCH 02/24] loops --- book.toml | 4 + src/SUMMARY.md | 8 +- src/branching_logic/boolean_expressions.md | 2 + src/branching_logic/challenges.md | 118 ++++ src/challenges.md | 1 - src/loops.md | 10 + src/loops/break.md | 36 ++ src/loops/continue.md | 21 + src/loops/do.md | 1 - src/loops/do_while.md | 37 ++ src/loops/endless_loops.md | 24 + src/loops/labeled_break.md | 59 ++ src/loops/labeled_continue.md | 18 + src/loops/nested_loops.md | 73 +++ src/loops/termination_conditions.md | 1 - src/loops/unreachable_code.md | 15 + src/loops/while.md | 39 ++ src/prelude/asking_for_help.md | 11 +- theme/book.js | 688 +++++++++++++++++++++ theme/css/chrome.css | 538 ++++++++++++++++ theme/css/general.css | 203 ++++++ theme/css/print.css | 54 ++ theme/css/variables.css | 255 ++++++++ theme/favicon.png | Bin 0 -> 5679 bytes theme/favicon.svg | 22 + theme/highlight.css | 82 +++ theme/highlight.js | 6 + theme/index.hbs | 316 ++++++++++ 28 files changed, 2630 insertions(+), 12 deletions(-) delete mode 100644 src/challenges.md delete mode 100644 src/loops/do.md create mode 100644 src/loops/do_while.md create mode 100644 src/loops/endless_loops.md create mode 100644 src/loops/labeled_break.md create mode 100644 src/loops/labeled_continue.md create mode 100644 src/loops/nested_loops.md delete mode 100644 src/loops/termination_conditions.md create mode 100644 src/loops/unreachable_code.md create mode 100644 theme/book.js create mode 100644 theme/css/chrome.css create mode 100644 theme/css/general.css create mode 100644 theme/css/print.css create mode 100644 theme/css/variables.css create mode 100644 theme/favicon.png create mode 100644 theme/favicon.svg create mode 100644 theme/highlight.css create mode 100644 theme/highlight.js create mode 100644 theme/index.hbs diff --git a/book.toml b/book.toml index 2d83f3d..08c3a61 100644 --- a/book.toml +++ b/book.toml @@ -10,6 +10,10 @@ git-repository-url = "https://github.com/Together-Java/ModernJava" edit-url-template = "https://github.com/Together-Java/ModernJava/edit/develop/{path}" mathjax-support = true +[output.html.fold] +enable = true # whether or not to enable section folding +level = 0 # the depth to start folding + [preprocessor.features] command = "python3 features.py" # Not ready diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 4ca1698..4137e81 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -105,10 +105,14 @@ - [Challenges](./branching_logic/challenges.md) - [Loops](./loops.md) - [While](./loops/while.md) - - [Termination Conditions](./loops/termination_conditions.md) + - [Endless Loops](./loops/endless_loops.md) - [Break](./loops/break.md) - [Continue](./loops/continue.md) - - [Do](./loops/do.md) + - [Unreachable Code](./loops/unreachable_code.md) + - [Do While](./loops/do_while.md) + - [Nested Loops](./loops/nested_loops.md) + - [Labeled Break](./loops/labeled_break.md) + - [Labeled Continue](./loops/labeled_continue.md) - [Challenges](./loops/challenges.md) # User Defined Types diff --git a/src/branching_logic/boolean_expressions.md b/src/branching_logic/boolean_expressions.md index 0457428..b1a6344 100644 --- a/src/branching_logic/boolean_expressions.md +++ b/src/branching_logic/boolean_expressions.md @@ -26,5 +26,7 @@ itself already evaluates to a `boolean`. You can directly assign the variable to ```java int age = 22; boolean canRent = age > 25; + +System.out.println(canRent); ``` diff --git a/src/branching_logic/challenges.md b/src/branching_logic/challenges.md index 9358534..2995476 100644 --- a/src/branching_logic/challenges.md +++ b/src/branching_logic/challenges.md @@ -1 +1,119 @@ # Challenges + +Remember the rules for this are + +* Try to use only the information given up to this point in this book. +* Try not to give up until you've given it a solid attempt + +## Challenge 1 + +Write code that will outputs `The number is even` if `x` is an even number. + +~IF toplevel_anonymous_class + +```java +void main() { + // Change this variable to different numbers + // to test your code + int x = 5; + + // < YOUR CODE HERE > +} +``` + +~ELSE + +```java +public class Main { + public static void main(String[] args) { + // Change this variable to different numbers + // to test your code + int x = 5; + + // < YOUR CODE HERE > + } +} +``` + +~ENDIF + +## Challenge 2 + +Make it so that your code from the previous problem will also output `The number is odd` +if the number is odd. + +## Challenge 3 + +Write code that will output `allowed` if the the `password` variable is equal to +`"abc123"` and `not allowed` if it isn't. + +~IF toplevel_anonymous_class + +```java +void main() { + // Change this variable to different strings + // to test your code + String password = "apple"; + + // < YOUR CODE HERE > +} +``` + +~ELSE + +```java +public class Main { + public static void main(String[] args) { + // Change this variable to different strings + // to test your code + String password = "apple"; + + // < YOUR CODE HERE > + } +} +``` + +~ENDIF + +## Challenge 4 + +Write code that will assign the string `The number is {x} even` to `message` if `x` is an even number +and `The number is {x} odd` if `x` is an even number. + +So if `x` is 12 the string you should assign `The number 12 is even` to `message`. + +~IF toplevel_anonymous_class + +```java +void main() { + String message; + + // Change this variable to different numbers + // to test your code + int x = 5; + + // < YOUR CODE HERE > + + System.out.println(message); +} +``` + +~ELSE + +```java +public class Main { + public static void main(String[] args) { + String message; + + // Change this variable to different numbers + // to test your code + int x = 5; + + // < YOUR CODE HERE > + + System.out.println(message); + } +} +``` + +~ENDIF \ No newline at end of file diff --git a/src/challenges.md b/src/challenges.md deleted file mode 100644 index 9358534..0000000 --- a/src/challenges.md +++ /dev/null @@ -1 +0,0 @@ -# Challenges diff --git a/src/loops.md b/src/loops.md index 4b787b4..7c5f106 100644 --- a/src/loops.md +++ b/src/loops.md @@ -1 +1,11 @@ # Loops + +`if` and `else` let you write programs which can take branching paths, +but they will still run from the beginning to the end. + +Not all programs can just _end_ though. +Video games should draw their world at one second and do it again the next. +If you enter the wrong password on your phone, it should ask you for your password again. + +This is what "loops" are for. You run code starting from some point and then +loop back to that same point and run that code again. \ No newline at end of file diff --git a/src/loops/break.md b/src/loops/break.md index aa29be5..23e170c 100644 --- a/src/loops/break.md +++ b/src/loops/break.md @@ -1 +1,37 @@ # Break + +While loops will usually stop running when the condition at the top evaluates +to `false`. + +This can be bypassed by using the `break` statement. + +```java +int x = 5; +while (x > 0) { + if (x == 2) { + break; + } + x--; +} + +System.out.println( + "Final value of x is " + x +); +``` + +If a `break` is reached, the code in the loop stops running immediately. +The condition of the loop is not checked again. + +This can be useful in a variety of situations, but notably it is the only way to exit +from an otherwise endless loop. + +```java +while (true) { + System.out.println( + "The people started singing it not knowing what it was" + ); + + // Will immediately leave the loop + break; +} +``` \ No newline at end of file diff --git a/src/loops/continue.md b/src/loops/continue.md index ca98b56..c4c0696 100644 --- a/src/loops/continue.md +++ b/src/loops/continue.md @@ -1 +1,22 @@ # Continue + +Unless there is a `break`, while loops will usually run all the code in their body from top to bottom. + +The only other situation this will not happen is if a `continue` statement is reached. + +```java +// Will output a message for every number except 4 +int x = 5; +while (x > 0) { + if (x == 4) { + continue; + } + System.out.println(x + " is a good number"); + x--; +} +``` + +If a `continue` is reached the code in the loop stops running immediately but, unlike `break`, +the condition of the loop _is_ checked again. If it still evaluates to `true` then the code +in the loop will run again. + diff --git a/src/loops/do.md b/src/loops/do.md deleted file mode 100644 index 5af93df..0000000 --- a/src/loops/do.md +++ /dev/null @@ -1 +0,0 @@ -# Do diff --git a/src/loops/do_while.md b/src/loops/do_while.md new file mode 100644 index 0000000..a5bca64 --- /dev/null +++ b/src/loops/do_while.md @@ -0,0 +1,37 @@ +# Do + +One variation on a while loop is a "do-while loop." + +```java +int x = 0; +do { + System.out.println(x); + x++ +} while(x < 5); +``` + +You write `do`, some code inside of `{` and `}`, and then `while`, a condition inside of +`(` and `)`, and finally a semicolon. + +```java +do { + +} while (CONDITION); +``` + +In most situations it works exactly the same as a regular while loop. The only difference +is that the first time the loop is reached the condition for the loop is not checked. + +```java +int x = 0; +do { + System.out.println("this will run"); +} while (x != 0) + +while (x != 0) { + System.out.println("this will not run"); +} +``` + +One way to remember the difference is that in a "do-while loop" you always "do the thing" +at least once. \ No newline at end of file diff --git a/src/loops/endless_loops.md b/src/loops/endless_loops.md new file mode 100644 index 0000000..966aaca --- /dev/null +++ b/src/loops/endless_loops.md @@ -0,0 +1,24 @@ +# Endless Loops + +If a while loop will never end, we call that an endless loop. + +This can happen if the condition is a constant like `while (true)` + +```java +while (true) { + System.out.println("This is the song that never ends"); +} +``` + +Or if the variables tested in the condition are not updated inside of the loop. + +```java +// x is never changed +int x = 0; +while (x != 1) { + System.out.println("It goes on and on my friends"); +} +``` + +Many games should never really "finish" so at the very start of that sort of program it is not uncommon +to see a `while (true)`. \ No newline at end of file diff --git a/src/loops/labeled_break.md b/src/loops/labeled_break.md new file mode 100644 index 0000000..b35fa02 --- /dev/null +++ b/src/loops/labeled_break.md @@ -0,0 +1,59 @@ +# Labeled Break + +If you want to break out of a nested loop from one of the inner loops, you can use a "labeled break." + +```java +outerLoop: +while (true) { + while (true) { + break outerLoop; + } +} +``` + +To do this, before your outer while or do-while loop you need to add a "label" followed by a `:`. +A label is an arbitrary name just like a variable's name. + +```java +