java-development-for-beginners-learnit / 09 - Iteration Statements (Loops) in Java /007 Labels in Java_en.srt
| 1 | |
| 00:00:05,000 --> 00:00:11,000 | |
| They will discuss what the labels endure and how you can use them labels in combination with break and | |
| 2 | |
| 00:00:11,000 --> 00:00:16,000 | |
| continue statement, allow us to implement some kind of go to statement in Java. | |
| 3 | |
| 00:00:16,000 --> 00:00:23,000 | |
| But first of all, what is go to statement go to is a statement in different programming languages which | |
| 4 | |
| 00:00:23,000 --> 00:00:28,000 | |
| allows you to pass control of execution to any line in your code without any condition. | |
| 5 | |
| 00:00:29,000 --> 00:00:35,000 | |
| The jump two lines of code are usually identified by labels, but you have to consider it to be a bad | |
| 6 | |
| 00:00:35,000 --> 00:00:36,000 | |
| practice. | |
| 7 | |
| 00:00:36,000 --> 00:00:43,000 | |
| That's why in Java go to is reserved keyword but never used why it is considered to be a bad practice | |
| 8 | |
| 00:00:43,000 --> 00:00:48,000 | |
| because code which contains goto statements is less readable and it is harder to maintain. | |
| 9 | |
| 00:00:49,000 --> 00:00:52,000 | |
| Also, it is extremely hard to test code with code to statements. | |
| 10 | |
| 00:00:53,000 --> 00:00:55,000 | |
| But in Java we have labels. | |
| 11 | |
| 00:00:55,000 --> 00:01:00,000 | |
| We can use labels to break the loop marked with specific label or to continue another loop. | |
| 12 | |
| 00:01:01,000 --> 00:01:06,000 | |
| I would say that labels with break statements in Java is more structured form of goto statement. | |
| 13 | |
| 00:01:07,000 --> 00:01:10,000 | |
| You're already aware that breaks statement breaks only closed loop. | |
| 14 | |
| 00:01:11,000 --> 00:01:16,000 | |
| But in case you have multiple nested loops, how you can break the main loop labels will help you to | |
| 15 | |
| 00:01:16,000 --> 00:01:17,000 | |
| do this. | |
| 16 | |
| 00:01:17,000 --> 00:01:19,000 | |
| Let me show you how you can do this. | |
| 17 | |
| 00:01:20,000 --> 00:01:22,000 | |
| In this example, we have two loops here. | |
| 18 | |
| 00:01:22,000 --> 00:01:25,000 | |
| One loop is nested and we have two labels. | |
| 19 | |
| 00:01:26,000 --> 00:01:29,000 | |
| Label is any invalid went into fire followed by a colon. | |
| 20 | |
| 00:01:30,000 --> 00:01:32,000 | |
| We have loop one and loop two here. | |
| 21 | |
| 00:01:33,000 --> 00:01:38,000 | |
| Once we have label block of code, we can use labels as a target of a break and a continuous statement. | |
| 22 | |
| 00:01:39,000 --> 00:01:45,000 | |
| In this case, if counter in nested loop is less than three, we continue our execution from this block. | |
| 23 | |
| 00:01:45,000 --> 00:01:47,000 | |
| And this is just an example. | |
| 24 | |
| 00:01:47,000 --> 00:01:50,000 | |
| In real life, you can continue execution from any block you wish. | |
| 25 | |
| 00:01:51,000 --> 00:01:57,000 | |
| But in case our counter in nested loop is equal to three or more, we break our main loop. | |
| 26 | |
| 00:01:57,000 --> 00:01:59,000 | |
| That's one console output. | |
| 27 | |
| 00:01:59,000 --> 00:02:01,000 | |
| You can see that we managed to print. | |
| 28 | |
| 00:02:01,000 --> 00:02:07,000 | |
| I counter only once in console and the main loop was broken by the statement. | |
| 29 | |
| 00:02:08,000 --> 00:02:10,000 | |
| So now you know how to use labels. | |
| 30 | |
| 00:02:11,000 --> 00:02:17,000 | |
| Let's recap what we have learned in this section about loops we learn how to use for type of loops. | |
| 31 | |
| 00:02:18,000 --> 00:02:22,000 | |
| We also discussed how we can use nested loops, jam statements and labels. | |
| 32 | |
| 00:02:23,000 --> 00:02:30,000 | |
| Now, let's review your homework, recommend you read books, chapter about loops, and here's recording | |
| 33 | |
| 00:02:30,000 --> 00:02:31,000 | |
| exercises for you to practice. | |
| 34 | |
| 00:02:32,000 --> 00:02:35,000 | |
| What will review solution for these scoring exercises together? | |