Top Banner
CONTROL STATEMENTS IN JAVA KIRAN KUMAR.V MCA B5 CLASS NO:16
24

CONTROL STATEMENTS IN JAVA

Nov 13, 2014

Download

Documents

vsunny_488
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CONTROL STATEMENTS IN JAVA

CONTROL STATEMENTS IN JAVA

KIRAN KUMAR.V

MCA B5

CLASS NO:16

Page 2: CONTROL STATEMENTS IN JAVA

JAVA CONTROL STATEMENTS

Java control statements are categorized

into threeJava Selection StatementsJava Iteration StatementsJava Jump Statements

Page 3: CONTROL STATEMENTS IN JAVA

Java’s selection statements

Java supports two selection statements if and switch.

If The simplest form of ‘if’ is if (condition) statement;

Page 4: CONTROL STATEMENTS IN JAVA

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

Page 5: CONTROL STATEMENTS IN JAVA

elseif The form of ‘else if ‘ is if (condition) statement; else if(condition) statement; else if(condition) statement; ……………………. else statement;

Page 6: CONTROL STATEMENTS IN JAVA

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

}

}

Page 7: CONTROL STATEMENTS IN JAVA

switch The switch statement is Java’s 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}

Page 8: CONTROL STATEMENTS IN JAVA

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

}}

Page 9: CONTROL STATEMENTS IN JAVA

Java’s Iteration Statements

Java’s iteration statements are for, while and do-while.

whilewhile The while loop is java’s most fundemental

looping statement. It repeats a statement or block while its controlling expression is true. Its general form

while(condition){ //body of loop}

Page 10: CONTROL STATEMENTS IN JAVA

//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++;

}

}

}

Page 11: CONTROL STATEMENTS IN JAVA

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

Page 12: CONTROL STATEMENTS IN JAVA

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

Page 13: CONTROL STATEMENTS IN JAVA

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

Page 14: CONTROL STATEMENTS IN JAVA

Java’s 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.

Page 15: CONTROL STATEMENTS IN JAVA

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.

Page 16: CONTROL STATEMENTS IN JAVA

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

Page 17: CONTROL STATEMENTS IN JAVA

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.

Page 18: CONTROL STATEMENTS IN JAVA

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.

Page 19: CONTROL STATEMENTS IN JAVA

// 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 won’t execute”);}System.out.println(“This won’t execute”);}System.out.println(“This is after second block.”);}}

Page 20: CONTROL STATEMENTS IN JAVA

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 loop’s end.

Page 21: CONTROL STATEMENTS IN JAVA

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(“”);}}}

Page 22: CONTROL STATEMENTS IN JAVA

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

Page 23: CONTROL STATEMENTS IN JAVA

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 won’t execute”);

}

}

Page 24: CONTROL STATEMENTS IN JAVA