Explaining Expressions and Flow control in Java Technology.
Size: 269.88 KB
Language: en
Added: Aug 05, 2024
Slides: 22 pages
Slide Content
●This module is intend to develop basic skills in
applying conditions to the applications and working
with iterative statements in the application.
Module Overview
2
Applyingconditions where ever required in the
applications.
Recognize Java technology keywords to work with
conditional applications.
Identify different iterative statements in Java .
Identify Different keywords available in java to work
with Iterations.
Working with applications that combine both
conditions and iterations.
Working with conditional operator.
Creating applications containing conditions and
iterations.
Objectives
3
Decision Making: In Java, decision making is done by JRE
depending on the value of the expressions at run time so that
control can be moved from one block to another block.
To facilitate this ,Java has provided following keywords.
if statement
switch statement
Conditonal operator statement
Decision Making And Branching
4
Decision with If
This statement is used to control the flow of execution
of statements.
It is basically two way decision statement and used in
combination with an expression.
This statement allows the jre to evaluate the expression
first , if block is executed only when condition or
expression is evaluated to true.
The if statement may be implemented in different
forms depending on the complexities of condition to be
tested.
Simple if statement
If.......Else Statement
Nesting Of If.....Else Statements
The Else If Ladder
Simple If Statement
if(test expression) {
statement-block;
}
statement-x;
Example:
int a =15
If(a>10)
System.out.println(“If block is evaluated”);
statement-block is executed. Even if condition is
false or true.
Simple If_Else Statement
Syntax:
if(test expression) {
True-block statements(s) } else {
false-block statement(s) }
statement-x
Example:
int a =15
If(a>10)
System.out.println(“If block is evaluated”);
else
System.out.println(“else block is evaluated”);
Nesting of If…Else Statements
To test multiple decisions , we use more than one
if..else statement in nested form as follows:
Syntax:
if(test condition1) {
if(test condition2) {statement-1; }
else { statement-2;
}//end of inner else }//end of if
else { statement-3;
}//end of else
statement-x; }
Else If Ladder
There is another way of putting ifs together when
multipath decision are involved.
Syntax :
if (condition1)
statement-1
else if(condition 2)
statement-3;
else if(condition n)
statement-n;
else
default-statement; }
Switch Statement
The switch statement test the value of given
variable (or expression) against a list of case
values and when match is found , a block of
statements associated with that case is executed.
Syntax:
switch(expression) {
case value-1: block-1
break;
case value-2: block-2
break; ......... .........
default: default-block break; }
statement-x;
In that expression is an integer expression
/character/String(java 7). value-1, value-2 ...are
constants or constant’s expression and in that case
labels values should be unique within switch
statement.
block-1,block-2...are statement list .There is no need
to put braces around these blocks but the important
one is case labels end with colon(:).
The break statement causes an exit from the switch
statement.
The default is an optional case,If the condition is not
match with any case label then default case will be
executed.
Switch Statement(Cont…)
The Conditional (?:) Operator
This operator is used for taking two-way decision.
This opertaor is a combination of ? and : and takes
three operands.
Syntax:
(conditional expression)? expression1: expression2
The conditional expression is evaluated first.
If the result is true , expression1 is evaluted and is
returned as the value of the conditional
expression.
Otherwise expression2 is evaluted and its value is
returned.
Looping /Iteration
Iteration:Iteration is the process of executing
statements continuously in the block until a
condition is met.
A looping process, include the four steps
setting and initilization of counter
Execution of the statement in the loop
Test a specified condition
Incrementing the operator
Types of Loops
The Java language provides three constructs for
performing loop operations.
The while statement
The do statement
The for statement
While
The while is an entry-controlled loop statement .
This is simplest looping construct in all looping
statement.
The syntax of while statement
initialization;
while(test Condition)
{Body of thr loop}
Working of While Loop
“test condition” is evaluted first .
If the condition is true then the body of the loop
is executed.
This process continues until the test condition
finally becomes false .
Once condition is false the control is transferred
out of the loop.
On exit ,the program continues with the
statement immediately after the body of the loop.
The Do-while Statement
In that body of the loop is first executed.
After the test condition is checked.
It is also called as Exit-controlled loop.
Syntax
initilization;
do{
Body of the loop
}while(test condition);
Working of do-while
In this body of the loop is execute first.
At the end of the loop , the test condition in while
statement is executed.
This process continues until condition becomes
false.
The loop will be terminated once the condition is
false
Example:
do{
sum=sum+n*n;
n=n+1;
}while(n<=5)
For Loop
The for loop is another entry-controlled loop.
Syntax:
for(initialization; test condition; increment){
//Body of the loop
}
Working of For Loop
First, initilization of the control variable is done,
using the assingment statements such as i=1 and
count=0;The variable i and count are known as
loop-control variables.
Value of control variable is tested using the test
condition. If the condition is true, the body of the
loop is executed and the control variable is
incremented.
Otherwise the loop will be terminated and the
execution continues with the statement that
immediately follows the loop.
Second step is repeated until the test condition
fails.
For Loop
Example:
Int I;
for(i=0;i<=9;i=i+1)
{
System.out.println(i);
}