In java goto keyword is not used. In place of goto we used break in java.
Why we using break in place of goto.
goto provides a way to branch in an arbitrary and unstructured manner. This usually makes goto ridden code hard to understand and hard to maintain. It also prohibits certain compiler optimization so break is used in place of break.
Syntax:-
break label;
label can be any variable but not java keyword. label is followed by colon.
Execution:-
Label break statement is used to transfer control unconditionally from one place to another place in program.
To use break as goto statement, we have to name a block, block are set of java statement. To name a block, put label at start of it.
Example:-
go:{
System.out.println("Hey");
.
.
block
.
.
}
Here go is label.
Once you labeled a block, you can use this label as the target of break statement.
When compiler see break statement with label than compiler transfer execution to at end of labeled block.
Label break statement is used exit from nested loops.
Example:-
Program to find sum of N number
import java.util.Scanner;
class group{
public static void main(String arng[]){
int num, i,sum=0;
go:{
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<100;i++)
{
sum=sum+i;
if(i==num)
break go;
}
}
System.out.println("Sum of odd number:"+sum);
}
}
Here go is label of break statement.
Following block is go labeled block.
go:{
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<100;i++)
{
sum=sum+i;
if(i==num)
break go;
}
}
Note:-
# Declared variable inside labeled block are visible only inside labeled block.
Previous Code:-
Continue statement in java
List Code:-
Java codes
Next Code:-
Return
Java Programs:-
List of Java Programs
Why we using break in place of goto.
goto provides a way to branch in an arbitrary and unstructured manner. This usually makes goto ridden code hard to understand and hard to maintain. It also prohibits certain compiler optimization so break is used in place of break.
Syntax:-
break label;
label can be any variable but not java keyword. label is followed by colon.
Execution:-
Label break statement is used to transfer control unconditionally from one place to another place in program.
To use break as goto statement, we have to name a block, block are set of java statement. To name a block, put label at start of it.
Example:-
go:{
System.out.println("Hey");
.
.
block
.
.
}
Here go is label.
Once you labeled a block, you can use this label as the target of break statement.
When compiler see break statement with label than compiler transfer execution to at end of labeled block.
Label break statement is used exit from nested loops.
Example:-
Program to find sum of N number
import java.util.Scanner;
class group{
public static void main(String arng[]){
int num, i,sum=0;
go:{
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<100;i++)
{
sum=sum+i;
if(i==num)
break go;
}
}
System.out.println("Sum of odd number:"+sum);
}
}
Here go is label of break statement.
Following block is go labeled block.
go:{
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num=data.nextInt();
for(i=0;i<100;i++)
{
sum=sum+i;
if(i==num)
break go;
}
}
Note:-
# Declared variable inside labeled block are visible only inside labeled block.
Previous Code:-
Continue statement in java
List Code:-
Java codes
Next Code:-
Return
Java Programs:-
List of Java Programs
The class name must be starting with Upper case letter !
ReplyDeletei dont think that is necessary
DeleteNice Explanation....
ReplyDeleteYes no need of upper case letters ,we can use either lower case letters also.
ReplyDelete