CONTROL STRUCTURES
Unit 1 / Chapter 4
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
1. Simple if 8. For loop
statements
2. If Else
Statements 9. The enhanced
for loop
3. Nested if
Statements 10. The break
statement
4. Else if Ladder
11. The continue
5. Switch statement
Statements
12. Labelled Loops
6. The While
Statements
7. The do-while
CONTENTS statements
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
CONTROL STATEMENTS
1. Selection Statements
if and switch
2. Iteration or Loop Statements
for, while and do-while
3. Jump Statement
break, continue and return
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
1. SIMPLE IF STATEMENT
Syntax:
if(expression)
statement;
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
2. IF ELSE STATEMENT
Syntax:
if(expression)
statement1;
else
statement2;
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
3. IF NESTED STATEMENT
Syntax:
if(expression1) {
if(expression2)
statement1;
else
statement2; VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
4. ELSE IF LADDER
Syntax:
if(expression1) {
statement1; }
else if(expression2) {
statement2; }
else if(expression3) {
statement3; }
………………
else if(expression n) {
statement n; }
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
5. SWITCH STATEMENT
Switch(expression)
{
case value1: // expression 1
break;
case value2: // expression 2
break;
………………………
default : //do default function
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
6. THE WHILE STATEMENT
while (test_expression)
{
// statements- body of the loop
update expression;
}
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
7. THE DO-WHILE STATEMENT
do
{
// Loop Body
Update_expression;
} while (test_expression); // Condition check
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
8. FOR LOOP STATEMENT
for (initialization expr; test expr; update exp)
// body of the loop
// statements we want to execute
}
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
9. THE ENHANCED FOR LOOP
for ( declaration : expression )
{
// loop body
// statement(s)
}
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
The Java for-each loop or enhanced for loop is introduced
since J2SE 5.0.
It provides an alternative approach to traverse the array or
collection in Java.
It is mainly used to traverse the array or collection
elements.
The advantage of the for-each loop is that it eliminates the
possibility of bugs and makes the code more readable.
It is known as the for-each loop because it traverses each
element one by one. VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
The drawback of the enhanced for loop is that it cannot traverse the
elements in reverse order.
Here, you do not have the option to skip any element because it does not
work on an index basis.
Moreover, you cannot traverse the odd or even elements only.
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
10. THE BREAK STATEMENT
Break Statement is a loop control statement that is
used to terminate the loop.
As soon as the break statement is encountered from
within a loop, the loop iterations stop there, and control
returns from the loop immediately to the first statement
after the loop.
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
Syntax: break;
Basically, break statements are used in situations
when we are not sure about the actual number of
iterations for the loop, or we want to terminate the
loop based on some condition.
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
11. THE CONTINUE STATEMENT
Continue statement is often used inside in programming
languages inside loops control structures.
Inside the loop, when a continue statement is encountered the
control directly jumps to the beginning of the loop for the next
iteration instead of executing the statements of the current
iteration.
The continue statement is used when we want to skip a
particular condition and continue the rest execution.
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
Java continue statement is used for all type of loops, but it is generally used in for, while,
and do-while loops.
In the case of for loop, the continue keyword force control to jump immediately to the
update statement.
Whereas in the case of a while loop or do-while loop, control immediately jumps to the
Boolean expression.
Syntax: continue;
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
12. LABELLED LOOPS
A label is a valid variable name that denotes the name of the loop to where the control of
execution should jump.
To label a loop, place the label before the loop with a colon at the end. Therefore, a loop
with the label is called a labeled loop.
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
Syntax:
labelname:
for(initialization; condition; incr/decr)
{
//functionality of the loop
}
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE
THANK YOU
VIJAY KUMAR AS, ASST. PROF. SIMS, BANGLORE