Java Control Structure
Java Control Structure
Java Statements
Java Control Structures
Decision Control Structures
Repetition Control Structures
Branching Statements
Java Statements
if (expression)
{
statement1;
statement2;
…
}
Decision Control Structures
NO Boolean YES
expression
Decision Control Structures
If Structure: Example 1
• Suppose that the passing grade on an
examination is 75 (out of 100). Then the if
statement may be written in Java as:
If Structure: Example 2
int grade = 68;
if (grade > 60)
{
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”);
}
Decision Control Structures
If-Else Structure
• The if-else statement is used when you want to
execute one statement if a condition is true, and
another statement if the condition is false.
• The general format of an if-else statement is:
if (expression)
statement1;
else
statement2;
or
if (expression) statement1;
else statement2;
Decision Control Structures
• As in the if statement, compound statements may
also be used in if-else statements (provided they are
grouped together by braces).
if (expression)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
Decision Control Structures
• Making a decision involves choosing between two
alternative courses of action based on some value within
a program.
NO Boolean YES
expression
Decision Control Structures
if (expression 1) {
statement1;
}
else if (expression 2) {
statement2;
}
else {
statement 3;
}
Decision Control Structures
• Nesting If and If-Else
Structure
NO Boolean YES
expression
NO Boolean YES
expression
Decision Control Structures
Nesting If and If-Else Structure: Example
int grade = 68;
if(grade > 90) {
System.out.println(“Very Good”);
}
else if(grade > 60) {
System.out.println(“Good”);
}
else {
System.out.println(“Failed”);
}
Decision Control Structures
Switch Structure
• The switch statement enables a program to
switch between different outcomes based
on a given expression.
• The general format of a switch statement is:
Decision Control Structures
switch (switch_expression)
{
case 1:
statement1;
case 2:
statement2;
case n:
statementn;
default:
..
}
Repetition Control Structures
while (boolean_expression)
{
statement1;
statement2
}
Repetition Control Structures
• While Structure: Examples
int counter = 1;
while (counter <= 10) {
System.out.println("counter = " +
counter);
counter += 1;
}
int counter = 1;
while (counter*counter < 1000) {
System.out.println(counter *
counter);
counter++;
}
Repetition Control Structures
Do-While Structure
• The do-while loop is similar to the while-loop.
The statements inside a do-while loop are
executed several times as long as the condition is
satisfied.
• The general format of a do-while statement is:
do
{
statement1;
statement2
…
} while (boolean_expression);
Repetition Control Structures
• Do-While Structure: Examples
int x = 0;
do {
System.out.println(“x = ” + x);
x++;
} while(x < 10);
do {
System.out.println(“Hello!”);
} while (false);
Repetition Control Structures
For Structure
• The for statement instructs a program to perform
a block of code a specified number of times.
• The general format of a for statement is:
for (initialization; condition;
increment)
{
statement 1;
statement 2;
}
Repetition Control Structures
• For Structure: Examples
int x;
for(x=1; x<=10; x++)
{
System.out.println(“x= ” + x);
}
int i;
for(i=0; i<10; i++)
{
System.out.println(i);
}
Branching Statements
• These statements redirect the flow
of program execution.
• These include:
• break
• continue
• return
Branching Statements
Break Statement
• The break statement causes an exit
from an enclosing loop.
• In other words, encountering the
break statement causes immediate
termination of the loop.
• Program control picks up at the
code following the loop.
Branching Statements
Continue Statement
• The continue statement works in a
somewhat similar way to the break
statement.
• But instead of forcing loop termination,
continue forces the next iteration for the
loop to take place, skipping any code in
between.
Repetition Control Structures
Return Statement
• The return statement is used to exit
from the current method.
• Flow of control returns to the
statement that follows the original
method call.