break - Angelfire

Download Report

Transcript break - Angelfire

Chapter - 2
Control Statements
This chapter includes  Selection Statements
 if statement
 if-else-then statement
 switch statement
 Iteration Statements
 while statement




 Do-while statement
 for statement
Jump statements
Break statements
Continue statement
Return statement
Control Statements
 Java’s control statements are nearly identical to those in C/C++.
 However there are few differences - especially in the break and continue
statement
Selection Statements
 Java supports two selection statements: if and switch
 The general form of Java’s if statement is:
if (condition) statement1;
else statement2;
 Each statement may be a single statement or a compound statement enclosed
in curly braces (that is, a block).
 The else clause is optional.
 The if works like this : If the condition is true, then statement1 is executed.
Otherwise, statement2 (if exists) excutes.
Nested if Statements
 Nested ifs are very common in programming.
 An else statement always refers to the nearest if statement that is within
the same block.
 Here is an example:
if(i = = 10) {
if(j < 20) a = b;
if(k > 100) c=d;
else a = c;
// associated with the if(k > 100)
}
else a = d;
// this else refers to if(i = = 10)
The if-else-if Ladder
 Let us follow the example below to understand the if-else-if Ladder
class IfElse {
public static void main(String arg[ ]) {
int month = 4; // April
String season;
if(month = = 12 | | month = = 1 | | month = = 2)
season = “Winter”;
else if(month = = 3 | | month = = 4 | | month = = 5)
season = “Spring”;
else if(month = = 6 | | month = = 7 | | month = = 8)
season = “Summer”;
else if(month = = 9 | | month = = 10 | | month = = 11)
season = “Authum”;
else
season = “Bogus Month”);
System.out.println(“April is in the ” + season + “.”);
}
}
switch Statement
 The general form is :
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
case valueN:
// statement sequence
break;
default;
// default statement sequence
}
An Example of switch Statements
 Let us follow the example below to understand the use of switch
