Branching Statements
break; - The break statement terminates the loop.
continue; - when reached, it goes to the loop statement and evaluates the condition.
Ex. for (x=0; x<10;x++){
if (x==4){
continue;
}
System.out.println(x);
}
//this code prints from 0 to 3 and skips 4 and prints 5 to 9.
return;
Ex.
import java.lang.*;
public class AddNumbers{
public int add(int x,int y){
return x + y; //passes 1 and 2 then adds them and return the result
}
public static void main(String[] args){
boolean addnum = true;
AddNumbers num = new AddNumbers();
while (addnum){
System.out.println(num.add(1,2)); //passes 1 and 2 to add method and prints the result.
addnum = false;
}
}
}
to be continued...
No comments:
Post a Comment