Java Control Statements PPT
Java Control Statements PPT
}
SEQUENCE
The sequence means the statements are being executed sequentially.
This represents default flow of statement.
Statement 1
Statement 2
Statement 3
SELECTION
THE SEQUENCE STATEMENTS MEANS THE EXECUTION
OF STATEMENT(S) DEPENDING UPON A GIVEN
CONDITION.
Body of iF Body
Statement
of else
1
Statement 1
Statement 1 Statement 1
Statement 2
Statement 2 Statement 2
Conditional Statement - if Statement
First Form of if Statement
if ( <conditional expression> ) {
execute statements -for-the-true-case;
}
=>Second form
if ( < conditional expression > ) {
execute statement1 -for-the-true-case
}
else {
execute statement2 -for-the-false-case
}
Result of conditional expression : Logical Type(true or false)
There are certain points worth remembering
about the if statement as outlined below:
The conditional expression is always
enclosed in parenthesis.
The conditional expression may be a simple
expression or a compound expression.
Each statement block may have a single or
multiple statements to be executed.
In case there is a single statement to be
executed then it is not mandatory to
enclose it in curly braces ({}) but if there
are multiple statements then they must be
enclosed in curly braces ({})
The else clause is optional and needs to be
included only when some action is to be
taken if the test condition evaluates to
false.
Example
if ( j<5 ) { // This is recommended
System.out.println(“j is less than 5”):
}
Else
{
System.out.println(“j is no less than 5”):
}
OR
if ( j<5 )
System.out.println(“j is less than 5”): //single statement
Syntax for the if Statement
if ( <boolean expression> )
<then block>
else
<else block>
if ( testScore < 70 )
Then
ThenBlock
Block JOptionPane.showMessageDialog(null,
"You did not pass" );
else
Else
ElseBlock
Block JOptionPane.showMessageDialog(null,
"You did pass " );
Indentation is important!
Nested if statement
if (<cond. expr.>)
if (<cond. expr.>)
// . . .
<statement>
for Loops
while Loops
do while Loop
break and continue
Looping / Repetitive Statement
Some time some portion of the program (one or more
statement) needs to be executed repeatedly for fixed no. of
time or till a particular condition is being satisfied. This
repetitive operation is done through a looping statement.
Definition
false Condition
?
True
Statement 1
Statement 2
Statement n
In an Entry-Controlled loop/Top-Tested/Pre-Tested
loop the test expression is evaluated before entering
into a loop.
for Examples: For loop and While loop.
In an Exit-Controlled loop/Bottom-Tested/Post-
Tested loop the test expression is evaluated before
exiting from the loop.
for Examples: do-While loop.
for Loops
for (initialization; Test-condition; update-statement)
{
//loop body;
}
Example:
int i;
for (i = 0; i<100; i++)
{
System.out.println("Welcome to Java! ” + i);
}
In for loop contains three parts separated by semicolons(;)
Initialization Before entering in a loop, its variables must be
initialized. The initialization expression is executed only once in
the beginning of the loop.
Test Expression It decides whether the loop-body will be
executed or not. if the test expression evaluates to true the loop-
body gets executed, otherwise the loop is terminated. Test
expression gets checked every time before entering in the body
of the loop.
Update Expression(s) The update expressions change the
values of loop variables. The update expression is executed every
time after executing the loop-body .
The body of the loop The statements that are executed
repeatedly (as long as the test-expression is nonzero) from the
body of the loop.
for Loop Flow Chart
for
// print 1 2 3 4 5 6 7 8 9 10
public class ClassXI
{
public static void main(String args[])
{
int n;
for(n=1; n<=10; n++)
{
System.out.println(“ ” + n);
}// end for loop
}// end main
} // end class
INFINITE LOOP: An infinite loop can be created by omitting the
test expression as shown below:
1. for( int j=25; ; --j)
{
System.out.println(“ An infinite for Loop”);
}
2. for ( ; ; ) // infinite loop
{
System.out.println(“ An infinite for Loop”);
}
3.Empty Loop: If a loop does not contain
any statement in its loop-body, it is said to
be an empty loop.
For(int j=20;j>=0;--j);
4. Declaration of variable inside the loops
and if: A variable declared within an if or
for/while/do-while statement cannot be
accessed after the statement is over,
because its scope becomes the body of the
statement(if/for/while/ do-while). It
cannot be accessed outside the statement
body.
while Loops
while (condition)
{
// loop-body;
}
Note: A while loop is pre-test loop. It first tests a specified
conditional expression and as long as the conditional
expression is evaluated to non zero (true), action taken
(i.e. statements or block after the loop is executed).
while Loop Flow Chart
START
while
// Demonstrate the while loop.
public class While {
public static void main(String args[]) {
int n = 1;
while(n<=1 0) {
System.out.println(“ " + n);
n++;
} //end while
} // end main
} // end class
do Loops
do
{
// Loop body;
} while (continue-condition);