PAGE-4

CHAPTER-1(Introduction to Java)                                   BOTTOM

 

Control Statements

A programming language uses control statements to cause the flow of execution of the state of a program. Java’s program control statements can be put into the following categories:
  • selection statements.
  • iteration statements.
  • jump statements.


Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable.

Iteration statements enable program execution to repeat one or more statements (that is, iterating statements using loops).

Jump statements allow your program to execute in a nonlinear fashion ie. Jumping from from one place to another .


Selection statements

Java provides selection statements that allow the program to choose between alternative actions during execution. The choice is based on criteria specified in the selection statement. These selection statements are:-

1. simple if Statement
2. if-else Statement
3. switch Statement

Simple if Statement
The simple if statement has the following syntax::-

if (<conditional expression>)

{

<statement>

}

It is used to decide whether an action is to be performed or not, based on a condition. The condition is specified by <conditional expression> and the action to be performed is specified by <statement>.

Example:- .

boolean dataAvailable;

if (dataAvailable){

ProcessData();
}

 


if-else Statement

The if-else statement has the following syntax:

if (<conditional expression>)

<statement1>

else

<statement2>

It is used to decide between two actions, based on a condition. The <conditional expression> is evaluated first. If its value is true, then <statement1> (the if block) is executed and execution continues with the rest of the program. If the value is false, then <statement2> (the else block) is executed and execution continues with the rest of the program. In other words, one of two mutually exclusive actions is performed. The else clause is optional; if omitted, the construct reduces to the simple if statement.


 Example:- .

boolean dataAvailable;

if (dataAvailable){

ProcessData();
}

else

{

waitForMoreData();

}

switch Statement

Conceptually the switch statement can be used to choose one among many alternative actions, based on the value of an expression. Its general form is as follows:

Syntax :-

switch (<non-long integral expression>) {
case label1: <statement1>
case label2: <statement2>
...
case labenl: <statementn>
default: <statement>
} // end switch

The syntax of the switch statement comprises a switch expression followed by the switch body, which is a block of statements. The type of the switch expression is non-long integral (i.e., char, byte, short, or int ). The statements in the switch body can be labeled, defining entry
points in the switch body where control can be transferred depending on the value of the switch expression. The semantics of the switch statement are as follows:

1.The switch expression is evaluated first.

2.The value of the switch expression is compared with the case labels. Control is transferred to the <statement> associated with the case label that is equal to the value of the switch expression.

3.After execution of the associated statement, control falls through to the next statement unless appropriate action is taken. If no case label is equal to the value of the switch expression, the statement associated with the default label is executed.

 

Example:- .


class SampleSwitch {


public static void main(String args[]) {

for(int i=0; i<6; 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;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
}

 

Output is:-

i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.

 


Iteration Statements:-

 

Java provides three language constructs for constructing loops:
1.while statement
2.do-while statement
3.for statement

 

while Statement
The syntax of the while loop is

while (<loop condition>)
{
<loop body>
}


The loop condition is evaluated before executing the loop body. The while statement executes the loop body as long as the loop condition is true. When the loop condition becomes false, the loop is terminated and execution continues with the statement immediately following the loop. If the loop condition is false to begin with, the loop body is not executed at all. In other words, a while loop can execute zero or more times. The loop condition must be a boolean expression.

Example:- .


class While {
public static void main(String args[]) {
int n = 10;
while(n > 0) {
System.out.println("Count " + n);
n--;
}
}
}

Output is:-

Count 10
Count 9
Count 8
Count 7
Count 6
Count 5
Count 4
Count 3
Count 2
Count 1


do-while Statement

The syntax of the do-while loop is

do { <loop body> }

while ( (<loop condition>);

The loop condition is evaluated after executing the loop body. The do-while statement executes the loop body until the loop condition becomes false. When the loop condition becomes false, the loop is terminated and execution continues with the statement immediately following the loop. Note that the loop body is executed at least once.The program written using the while statement can be rewritten using the dowhile statement as follows:-

Example:- .



class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("Count " + n);
n--;
} while(n > 0);
}
}


for-loop statement

The for loop is the most general of all the loops. It is mostly used for counter-controlled loops, that is, when the number of iterations is known beforehand.
The syntax of the loop is as follows:

for (<initialization>; <loop condition>; <increment expression>)
{
<loop body>
}

The <initialization> usually declares and initializes a loop variable that controls the execution of the <loop body>. The <loop condition> is a boolean expression, usually involving the loop variable, such that if the loop condition is true, the loop body is executed; otherwise, execution continues with the statement following the for loop. After each iteration (i.e., execution of the loop body), the <increment expression> is executed. This usually modifies the value of the loop variable to ensure eventual loop termination. The loop condition is then tested to determine if the loop body should be executed again. Note that the <initialization> is only executed once on entry to the loop.

 

Example:- .

class ForCount {
public static void main(String args[]) {
int n;
for(n=10; n>0; n--)
System.out.println("Count " + n);
}
}


Using continue

Sometimes it is useful to force an early iteration of a loop. That is, you might want tocontinue running the loop, but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop’s end. The continue statement performs such an action. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression 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. Here is an example program that uses continue to cause two numbers to be printed on each line:

Example:- .

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

Output:-

0 1
2 3
4 5
6 7
8 9


return

The last control statement is return. The return statement is used to explicitly return
from a method.

.
Example:- .

class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t)
return;
System.out.println("This won't execute.");
}
}

Output:-Before the return



Note :
After Successful completion of Training Candidate will be provided with Project Report and Training Certificate.

Home  |   FeedBack  |   Terms of Use  |   Contact Us  |   Report Error
                                                                            Copyright © 2009 R.M Infotech (P) Ltd.                                             Designed by: Raman