class SampleSwitch {
public static void main(String arg[ ]) {
for(int i = 0; i < 4; i++) {
switch(i) {
case 0;
System.out.println(“ i is zero”);
break;
case 1;
System.out.println(“ i is one”);
break;
case 2;
System.out.println(“ i is two”);
break;
default;
System.out.println(“ i is greater than 2”);
}
}
Structure of Nested switch Statements
switch(count) {
case 1 :
switch(target) {
case 0 :
System.out.println(“ target is zero”);
break;
case 1 :
System.out.println(“ target is one”);
break;
default;
System.out.println(“ i is greater than 2”);
}
break;
case 2 :
// … … …
}
}
Iteration Statements
 Java’s iteration statement are for, while, and do-while.
 These statements create what we commonly call loops.
while Statements
 It repeats a statement or block while its controlling expression is true.
 The general form of while statement is:
while (condition) {
// body of the loop
}
 The condition can be any Boolean expression. The body of the loop will be
executed as long as the conditional expression is true.
EXAMPLE: class WhileDemo {
public static void main(String arg[ ]) {
int n=10;
while (n>0) {
System.out.println(“n = ” + n) ;
n-}
}
}
do-while statement
 You might have noticed that if the conditional expression controlling a while
loop is initially false, then the body of the loop will not be executed at all.
 The do-while loops always executes its body atleast once, because its
conditional expression is at the bottom of the loop.
 The general for of do-while statements is as follows:
do {
// body of loop
} while (expression);
 If the expression is true the loop will repeated. Follow the example :
class DoWhileDemo {
public static void main(String arg[ ]) {
int n=10;
do {
System.out.println(“n = ” + n) ;
n-} while (n > 0) ;
}
}
for statement
 The general for of do-while statements is as follows:
for(initialization; condition; iteration) {
// body
}
 The for loop operates as follows. When the loop first starts, the initailization
portion of the loop is executed.
 This is an expression that sets the value of the loop control variable.
 Next condition is evaluated. This must be a Boolean expression.
 If the expression value is true, then the body of the loop is executed.
 Next, the iteration portion of the loop is executed.
 Follow the example :
class ForDemo {
public static void main(String arg[ ]) {
int n;
for (n=10; n>0; n - -) {
System.out.println(“n = ” + n) ;
}
}
}
Using the Comma in for statement
 To allow two or more or more variables to control a for loop, Java permits
you to include multiple statements in both the initialization and iteration
portion of the for.
 Let us follow the example:
int a, b;
for(a = 1, b = 4; a<b; a++, b - - ) {
System.out.println(“a = ”+ a);
System.out.println(“b = ”+ b);
}
Nested for Loops
int i, j;
for(i = 0; i<10; i++ ) {
for(j = 0; j<10; j++ ) System.out.print (“ . ”);
System.out.println( );
}
Variation in for statement
VARIATION 1:
 The condition controlling the for can be any Boolean Expression.
 Follow the example below:
boolean done = false;
for(int i = 1; !done; i++) {
// …..
if(interrupted( )) done = true;
}
VARIATION 2:
 Either the initialization or the iteration expression or both may be absent.
Int i=0;
boolean done = false;
for( ; !done; ) {
// …..
i++
if(i = = 10) done = true;
}
Jump statement
 Java supports three jump statements: break, continue and return.
 These statements transfer control to another part of your program.
Using Break
 In Java, the break statement has three uses.
 First as you have seen, it terminates a statement sequence in a switch
statement.
 Second, it can be used to exit a loop.
 Third, it can be used as a “civilized form of goto”.
USING BREAK TO EXIT A LOOP
for(int i=0; i < 100; i++) {
if(i = = 10) break; // terminates loop if i = 10
System.out.println(“i: =”+i);
}
System.out.println(“loop complete.”);
 break statement can be used with any of java’s loops, like while loop.
 When used inside a set of nested loops, the break statement will only break
out of the innermost loop.
Using Break As a form of GOTO
 Java defines an expanded form of the break statement.
 By using this form of break statement, you can break out of one or
more blocks of code. This form of block works with label.
class BreakInvalidDemo {
public static void main(String arg[ ]) {
one : for(int i = 0; i<3; i++) {
System.out.println(“Pass ” + i + “: ”);
}
for(int j = 0; j<3; j++) {
if ( j = = 10) break one // WRONG
System.out.print( j + “ ”);
}
}
}
Using Break As a form of GOTO (Continued)
 Keep in mind that you can not break to any label which is not defined for an
enclosing block.
 For example, the following program is invalid and will not compile.
class BreakDemo {
public static void main(String arg[ ]) {
boolean t = true;
first : {
second : {
third : {
System.out.println(“Before break”);
if(t) break second;
System.out.println(“This won’t execute”);
}
System.out.println(“This won’t execute”)
}
System.out.println(“This is after second block”)
}
}
}
Continue Statement
 In while and do-while loops a continue statement causes control to be
transferred directly to the conditional rxpression that controls the loop.
 In a for loop, control goes first to the iteration portion of the for
statement and then to the conditional expression.
 Follow the example below:
class ContinueDemo1 {
public static void main(String arg[ ]) {
for (int i=0; i<10;i++) {
System.out.print(i + “ ” );
if(i%2) continue;
}
System.out.println(“ ”);
}
}
Continue Statement (continued)
 As with the break statement, continue may specify a label to describe
which enclosing loop to continue.
class ContinueDemo {
public static void main(String arg[ ]) {
outer : for (int i=0; i<10;i++) {
for(int j=0;j<10;j++) {
if(j>1) {
System.out.println( );
continue outer;
}
System.out.println(“”+(i * j));
}
}
System.out.print( );
}
}
return Statement
 The return statement is used to explicitly return from a method.
class ReturnDemo {
public static void main(String arg[ ]) {
boolean t = true;
System.out.println(“before return statement );
if(t) return;
System.out.println(“This won’t execute”);
}
}