Escolar Documentos
Profissional Documentos
Cultura Documentos
If
The simplest form of if is if (condition) statement;
Eg:
Class IfSample{ public static void main(String args[]) { int x,y; x=10; y=20; if(x<y) System.out.println(x is less than y); } }
elseif
The form of else if is if (condition) statement; else if(condition) statement; else if(condition) statement; . else statement;
Eg: Class IFSample{ public static void main(String args[]) { int x,y; x=10; y=20; if(x>y) System.out.println(x is greater than y); else if(x==y) System.out.println(x is equal to y); else System.out.println(y is greater than x); } }
switch
The switch statement is Javas multiway branch statement. The general form of a switch statement : switch (expression){ case value1: //statement sequence break; case value2: //statement sequence break; . default: //default statement sequence }
//A simple example of while loop class While{ public static void main(String args[ ]){ int n=0; while(n<10){ System.out.println(tick+n) n++; } } }
do-while
If the conditional expression controlling a while loop is initially false, then the body of a while loop will not be executed at all. Sometimes it is desirable to execute the body of the while loop at least once. The do-while loop always executes its body atleast once, because the conditional expression is at the bottom of the loop. Its general form is do{ //body of loop } while(condition);
//A simple example of do-while loop class DoWhile{ public static void main(String args[ ]){ int n=0; do { System.out.println(tick +n); n++; } while(n<10); } }
for The general form of the for statement for(intialization;condition;iteration){ //body } eg: class ForTick{ public static void main(String args[ ]){ int n; for (n=0;n<10;n++) System,out,println(tick +n); }}
The general form of the labeled break statement is break label; label is the name of a label that identifies a block of code. To name a block, put a label at the start of it. A label is any valid java identifier followed by a colon.
continue
Continue statement is useful to force an early iteration of a loop. The continue statement performs continue running the loop, but stop processing the remainder of the code in its body of the loop, to the loops end.
Eg:
class Continue{ public static void main(String args[ ]){ for(int i=0;i<10;i++){ System.out.print( i + ); if(i%2==0) continue; System.out.println(); } } }
return
The return statement is used to explicitly return from a method. In a method the return statement can be used to cause execution to branch back to the caller of the method. Thus, the return statement immediately terminates the method in which it is executed
Eg:
class Return{ Public static void main(String args[ ]){ boolean t=true; System.out.println(Before the return); If(t) return;//return to caller System.out.println(This wont execute); } }