The Break Statements

## Contents

###### Control Flow Statements
1
Introduction
PREVIEW37s
2
If Else Statement
PREVIEW20m 24s
7
For Loops
15m 38s
9
While Loop
18m 49s
14

## The course is part of this learning path

Start course
Difficulty
Beginner
Duration
3h 15m
Students
53
Ratings
4/5
Description

In this course, we look at control flow statements in the Java programming language.

### Learning Objectives

• Decision-making statements
• Looping statements
• Branching statements

### Intended Audience

• Anyone looking to get Oracle Java Certification
• Those who want to learn the Java Programming language from scratch
• Java developers who want to increase their knowledge
• Beginners with no previous coding experience in Java programming
• Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

### Prerequisites

• No prior knowledge is required about the Java programming language.
• Basic computer knowledge
Transcript

Hi there. In this section, we'll talk about the branching statements. The branching statements are used to change the normal flow of execution in some conditions. Java provides three branching statements, break, continue, and return. The break is used to terminate loops or switch statements. Control of the program moves to the next statement following the top. The continue keyword is used to continue the loop. When the continue statement is executed, control of the program jumps to the end of the loop and the loop condition is evaluated. Return is used to exit from the current method and control flow returns to where the method was invoked. Let's make some examples with branching statements. We're starting with the break statement.

In exercise project, right click on the source folder and select 'New' 'Class.' Specify the package name as branching and the class name as Break Statement and select the check box for the main method. First, we use the break statement in a switch case statement. Let's declare a variable a type of int and the initial value can be zero. Conditional switch expression is variable a. We'll create two cases. The first one is zero. If the value of variable a is zero then we will print Hi to the console and we will add the break statement. Let's copy, paste this case and change the case value. And if the value is one, then we'll print Hello to the console. We use the break statement again. The semicolon is required for the break keyword.

Let's run the code. You see the Hi message in the console. Let's change the variable of a with one. Let's run the code. This time you see the hello message in the console. Let's change the value of variable a with zero. If we don't use the break keyword in the first case, what will happen? Let's remove the break keyword from the code. Let's run the code. You see the Hi message and the Hello message in the console. So all the cases will be executed until it sees the break keyword. It's recommended to put the break after each case. We've already learned this way of using the break keyword in the switch case lesson. Now let's look at how to use it in the for loop. I'll convert these codes to the comment line.

In for loop, we declare a variable i with the initial value zero. Our condition is i less than 10 and it increments by one. If i equals seven, we use the break statement to terminate the loop. We use the print method to write numbers to screen. Let's run the code. You see the numbers from zero to six. In this code, when the value of variable i becomes seven, the if condition is evaluated to be true, then the break statement terminates the loop. Pay attention, not the if statement, it terminates the loop. Now let's do another example with for each loop. I will convert these codes to the comment line. First, I will create a string array. The name of this array can be 'cars' and I'll initialize this array. The first element of this array can be 'Mercedes.' The second element can be 'BMW.'  The third can be 'Ferrari.' The forth can be 'Opel.' The fifth can be 'Ford.'

And now let's create the for each loop. for(string car : cars). And I'll create an if statement in the for each loop. if(car == "Opel"), I'll use the break keyword and after the statement, I'll print the car object to the console. Before running the code, let's examine it. The string array has five elements. With the for each loop, we access the elements of the array in order. In other words, the 'Mercedes' element will be transferred to the 'car' variable in the first iteration. 'BMW' in the second iteration, 'Ferrari' in the third. In this way the loop will continue. Of course, we check the value passed to the car object with the if statement in each loop. Whenever the value passed to the car object is Opel, this time, the if condition will be satisfied and the compiler will enter in the if block. Since we are using the break keyword here, the loop will terminate at this stage and the compiler will go out of the loop.

Therefore, 'Mercedes,' 'BMW,' and 'Ferrari,' will be printed on the console screen. Let's run it and test it. As you can see Mercedes, BMW, and Ferrari were printed on the console screen. Now, let's do this example using the while loop and the index number of the elements in the array. First, let's create a variable called index number of type int. Its initial value will be zero. I'll also create a variable called 'car' where we will pass each element of the array to a variable of type string. Let the initial value be 'null'. Now let's create the while loop. Also, as a condition, (indexNumber < cars.length). Here, I'll first transfer each element of the array to the variable 'car' car = cars[indexNumber]. If you remember, we could access the index element of the array in this way, using the index number.

Now, let's create an if statement as before. if car == "Opel", I will use the break keyword. Finally, Let's increase the index number by one. This time, I'll create the print method outside of the while loop. Thus, we'll also see which element was transferred to the variable 'car' last. We're expecting the element 'Opel' to be printed on the console. Let's run and test our code. As you can see, it says Opel on the console screen because when the element 'Opel' is passed to the 'car' variable, the condition in the if statement becomes true and the break keyword is activated and the loop is terminated. Therefore, the last element 'Opel' was transferred to the 'car' variable. Now I'll talk about the concept of label which is still used in the literature, although it is used very rarely. If you want, at the beginning of your loops you can use a label that defines that loop. And after the break keyword, you can more easily follow which loop to terminate by using this label.

Let's run this loop once again using a label. At the beginning of the loop, just to the left of the while keyword, I'll write a statement representing this loop. For example, let the label of the loop be 'car Loop.' Then I put a colon between the label and the loop. Now let's write the label on the loop after the break keyword, namely the car Loop statement. Now let's run the application and test it. As you can see, the result is the same, nothing has changed. It's good to know that there is such a use . Now I will give one last tip and end the lesson. Can we use the break keyword anywhere? Let's try to use the break statement anywhere. Let's declare a variable number and its initial value. Let it be 100. If the number equals 100, we try to use the break keyword. But we get a compilation error because break cannot be used outside of a loop or a switch. If you see such usage, it's wrong. Yes, I think the break keyword is understood. Let's take a short break here. See you in the next lesson.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8808525,"math_prob":0.7062446,"size":7770,"snap":"2023-14-2023-23","text_gpt3_token_len":1720,"char_repetition_ratio":0.17228946,"word_repetition_ratio":0.03939828,"special_character_ratio":0.22136422,"punctuation_ratio":0.11630847,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9594613,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-21T11:41:24Z\",\"WARC-Record-ID\":\"<urn:uuid:d4c13c82-c917-4e4a-a8da-3e9b2efe2dde>\",\"Content-Length\":\"248192\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d038bd7f-dd95-4572-865b-1ee43603029a>\",\"WARC-Concurrent-To\":\"<urn:uuid:944767ea-fc89-443b-9582-0bb7899eaffc>\",\"WARC-IP-Address\":\"108.138.85.102\",\"WARC-Target-URI\":\"https://cloudacademy.com/course/control-flow-statements-3181/the-break-statements/\",\"WARC-Payload-Digest\":\"sha1:ELTJG65EM35ETH7SNTTIRJ3I35SK7CPS\",\"WARC-Block-Digest\":\"sha1:SGPFM3Z3DJKDPVMSLHQZDVDRDVCWEPFE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296943695.23_warc_CC-MAIN-20230321095704-20230321125704-00386.warc.gz\"}"}