The Break Statements
Start course
Difficulty
Beginner
Duration
3h 15m
Students
53
Ratings
4/5
starstarstarstarstar-border
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.

 

About the Author
Students
1870
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics