The control statement are used to controll the flow of execution of the program
.
This execution order
depends on the supplied data values and the conditional logic.
Java contains the
following types of control statements. .
1-
Selection 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 Java
supports two selection
statements, "if and switch".
a: If
Statement:
It can be used to route program execution through two different paths.
This is a control statement to execute a single statement or a block of code,
when the given condition is true and if it is false then it skips
if block and rest code of program is executed .
Syntax:
|
if(condition) { <statement1>; } else { <statement2>; } |
If the condition is true, then statement1 is executed. Otherwise, statement2
(if it exists) is executed.
Example:
|
if(a>b) { System.out.println("a is greater than b");// prints a is greater than b } else { System.out.println("b is greater than a");//prints b is greater than a } |
if-else-if Ladder
When there are more than
two conditions then a common programming construct that is based upon a
sequence of nested ifs called if-else-if ladder is used. It looks like this:
|
if(condition) <statement>; else if(condition) <statement>; else if(condition) <statement>; ... else <statement>; |
b.Switch
statement:
Switch statement provides a better alternative than a large series of
if-else-if statements.This is an easier implementation to the if-else
statements. The keyword "switch" is followed by an expression that
should evaluates to byte, short, char or int primitive data types ,only.
General Syntax:
|
switch (x) { case value0: doSomething0(); break; case value1: doSomething1(); break; case value2: doSomething2(); break; case value3: doSomething3(); break; case value4: doSomething4(); break; .... .... case valueN: doSomethingN(); break; default: doSomethingElse(); } |
The switch statement works
like this: The value of the expression is compared with each of the literal
values in the case statements. If a match is found, the code sequence following
that case statement is executed. If none of the constants matches the value of
the expression, then the default statement is executed. However, the default
statement is optional. If no case matches and no default is present, then no
further action is taken. The break statement is used inside the switch to
terminate a statement sequence. When a break statement is encountered,
execution branches to the first line of code that follows the entire switch
statement. This has the effect of “jumping out” of the switch.
2-
Looping Statements:
Looping statements or Iteration statements enable program execution to repeat
one or more statements (that is,iteration statements form loops).
a: for loop:
|
for(initialization; condition;
iteration) { // body } |
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.
b:
do-while
|
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
c:
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.
3- Branching
Statements:
Branching statements allow your program to execute in a nonlinear fashion. Break,
jump and goto are the branching statements used in java.
No comments:
Post a Comment