You are on page 1of 34

CONTROL STRUCTURES

 Java Statements
 Java Control Structures
 Decision Control Structures
 Repetition Control Structures
 Branching Statements
Java Statements

• In Java, a statement is one or more


lines of code ending with a semi-
colon (;).
• It generally contains expressions
(expressions have a value).
Java Statements
• Simple Statement
• Basic building blocks of a program
• One or more lines terminated by a
semicolon (;)
• Compound Statement or Block
• Used to organize simple statements into
complex structures
• One or more statements enclosed by braces
{}
• Recommended to indent blocks for
readability
Java Control Structures
• Control structures allow to change the
ordering of how the statements in a program
is executed
• Types of control structures:
• Decision control structures
• allow to select specific sections of code
to be executed
• Repetition control structures
• allow to execute specific sections of the
code a number of times
Decision Control Structures
• Java statements that allows a programmer to
select and execute specific blocks of code
while skipping other sections.
• Include types such as:
• If
• If-Else
• If-Else-If
• Switch structure
Decision Control Structures
If Structure
• The if statement specifies that a statement (or block
of code) will be executed if and only if a certain
Boolean statement is true.
• The general format of an if statement is:
if (expression)
statement or block;
or
if (expression) statement;
where expression represents a relational, equality,
or logical expression (conditional expression).
Decision Control Structures
• If there is a need to execute several statements
(compound statement), then left and right {} braces
can group these statements.

if (expression)
{
statement1;
statement2;

}
Decision Control Structures

Every time Java encounters an if statement,


1. It determines whether expression is true
or false.
2. If expression is true, then Java executes
statement.
3. If expression is false, then Java ignores or
skips the remainder of the if statement
and proceeds to the next statement.
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 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 (grade >= 75)


System.out.println(“You
Passed!”);
Decision Control Structures

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-Else Structure: Example 1


int grade = 68;
if (grade > 60)
System.out.println(“Congratulations!”
);
else
System.out.println(“Sorry you
failed!”);
Decision Control Structures

If-Else Structure: Example 2


int grade = 68;
if (grade > 60) {
System.out.println(“Congratulations!”);
System.out.println(“You Passed!”);
}
else {
System.out.println(“Sorry you failed!”);
}
Decision Control Structures
Nesting If and If-Else Structure
• The statement in the else-clause of an if-else block can be
another if-else structure.
• The general format of an if-else-else if statement is:

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

• Java statements that allows a programmer to


execute specific blocks of code a number of
times.
• Include types such as:
• While
• Do-While
• For
Repetition Control Structures
While Structure
• The while loop is a statement or block of
statements that is repeated as long as some
condition is satisfied.
• The general format of a while statement is:

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

Break Statement: Example


for (ctr = 1; ctr <= 100; ++ctr) {
System.out.print(“\nctr = “ +
ctr);
if (crt = = 50)
break;
}

This program segment will normally print all numbers


between 1 to 100. But because of the break statement,
only the numbers between 1 to 50 will be printed.
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

• Continue Statement: Example


for (ctr = 1; ctr <= 100; ++ctr) {
if (x%2 ! = 0)
continue;
System.out.print(“\n” + ctr);
}

This program segment will print all even numbers


between 1 to 100.
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.

You might also like