Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Saturday, 7 January 2017

Break, Switch, Continue keword

 

Break Keyword 

Break,Switch,Continue Keyword

Break keyword in java programming is used to come out of the loop control statement. The break keyword is used to stops execution of for loop, while loop, and switch-case statement. It breaks the current flow of the program at specified condition. In case of inner loop , it breaks only inner loop.

Syntax:

break;

The following example shows break statement inside a for loop

class BreakStatement1
{
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

The following example shows break statement inside a Inner loop

class BreakStatement2
{
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
              12
              13
              21
              32
              33

The following example shows break statement inside a while loop

class BreakStatement3
{
public static void main(String args[])
{
int number = 0;
while(true)
{
number++;
System.out.println(number);
if(number >= 10)
{
break;
}
}
}

output: It will print 1 to 10 numbers.


Before using break keyword in switch statement , we learn what is switch statement in java.


Switch Statement with Java

The java switch statement executes one statement from multiple condition. A switch statement work with byte, short, int , char primitive data types and it is also work with String and enumerated types.

Syntax:

switch(expression)
{
case value1:
//Statements;
break; //optional

case value2:
//Statements;
break; //optional

case value3:
//Statements;
break; //optional

default:
//Statement will execute if all cases are not matched.
}


There are some rules to work with Switch Statement

  • Variables use in switch statement can only be int, byte, char, short, String, enums.
  • You can use any number of case statements within switch but values for a case must be same as the variable in switch.

Flow of switch statement

  • Expression values is compared with each case value. If it is matches , statements following case would be executed.
  • Break statement is used to terminate the execution of statement.
  • If none of these case matches, statement following default would be executed.
  • If break statement is not used within case, all cases following matching cases would be executed.

For example:

class BreakWithSwitch
{
public static void main(String args[])
{
int i = 100;
switch(i)
{
case 10:
System.out.println("10");
break;
case 20:
System.out.println("20");
break;
case 100:
System.out.println("100");
break;
default:
System.out.println("100 number are not matched with cases");
}
}
}

output: 100


Continue Keyword

Continue statement is used to skip a particular iteration or specified condition of the loop.

Syntax:

continue;

For example:

class ContinueExample
{
public static void main(String []args)
{
for(int i = 1; i<=10; i++)
{
if(i == 5)//5 will not print
{
continue;
}
System.out.println(i);
}
}
}

output:1
             2
             3
             4
             6
             7
             8
             9
            10

There are all number except 5.

If you want make your logic strong then you will have to good practice of break, switch and continue keyword because these keyword are mostly used.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate