Sunday, January 24, 2010

Break and Continue in BlueJ


Both break and continue are jumping statements, break statement can be used inside any loop body or inside each case body of a switch statement. When break is used inside a loop  body and the control encounters it, the break statement sends the control out of the loop body.

Program  of break in BlueJ

for(i=0;i< 10;i++)
{
if(i==5)
break;
}
here the control will leave the loop body when the value of 'i' becomes 5.break statement within a loop body must be kept within the body of a decision making statement.

Another use of break statement is within case body of switch statement. When a case body is executed, break statement sends the control out of switch body, skipping all the other case bodies. If we don’t use break with case body, compiler will not show any error. It allows the control to enter the other case bodies without ant checking, following the case body which was entered first.

Program on break in switch

int  day=3;
switch(day)
{
case 1:
System.out.println(“Day is Monday…”);
break;
case 2:
System.out.println(“Day is Tuesday…”);
break;
case 3:
System.out.println(“Day is Wednesday…”);
break;
case 4:
System.out.println(“Day is Thursday…”);
break;
case 5:
System.out.println(“Day is Friday…”);
break;
case 6:
System.out.println(“Day is Saturday…”);
break
case 7:
System.out.println(“Day is Sunday…”);
break;
}
  
Here in the above program, switch would find a match in case 3, it will execute the body statement of case 3 and the break statement at the end of the case body will send the control out of loop body. ( case body starts with the case statement and ends with the break statement. In the above program output will be Day is Wednesday…

BlueJ Program on switch without break.

int var=0;
switch(var)
{
case 0:
var++;
case 1:
var++;
case 3:
var++;
}
System. out.println(var);

The output would be 3 as after executing the first case body where match is found, the control will execute both case 2 and case 3 as there is no break statement.

Continue statement is used only in loops. It starts the pre matured iteration of a loop means the loop body executes skipping statement(s) in the loop body. Like break statement the continue statement is to be placed within decision making statements body and when this continue statement is encountered, the control skips the remaining loop body statements and gets ready for another execution. In case of for loop, the control goes to the reinitialization statement and then the conditional statement. In while or do-while loop control directly goes to the conditional statement.

 Next is a program on for loop.If the entered value is less than 0, then the value will not be displayed.

import java.io.*;.
class A
{
private   int x, i=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void take () throws IoException
{

for (i=0;i< 10;i++)
{
System. Out.println("Enter a value:-");
x=Integer.parseInt(br.readLine()); 
if (x< 0)
continue;
System. Out.println("The value is: , +x);
}
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner