Java Break and Continue Statement
15:21:00Java Break & Continue Statement
The Java break is used to break loop or switch
statement. It breaks the current flow of the program at specified condition. In
case of inner loop, it breaks only inner loop.
Syntax:
1.
jump-statement;
2.
break;
|
Java
Break Statement with Loop
Example:
1.
public class BreakExample {
2.
public static void main(String[] args) {
3.
for(int i=1;i<=10;i++){
4.
if(i==5){
5.
break;
6.
}
7.
System.out.println(i);
8.
}
9.
}
10. }
|
Output:
1
2
3
4
|
Java Continue Statement
The Java continue
statement is used to
continue loop. It continues the current flow of the program and skips the
remaining code at specified condition. In case of inner loop, it continues only
inner loop.
Syntax:
1.
jump-statement;
2.
continue;
|
Java
Continue Statement Example
Example:
1.
public class ContinueExample {
2.
public static void main(String[] args) {
3.
for(int i=1;i<=10;i++){
4.
if(i==5){
5.
continue;
6.
}
7.
System.out.println(i);
8.
}
9.
}
10. }
|
OUTPUT:
1
2
3
4
6
7
8
9
10
|
Java Comments
The java comments are statements that are not
executed by the compiler and interpreter. The comments can be used to provide
information or explanation about the variable, method, class or any statement.
It can also be used to hide program code for specific time.
Types of
Java Comments
There are 3 types of comments in java.
1.
Single Line Comment
2.
Multi Line Comment
1) Java
Single Line Comment
The single line comment is used to comment only one
line.
Syntax:
1.
//This is single line comment
Example:
1.
public class CommentExample1 {
2.
public static void main(String[] args) {
3.
int i=10;//Here, i is a variable
4.
System.out.println(i);
5.
}
6.
}
Output:
10
2)
Java Multi Line Comment
The multi line comment is used to
comment multiple lines of code.
Syntax:
1.
/*
2.
This
3.
is
4.
multi line
5.
comment
6.
*/
Example:
1.
public class CommentExample2 {
2.
public static void main(String[] args) {
3.
/* Let's declare and
4.
print variable in java. */
5.
int i=10;
6.
System.out.println(i);
7.
}
8.
}
Output:
10
0 comments