BRANCHING STATEMENT(Break, Continue)

These statements transfer the control to a different place in the program.we will see the operation of each of these. We have already seen the use of break statement in switch case.

syntax for break statement:
for(intitialization;condition;increment/decrement)
{
-
-
break;
-
-
-
}

1.Example of break statement

import java.util.*;
class Test{
public static void main(String args[])
{
int [] numbers={10,20,30,40,50};
for(int x: numbers)
{
if(x==30)
{
break;
}
System.out.println(x);
}
}
}
OUTPUT:
10
syntax for Continue statement
for(intitialization;condition;increment/decrement)
{
-
-
continue;
-
-
}

1.Displaying Continue example

import java.util.*;
class Test{
public static void main(String args[])
{
int [] numbers={10,20,30,40,50};
for(int x: numbers)
{
if(x==30)
{
continue;
}
System.out.print(x);
System.out.print("\n");
}
}
}
OUTPUT:
10
20
Translate »