You are on page 1of 24

CONTROL STATEMENTS IN JAVA

KIRAN KUMAR.V MCA B5 CLASS NO:16

JAVA CONTROL STATEMENTS


Java control statements are categorized into three Java Selection Statements Java Iteration Statements Java Jump Statements

Javas selection statements


Java supports two selection statements if and switch.

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 the switch


class SampleSwitch{ public static void main(String args[ ]){ for(i=0;i<2;i++) switch(i){ case 0: System.out.println( i is zero); break; case 1: System.out.println( i is one); break; default: System.out.println( i is greater than one); }}

Javas Iteration Statements


Javas iteration statements are for, while and do-while. while The while loop is javas most fundemental looping statement. It repeats a statement or block while its controlling expression is true. Its general form while(condition){ //body of loop }

//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); }}

Javas Jump statements


Java supports three jump statements break, continue and return. break In java, the break statements has three uses. It terminates a statement sequence in a switch statement. It can be used to exit a loop. It can be used as a form of goto.

Using break to exit a loop


When a break statement is encountererd inside a loop, the loop is terminated and program control resumes at the next statement following the loop.

//Using break to exit a loop


class BreakLoop{ public static void main(String args[ ]){ for(int i=0;i<100;i++) { if(i==10) break; //terminate loop if i is 10 system.out.println(i : +i); } System.out.println(Loop complete.); } }

Using break as a form of Goto


The break statement can also employed by itself to provide a civilized form of the goto statement. Java does not have a goto statement, because it provides a way to branch in an arbitrary and unstructured manner. By using this form of break, you can break out of one or more blocks of code. These blocks need not be part of a loop or a switch. The break statement is works with a label to specify where the execution will resume.

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.

// Using break as a civilized form of goto


class Break { Public static void main(String args[ ]) boolean t= true; first:{ second:{ third:{ System.out.println(Before the break.); If(t) break second;// break out of second block System.out.println(This wont execute); } System.out.println(This wont execute); } System.out.println(This is after second block.); }}

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); } }