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:
Java Continue Statement Example
Example:
Output:
1 2 3 4 6 7 8 9 10 Java Continue Statement with Inner Loop
It continues inner loop only if you use continue statement inside the inner loop.
Example:
Output:
1 1 1 2 1 3 2 1 2 3 3 1 3 2 3 3 |
Tuesday, 28 June 2016
Continue Statement in Java
Break Statement in Java
Java Break 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:
- jump-statement;
- break;
Java Break Statement with Loop
Example:
- public class BreakExample {
- public static void main(String[] args) {
- for(int i=1;i<=10;i++){
- if(i==5){
- break;
- }
- System.out.println(i);
- }
- }
- }
Output:
1 2 3 4
Java Break Statement with Inner Loop
It breaks inner loop only if you use break statement inside the inner loop.
Example:
- public class BreakExample2 {
- public static void main(String[] args) {
- for(int i=1;i<=3;i++){
- for(int j=1;j<=3;j++){
- if(i==2&&j==2){
- break;
- }
- System.out.println(i+" "+j);
- }
- }
- }
- }
Output:
1 1 1 2 1 3 2 1 3 1 3 2 3 3
Java Break Statement with Switch
To understand the example of break with switch statement, please visit here: Java Switch Statement.
For-each loop (Advanced or Enhanced For loop):
For-each loop (Advanced or Enhanced For loop):
The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
Advantage of for-each loop:
- It makes the code more readable.
- It elimnates the possibility of programming errors.
Syntax of for-each loop:
- for(data_type variable : array | collection){}
Simple Example of for-each loop for traversing the array elements:
- class ForEachExample1{
- public static void main(String args[]){
- int arr[]={12,13,14,44};
- for(int i:arr){
- System.out.println(i);
- }
- }
- }
Output:12 13 14 44
Simple Example of for-each loop for traversing the collection elements:
- import java.util.*;
- class ForEachExample2{
- public static void main(String args[]){
- ArrayList<String> list=new ArrayList<String>();
- list.add("vimal");
- list.add("sonoo");
- list.add("ratan");
- for(String s:list){
- System.out.println(s);
- }
- }
- }
Output:vimal sonoo ratan
do-while Loop in Java
Java do-while Loop
The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use while loop.
It is executed at least once because condition is checked after loop body.
Like a while statement, except that it tests the condition at the end of the loop body |
Syntax:
- do{
- //code to be executed
- }while(condition);
Example:
- public class DoWhileExample {
- public static void main(String[] args) {
- int i=1;
- do{
- System.out.println(i);
- i++;
- }while(i<=10);
- }
- }
Output:
1 2 3 4 5 6 7 8 9 10
Java Infinitive do-while Loop
If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
- while(true){
- //code to be executed
- }
Example:
- public class DoWhileExample2 {
- public static void main(String[] args) {
- do{
- System.out.println("infinitive do while loop");
- }while(true);
- }
- }
Output:
infinitive do while loop infinitive do while loop infinitive do while loop ctrl+c
Now, you need to press ctrl+c to exit from the program.
Subscribe to:
Posts (Atom)