Hey CodSter (CODer+teSTER), From today onwards I am going to call automation engineers as Codster because they are the only superman on this planet who write codes to perform end to end functional testing. Hope it sounds good. Well, let’s start today’s learning journey. Before we begin, let me remind you, yesterday we discussed For loop, For each loop, While loop and do while loop in Java, and today we are going to discuss Break statement and Continue statement which is going to be used within loops, hence, you need to recall your iteration & switching concepts of Java. Along with this, we are going to discuss ways to write comments in Java and about Java documentation as well, we simply call it as Java doctool.
Let’s begin our Java learning journey!
Break Statement in Java
When we want to immediately terminate the loop to execute the next statement then we implement break operation with the help of a break statement.
Where can we implement the Break statement in Java?
We can use a break statement at the following places:
- To jump to the next case of the switch statement
- We use the break statement to break the inner loop
- It is even used to break the outer loop
- Break statement is used with all types of loops like For, For each, While and Do-While loop
Here are some of the sample programs where the use of the break statement is depicted:
Sample Program
public static void main(String[] args) { for(int i=1;i<=10;i++){ if(i==9){ break; } System.out.println(i); } }
Sample Program: Java Break statement used with the inner loop
Sample Program: Java Break statement used with the labeled for loop
Sample Program: Java Break statement used with the switch statement
Sample Program
public static void main(String[] args) { int i=0; while(i<=10){ if(i==9){ i++; break; } System.out.println(i); i++; } }
Sample Program
public static void main(String[] args) { int i=1; do{ if(i==9){ i++; break; } System.out.println(i); i++; }while(i<=10); }
Continue Statement in Java
Continue statement is used in a loop when there is a requirement to jump to the next iteration by skipping the current one on fulfilling certain conditions. The continue statement is used with all the loops.
Let’s look at some sample programs listed below. It will magnify your understanding of the Continue statement.
Sample Program
public static void main(String[] args) { a1: for(int i=1;i<=3;i++){ a2: for(int j=1;j<=3;j++){ if(i==3&&j==5){ continue a1; } System.out.println(i+" "+j); } } }
public static void main(String[] args) { int i=0; while(i<=10){ if(i==9){ i++; continue; } System.out.println(i); i++; } }
public static void main(String[] args) { int i=1; do{ if(i==9){ i++; continue; } System.out.println(i); i++; }while(i<=10); }
Comments in Java
Comments in Java are used to provide information about the Java statements. Comments are neither compiled nor interpreted by the Java compiler and interpreter. We use comments to hide some part of the code which we do not want to execute.
Types of Java Comments
There are mainly two types of Java comments, which are listed below:
Java Single Line Comment
The single-line comment is used to comment a single line.
//I am single line commentator
Java Multi Line Comment
The multi-line comment is used to comment multiple lines of statements at a time.
/*I Am Multi Line Commentator */
Java Doctool
Java documentation tool called as Java doctool in short. It is used to create the documentation API. Java doctool is also a kind of comment which provides documentation environments.
/** I Am Using Javadoctool */
This was all about break statement, Continue statement, Comments, and Javadoctool of Java. If you have any further questions, please share in comments below.