Type of Java Statements
- Control Statement
- Assignment Statement
Control Statements
- Conditional execution
- Looping
- Flow Control Statement
Type of Conditional Execution
- If Statement
- If – Else statement
- Switch Statement
If Statement
If statement encloses some code which is executed only if a condition
is true. If
statement takes boolean
expression as condition and nothing else.
Note*: Unlike other language Java doesn’t accept numbers as
conditional Operator. Only thing that can be accepted as condition is boolean expression which
returns TRUE / FALSE.
Syntax of If Statement
if (true)
System.out.println("Hello!
This will always get printed");
if (Boolean Expression) {
Code block
to get executed
}
If Else Statement
If Else
statement encloses some code which is
executed only if a condition is true else other part of code will get executed.
If statement takes boolean expression
as condition and nothing else.
Note*: Unlike other language Java doesn’t accept numbers as
conditional Operator. Only thing that can be accepted as condition is boolean expression which
returns TRUE / FALSE.E.g If(x=1) is not allowed while if(1==1) is accepted as it is boolean expression and will return true.
if (1==2)
System.out.println("Hello!
This will not get printed");
else
System.out.println("Hello!
This will get printed");
Nested ifs
A nested if is an
if statement that is the target of another if or else.
Here is an example:
if(i == 10) {
if(j < 20) a =
b;
if(k > 100) c =
d; // this if is
else a = c; //
associated with this else
}
else a = d; // this
else refers to if(i == 10)
As the comments
indicate, the final else is not associated with if(j<20), because it is not
in the same block
(even though it is the nearest if without an else). Rather, the final else
is associated with
if(i==10). The inner else refers to if(k>100), because it is the closest if
within the same
block.
Switch
The switch statement
is Java’s multiway branch statement. It provides an easy way to dispatch
execution to different parts of your code based on the value of an expression.
Here is the general
form of a switch statement:
switch (expression)
{
case value1:
// statement
sequence
break;
case value2:
// statement
sequence
break;
...
case valueN:
// statement
sequence
break;
default:
// default
statement sequence
}
The expression must
be of type byte, short, int, or char; each of the values specified in the case statements
must be of a type compatible with the expression. Each case value must be a
unique literal (that is, it must be a constant, not a variable). Duplicate case
values are not allowed.
Type of Looping Statement
while
The while loop is Java’s most
fundamental looping statement. It repeats a statement or block while its
controlling expression is true. Here is its general form:
while(condition) {
// body of loop
}
The condition can be any Boolean
expression. The body of the loop will be executed as long as the conditional
expression is true. When condition becomes false, control passes to the next
line of code immediately following the loop. The curly braces are unnecessary if
only a single statement is being repeated.
Here is a while loop that counts
down from 10, printing exactly ten lines of “tick”:
// Demonstrate the while loop.
class While {
public static void main(String
args[]) {
int n = 10;
while(n > 0) {
System.out.println("tick "
+ n);
n--;
}
}
}
When you run this program, it will
“tick” ten times:
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
do-while
The do-while loop always executes its
body at least once, because its conditional expression is at the bottom of the loop.
Its general form is
do {
// body of loop
} while (condition);
Each iteration of the do-while loop
first executes the body of the loop and then evaluates the conditional
expression. If this expression is true, the loop will repeat. Otherwise, the loop
terminates. As with all of Java’s loops, condition must be a Boolean expression.
Here is a reworked version of the “tick” program that demonstrates the do-while
loop. It generates the same output as before.
// Demonstrate the do-while loop.
class DoWhile {
public static void main(String
args[]) {
int n = 10;
do {
System.out.println("tick "
+ n);
n--;
} while(n > 0);
}
}
for
Here is the general form of the for
statement:
for(initialization; condition;
iteration) {
// body
}
If only one statement is being
repeated, there is no need for the curly braces. The for loop operates as
follows. When the loop first starts, the initialization portion of the loop is
executed. Generally, this is an expression that sets the value of the loop control
variable, which acts as a counter that controls the loop. It is important to
understand that the initialization expression is only executed once. Next,
condition is evaluated. This must be a Boolean expression. It usually tests the
loop control variable against a target value. If this expression is true, then
the body of the loop is executed. If it is false, the loop terminates. Next,
the iteration portion of the loop is executed. This is usually an expression
that increments or decrements the loop control variable. The loop then
iterates, first evaluating the conditional expression, then executing the body
of the loop, and then executing the iteration expression with each pass. This
process repeats until the
controlling expression is false.
// Demonstrate the for loop.
class ForTick {
public static void main(String
args[]) {
int n;
for(n=10; n>0; n--)
System.out.println("tick "
+ n);
}
}
Type of Flow Control Statement
- Return Statement
- Continue Statement
- Break Statement
return
The last control
statement is return. The return statement is used to explicitly
return from a method.
// Demonstrate
return.
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.");
}
}
The output from
this program is shown here:
Before the return.
Using continue
Sometimes it is
useful to force an early iteration of a loop.
// Demonstrate
continue.
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("");
}
}
}
This code uses the
% operator to check if i is even. If it is, the loop continues without
printing a newline.
Here is the output from this program:
0 1
2 3
4 5
6 7
8 9
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.
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.");
}
}
This program
generates the following output:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
Loop complete.
Using break as a Form of Goto
The general form of
the labeled break statement is shown here:
break label;
Here, label is the
name of a label that identifies a block of code.
// 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.");
}
}
}
Running this
program generates the following output:
Before the break.
This is after
second block.
No comments:
Post a Comment