The document discusses different types of control statements in Java including selection statements like if/else and switch statements, iteration statements like while, for, and foreach loops, and jump statements like break, continue, and return. Selection statements allow branching program execution based on conditions. Iteration statements allow repeating code blocks until a condition is met. Jump statements allow unconditionally transferring program control to another part of the program.
2. Java - Control Statements
Same as in C++ !
A program executes from top to bottom except when we use
control statements, we can control the order of execution of the
program, based on logic and values.
In Java, control statements can be divided into the following
three categories:
Selection Statements
Iteration Statements
Jump Statements
3. Java - Control Statements
In Java, control statements can be divided into the following
three categories:
Selection Statements
Iteration Statements
Jump Statements
4. Java – Control Statements
Selection Statements
• The if statements
• The if-else statements
• The if-else-if statements
• The switch statements
10. Java – Control Statements
Switch Statements The switch statement is a multi-way branch statement.
11. Java – Control Statements
Switch Statements The switch statement is a multi-way branch statement.
12. Java - Control Statements
In Java, control statements can be divided into the following
three categories:
Selection Statements
Iteration Statements
Jump Statements
13. Java – Control Statements
Iteration Statements
Repeating the same code fragment several times until a
specified condition is satisfied is called iteration
Java provides the following loop for iteration statements:
• The while loop
• The for loop
• The do-while loop
• The for each loop
14. Java – Control Statements
The While Loop - It continually executes a statement (that is usually be a block)
while a condition is true. The condition must return a Boolean value.
Output Do not forget to increment the
variable used in the condition,
otherwise the loop will never end!
15. Java – Control Statements
The for loop - A for loop executes a statement (that is usually a block) as long as the
Boolean condition evaluates to true.
• Statement 1 is executed (one time) before the execution
of the code block.
• Statement 2 defines the condition for executing the code
block.
• Statement 3 is executed (every time) after the code block
has been executed.
16. Java – Control Statements
The For loop
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be
less than 5). If the condition is true, the loop will start over again, if
it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in
the loop has been executed.
17. Java – Control Statements
The For each loop
This was introduced in Java 5. This loop is basically used to
traverse the array or collection elements.
18. Java – Control Statements
The For each loop
This was introduced in Java 5. This loop is basically used to
traverse the array or collection elements.
20. Java - Control Statements
In Java, control statements can be divided into the following
three categories:
Selection Statements
Iteration Statements
Jump Statements
21. Java - Control Statements
Jump statements are used to unconditionally transfer the
program control to another part of the program.
Java provides the following jump statements:
• break statement
• continue statement
• return statement
22. Java - Control Statements
Break Statement
The break statement immediately quits the current iteration
and goes to the first statement following the loop.
The break statement has the following two forms:
• Labeled Break Statement
• Unlabeled Break Statement
Unlabeled Break Statement: This is used to jump program
control out of the specific loop on the specific condition.
24. Java - Control Statements
Output !
Labeled Break Statement: This is used when we want to jump the
program control out of nested loops or multiple loops.
25. Java - Control Statements
Continue Statement
The continue statement is used when you want to continue running
the loop with the next iteration and want to skip the rest of the
statements of the body for the current iteration.
The continue statement has the following two forms:
• Labeled Continue Statement
• Unlabeled Continue Statement
Unlabeled Continue Statement: This statement skips the current
iteration of the innermost for, while and do-while loop.
27. Java - Control Statements
Output !
Labeled Continue Statement: This statement skips the current
iteration of the loop with the specified label.
28. Java - Control Statements
Return Statement - The return statement is used to immediately
quit the current method and return to the calling method. It is
mandatory to use a return statement for non-void methods to
return a value.
// a method for computing the area of the rectangle
public int getArea()
{
return width * height;
}